Minion Game Python
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
Completed A challenge on HackerRank: Develop a game where a given string is split into all possible substrings and then depending on the beginning of the substring (vowel or consonant) determines which player (Kevin or Stuart) received a point. Hoping I could get advice on neatness, anything I did wrong/needs to be done better.
'''
https://www.hackerrank.com/challenges/the-minion-game/problem
Problem Statement: Both players are given the same string, s
Both players have to make substrings using the letters of the string s
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.
output format: Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
Created on Jan 13, 2018
@author: Anonymous3.1415
'''
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
def minion_game(given_string):
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
if vowels > consonants:
print("Kevin %d") % (vowels)
elif vowels == consonants:
print("draw")
else:
print("Stuart %d") % (consonants)
return
given_string = 'BANANA'
minion_game(given_string)
python beginner programming-challenge
add a comment |Â
up vote
3
down vote
favorite
Completed A challenge on HackerRank: Develop a game where a given string is split into all possible substrings and then depending on the beginning of the substring (vowel or consonant) determines which player (Kevin or Stuart) received a point. Hoping I could get advice on neatness, anything I did wrong/needs to be done better.
'''
https://www.hackerrank.com/challenges/the-minion-game/problem
Problem Statement: Both players are given the same string, s
Both players have to make substrings using the letters of the string s
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.
output format: Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
Created on Jan 13, 2018
@author: Anonymous3.1415
'''
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
def minion_game(given_string):
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
if vowels > consonants:
print("Kevin %d") % (vowels)
elif vowels == consonants:
print("draw")
else:
print("Stuart %d") % (consonants)
return
given_string = 'BANANA'
minion_game(given_string)
python beginner programming-challenge
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Completed A challenge on HackerRank: Develop a game where a given string is split into all possible substrings and then depending on the beginning of the substring (vowel or consonant) determines which player (Kevin or Stuart) received a point. Hoping I could get advice on neatness, anything I did wrong/needs to be done better.
'''
https://www.hackerrank.com/challenges/the-minion-game/problem
Problem Statement: Both players are given the same string, s
Both players have to make substrings using the letters of the string s
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.
output format: Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
Created on Jan 13, 2018
@author: Anonymous3.1415
'''
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
def minion_game(given_string):
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
if vowels > consonants:
print("Kevin %d") % (vowels)
elif vowels == consonants:
print("draw")
else:
print("Stuart %d") % (consonants)
return
given_string = 'BANANA'
minion_game(given_string)
python beginner programming-challenge
Completed A challenge on HackerRank: Develop a game where a given string is split into all possible substrings and then depending on the beginning of the substring (vowel or consonant) determines which player (Kevin or Stuart) received a point. Hoping I could get advice on neatness, anything I did wrong/needs to be done better.
'''
https://www.hackerrank.com/challenges/the-minion-game/problem
Problem Statement: Both players are given the same string, s
Both players have to make substrings using the letters of the string s
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.
output format: Print one line: the name of the winner and their score separated by a space.
If the game is a draw, print Draw.
Created on Jan 13, 2018
@author: Anonymous3.1415
'''
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
def minion_game(given_string):
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
if vowels > consonants:
print("Kevin %d") % (vowels)
elif vowels == consonants:
print("draw")
else:
print("Stuart %d") % (consonants)
return
given_string = 'BANANA'
minion_game(given_string)
python beginner programming-challenge
asked Jan 13 at 10:03
Anonymous3.1415
376212
376212
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
That comment adds no information to the method name. The method name is already very explicit. The comment rather even adds wrong information, because you don't actually split the string.
You could consider yield
ing the return values, so that you can iterate over them without needing the complete list in memory.
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
The variable name "split_string" lies about its contents. It contains the substrings
, not a split string. Saving the result of get_sub_strings
in it should give you a hint.
Don't iterate over the substrings with for i in xrange(...)
, if you don't use the index for anything than accessing the string. Instead do
for substring in get_substrings(given_string):
if substring[0].lower() in "aeiou":
# ...
Note that I also converted the string to lowercase before the comparison, in case the input string is not all uppercase.
You might also move that counting loop to its own method and return a tuple with the two counts, so that you can write
vowels, consonants = count_vowels_and_consonants(get_sub_strings(given_string))
elif vowels == consonants:
print("draw")
Note that the specification says to print Draw, not draw, so even if the hackerrank test is lenient one might be pedantic about the fulfillment of the specification.
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
That comment adds no information to the method name. The method name is already very explicit. The comment rather even adds wrong information, because you don't actually split the string.
You could consider yield
ing the return values, so that you can iterate over them without needing the complete list in memory.
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
The variable name "split_string" lies about its contents. It contains the substrings
, not a split string. Saving the result of get_sub_strings
in it should give you a hint.
Don't iterate over the substrings with for i in xrange(...)
, if you don't use the index for anything than accessing the string. Instead do
for substring in get_substrings(given_string):
if substring[0].lower() in "aeiou":
# ...
Note that I also converted the string to lowercase before the comparison, in case the input string is not all uppercase.
You might also move that counting loop to its own method and return a tuple with the two counts, so that you can write
vowels, consonants = count_vowels_and_consonants(get_sub_strings(given_string))
elif vowels == consonants:
print("draw")
Note that the specification says to print Draw, not draw, so even if the hackerrank test is lenient one might be pedantic about the fulfillment of the specification.
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
add a comment |Â
up vote
4
down vote
accepted
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
That comment adds no information to the method name. The method name is already very explicit. The comment rather even adds wrong information, because you don't actually split the string.
You could consider yield
ing the return values, so that you can iterate over them without needing the complete list in memory.
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
The variable name "split_string" lies about its contents. It contains the substrings
, not a split string. Saving the result of get_sub_strings
in it should give you a hint.
Don't iterate over the substrings with for i in xrange(...)
, if you don't use the index for anything than accessing the string. Instead do
for substring in get_substrings(given_string):
if substring[0].lower() in "aeiou":
# ...
Note that I also converted the string to lowercase before the comparison, in case the input string is not all uppercase.
You might also move that counting loop to its own method and return a tuple with the two counts, so that you can write
vowels, consonants = count_vowels_and_consonants(get_sub_strings(given_string))
elif vowels == consonants:
print("draw")
Note that the specification says to print Draw, not draw, so even if the hackerrank test is lenient one might be pedantic about the fulfillment of the specification.
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
That comment adds no information to the method name. The method name is already very explicit. The comment rather even adds wrong information, because you don't actually split the string.
You could consider yield
ing the return values, so that you can iterate over them without needing the complete list in memory.
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
The variable name "split_string" lies about its contents. It contains the substrings
, not a split string. Saving the result of get_sub_strings
in it should give you a hint.
Don't iterate over the substrings with for i in xrange(...)
, if you don't use the index for anything than accessing the string. Instead do
for substring in get_substrings(given_string):
if substring[0].lower() in "aeiou":
# ...
Note that I also converted the string to lowercase before the comparison, in case the input string is not all uppercase.
You might also move that counting loop to its own method and return a tuple with the two counts, so that you can write
vowels, consonants = count_vowels_and_consonants(get_sub_strings(given_string))
elif vowels == consonants:
print("draw")
Note that the specification says to print Draw, not draw, so even if the hackerrank test is lenient one might be pedantic about the fulfillment of the specification.
#splits the given string
def get_sub_strings(given_string):
string_leangth = len(given_string)
return [given_string[i:j+1] for i in xrange(string_leangth) for j in xrange(i, string_leangth)]
That comment adds no information to the method name. The method name is already very explicit. The comment rather even adds wrong information, because you don't actually split the string.
You could consider yield
ing the return values, so that you can iterate over them without needing the complete list in memory.
split_string = get_sub_strings(given_string)
vowels = 0
consonants = 0
for i in xrange(len(split_string)):
if split_string[i][0] in "AEIOU":
vowels += 1
else:
consonants += 1
The variable name "split_string" lies about its contents. It contains the substrings
, not a split string. Saving the result of get_sub_strings
in it should give you a hint.
Don't iterate over the substrings with for i in xrange(...)
, if you don't use the index for anything than accessing the string. Instead do
for substring in get_substrings(given_string):
if substring[0].lower() in "aeiou":
# ...
Note that I also converted the string to lowercase before the comparison, in case the input string is not all uppercase.
You might also move that counting loop to its own method and return a tuple with the two counts, so that you can write
vowels, consonants = count_vowels_and_consonants(get_sub_strings(given_string))
elif vowels == consonants:
print("draw")
Note that the specification says to print Draw, not draw, so even if the hackerrank test is lenient one might be pedantic about the fulfillment of the specification.
answered Jan 13 at 12:01
Raimund Krämer
1,808321
1,808321
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
add a comment |Â
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Could you by chance review my new code I left as an answer, I have made several new solutions to the challenge but this one is the quickest thus far. Consequently it's also the solution that took some paperwork to figure out! Also, I have a solution utilizing the yield function; I appreciate the advice to use that I learned a lot.
â Anonymous3.1415
Jan 14 at 0:53
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
Please do not post updates as answers. You can add information to the question by editing it, or if it is a completely new version of the code please open a new question. Add a link to the original one (this) in the new question, and to the new one in an edit of this question. I will then happily review the new version, if you just comment here again afterwards.
â Raimund Krämer
Jan 15 at 8:27
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185022%2fminion-game-python%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password