Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Last active September 20, 2016 17:08
Show Gist options
  • Save CleverProgrammer/fbdcd4d17b4b1982593930c5a0d27d2a to your computer and use it in GitHub Desktop.
Save CleverProgrammer/fbdcd4d17b4b1982593930c5a0d27d2a to your computer and use it in GitHub Desktop.
Hackerrank Minion Game problem
"""
Submit this code at: https://www.hackerrank.com/challenges/the-minion-game
"""
def match(text, pattern):
"""
>>> match('banana', 'ana')
2
"""
return sum([1 for i in range(len(text)) if text[i:i+len(pattern)] == pattern])
def substrings(word):
"""
>>> substrings('hello')
['h', 'he', 'hel', 'hell', 'hello']
"""
return [word[:i+1] for i in reversed(range(len(word)))]
def all_substrings(word):
"""
>>> all_substrings('hel')
[['h', 'he', 'hel'], ['e', 'el'], ['l']]
"""
all_subs = []
for i in range(len(word)):
all_subs += substrings(word[i:])
return set(all_subs)
def minion(word):
stuart = 0
kevin = 0
consonants = 'bcdfghjklmnpqrstvwxyz' + 'bcdfghjklmnpqrstvwxyz'.upper()
vowels = 'aeiou' + 'aeiou'.upper()
for string in all_substrings(word):
if string[0] in consonants:
stuart += match(word, string)
elif string[0] in vowels:
kevin += match(word, string)
if stuart > kevin:
return 'Stuart {}'.format(stuart)
elif stuart < kevin:
return 'Kevin {}'.format(kevin)
else:
return 'Draw'
print(minion(input()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment