简体   繁体   中英

Minion game- Explain?


  1. I cannot understand how this logic is able to find out the permutations of every letter and able find its value/score. Can someone please explain this code? Both players are given the same string, . Both players have to make substrings using the letters of the string . Stuart has to make words starting with consonants. Kevin has to make words starting with vowels. The game ends when both players have made all possible substrings.

Scoring A player gets +1 point for each occurrence of the substring in the string .

For Example: String = BANANA Kevin's vowel beginning word = ANA

Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.

    s = raw_input()
    vowels = 'AEIOU'  
    kevsc = 0
    stusc = 0
    for i in range(len(s)):
        if s[i] in vowels:
            kevsc += (len(s)-i)
        else:
            stusc += (len(s)-i)    
    if kevsc > stusc:
        print "Kevin", kevsc
    elif kevsc < stusc:
        print "Stuart", stusc
    else:
        print "Draw"

I don't quite know what you mean by "find out the permutations of every letter", but this code is not doing anything related to permutations. What it is doing doesn't seem to make much sense.

It goes through the input string, and awards points to Kevin for each uppercase vowel, and Stuart for each other character. The number of points awarded in each case is equal to distance of the letter from the end of the input string, eg in the string "AB", Kevin will get 2 points for the A (because it is an uppercase vowel 2nd to last) and Stuart will get 1 point for B (because it is not an uppercase vowel, and it is last).

I was initially confused as to how we can find the number of substrings. Until I worked using pen and paper.

Let us make this easy. Consider the sample test case BANANA Step 1: There will be two cases here since we have vowels and consonants. It means we have conditions. So, we need to use if and else to decide who is the winner.

Then,

Think of how the game could run. Let us consider they will choose letter by letter and then make substrings out of it. I mean to say...

Consider B which is at position 1, then what are the possible substrings that we can make?

BANANA(string itself) - 1, BANAN - 2, BANA - 3, BAN - 4, BA - 5, B - 6. The count is '6' which is equal to length of the string or length subtracted by 0

The second letter is A, Similar to the above approach, we can form ANANA-1, ANAN-2,ANA-3,AN-4,A-5. The count is 5 which is length of the main string subtracted by 1

The third letter is N; NANA-1,NAN-2,NA-3,N-4(length-2 = 4)

The fourth letter is A; ANA-1,AN-2,A-3(length-3 = 3)

The fifth letter is N; NA-1,N-2(length-4 = 2)

The sixth letter is A; A-1(length-5 = 1)

Now add the results of vowels and consonants seperately.

consonant_sum(B,N,N) = 6+4+2 = 12 which is the score of 'Stuart'

vowels_sum(A,A,A) = 5+3+1 = 9 which is the score of 'Kevin'.

You iterate through the string based on its length which can be done using a for loop and range function.

So now you can simply output the winner using if,elif,else by comparing these sums. This made the problem very simple because we need to traverse through the whole string only once.

Finally! do not forget as I did to include a condition to print 'Draw' if the scores are equal.

Let me know your thoughts in case if something is unclear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM