Minion Game Python

The name of the pictureThe name of the pictureThe name of the pictureClash 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)






share|improve this question

























    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)






    share|improve this question





















      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)






      share|improve this question











      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)








      share|improve this question










      share|improve this question




      share|improve this question









      asked Jan 13 at 10:03









      Anonymous3.1415

      376212




      376212




















          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 yielding 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.






          share|improve this answer





















          • 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











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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 yielding 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.






          share|improve this answer





















          • 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















          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 yielding 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.






          share|improve this answer





















          • 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













          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 yielding 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.






          share|improve this answer













          #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 yielding 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.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          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

















          • 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













           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?