No Title....

 from itertools import permutations as pm

'''Implementing My Own Count Function'''
def count(s:str,st:str)->int:
    '''
    s: The string in which we have to search the count of 'st'
    st: The string whose count we need to get in 's'
    Returns: The number of times st was in s, it's count
    '''
    # Sample Case: s->cdababa, st->'aba',count should be 2 not 1(as given by the count function of Python)
    
    # To traverse in st
    itr=0
    
    # The main count variable
    count=0
    
    # The sizes of both the strings
    n1=len(s)
    n2=len(st)
    #Just access every element of s
    for i in range(n1):
        chk=1
        if s[i]==st[0]:
            # print(f"{s[i]} at index {i} of s is equal to {st[0]} at index {0} of st. ")
            itr=i
            for j in range(n2):
                if itr<n1 and s[itr]!=st[j]:
                    chk=0
                    break
                if itr==n1-1 and j!=n2-1:
                    # When s is done but st was still left to be traversed
                    chk=0
                itr+=1
            if chk==1:
                #Means st is in s
                count+=1
    # print(f"The count of the string {st} in {s} is {count}.")  
    return count
    
# print(count("banana","an")).

def minion_game(string:str):
    # your code goes here
    substrs=[]
    '''
    Stuart: Consonant
    Kevin: Vowel
    '''
    stuart_score=0
    kevin_score=0
    n=len(string)
    for i in range(n-1):
        if string[i] not in substrs:
            substrs.append(string[i])
        for j in range(i+2,n+1):
            st=string[i:j]
            if st not in substrs:
                substrs.append(st)
    # print(substrs,len(substrs))
    for elem in substrs:
        if elem[0] in ['A','E','I','O','U']:
            # Then the elem is starting with a vowel
            kevin_score+=count(string,elem)
        else: 
            # Else the elem is starting with a consonant
            stuart_score+=count(string,elem)
    if kevin_score>stuart_score:
        print("Kevin",kevin_score)
    elif stuart_score>kevin_score:
        print("Stuart",stuart_score)
    else:
        # Draw
        print("Draw")

Comments

Popular Posts