-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminion.py
67 lines (51 loc) · 1.65 KB
/
minion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""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.
A player gets +1 point for each occurrence of the substring in the string .
Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
"""
def minion_game(s):
"""This method creates all subsets and then counts"""
vowels = set(['A', 'E', 'I', 'O', 'U'])
if len(set(string)) == 1:
sub = [string[0]]
else:
#This finds all subsets of words in subsequent order
sub = []
for i in range(len(s)):
sub.append(s[i])
for j in range(i+1, len(s)):
sub.append(s[i:j+1])
kevin_sum = 0
stuart_sum = 0
for item in sub:
if item[0] in vowels:
kevin_sum +=1
else:
stuart_sum += 1
if stuart_sum > kevin_sum:
print "Stuart %s" % stuart_sum
elif stuart_sum < kevin_sum:
print "Kevin %s" % kevin_sum
else:
print "Draw"
def minion_game_better_runtime(s):
"""This is O(n) runtime solution"""
vowels = set(['A', 'E', 'I', 'O', 'U'])
kevsc = 0
stusc = 0
for i in xrange(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"
minion_game_better_runtime('banana')