Print the string equivalents of a phone numberGenerate parentheses solutionTesting if numbers in the array can be added up to equal the largest number in the arrayMystery sum with placeholder digitsFind total number of phone numbers formed by the movement of Knight and Bishop on keypadYear 0: Instruction FollowerLeetcode 17. Letter Combinations of a Phone NumberFind the minimum number of operations to convert 1 into n, and print the sequence of numbersCounting adjacent swaps to sort an array with 3 different valuesGenerate Letter Combinations of a Phone Number

What does it cost to buy a tavern?

Draw a symmetric alien head

Greeting with "Ho"

Where should a runway for a spaceplane be located?

Why does this compile? Returning nullptr as std::string

What are the current battlegrounds for people’s “rights” in the UK?

How can I ping multiple IP addresses at the same time?

What are the pros and cons for the two possible "gear directions" when parking the car on a hill?

Definition of 'vrit'

Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?

Why does Linux list NVMe drives as /dev/nvme0 instead of /dev/sda?

What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?

What is the most suitable position for a bishop here?

How do internally carried IR missiles acquire a lock?

Find the common ancestor between two nodes of a tree

Do I have to explain the mechanical superiority of the player-character within the fiction of the game?

What is "industrial ethernet"?

How much steel armor can you wear and still be able to swim?

Why is it easier to balance a non-moving bike standing up than sitting down?

How to work with PETG? Settings, caveats, etc

Syntax and semantics of XDV commands (XeTeX)

Why does independence imply zero correlation?

Are there any individual aliens that have gained superpowers in the Marvel universe?

Second 100 amp breaker inside existing 200 amp residential panel for new detached garage



Print the string equivalents of a phone number


Generate parentheses solutionTesting if numbers in the array can be added up to equal the largest number in the arrayMystery sum with placeholder digitsFind total number of phone numbers formed by the movement of Knight and Bishop on keypadYear 0: Instruction FollowerLeetcode 17. Letter Combinations of a Phone NumberFind the minimum number of operations to convert 1 into n, and print the sequence of numbersCounting adjacent swaps to sort an array with 3 different valuesGenerate Letter Combinations of a Phone Number






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








12












$begingroup$


Task



Old mobile phones had the ability to type characters by pressing a number. The letter a could be typed by pressing 2 once. The letter b could be typed by pressing 2 twice.



Given a sequence of numbers, give all possible letter combinations.



For example: The number 23 could give an output ad, ae, af, bd, be, bf, cd, ce, cf



numeric keypad with letter equivalents



My recursive solution to this problem is given below.



def num_to_char(value):
if value == 2: return ["a","b","c"]
if value == 3: return ["d","e","f"]
if value == 4: return ["g","h","i"]
if value == 5: return ["j","k","l"]
if value == 6: return ["m","n","o"]
if value == 7: return ["p","q","r","s"]
if value == 8: return ["t","u","v"]
if value == 9: return ["w","x","y","z"]

def convert_num(number, current_string = ""):
if number == []:
print(current_string)
return
get_list = num_to_char(int(number[0]))
for character in get_list:
current_string += character
convert_num(number[1:], current_string)
current_string = current_string[:-1]

num_to_covert = list("234")
convert_num(num_to_covert)










share|improve this question











$endgroup$


















    12












    $begingroup$


    Task



    Old mobile phones had the ability to type characters by pressing a number. The letter a could be typed by pressing 2 once. The letter b could be typed by pressing 2 twice.



    Given a sequence of numbers, give all possible letter combinations.



    For example: The number 23 could give an output ad, ae, af, bd, be, bf, cd, ce, cf



    numeric keypad with letter equivalents



    My recursive solution to this problem is given below.



    def num_to_char(value):
    if value == 2: return ["a","b","c"]
    if value == 3: return ["d","e","f"]
    if value == 4: return ["g","h","i"]
    if value == 5: return ["j","k","l"]
    if value == 6: return ["m","n","o"]
    if value == 7: return ["p","q","r","s"]
    if value == 8: return ["t","u","v"]
    if value == 9: return ["w","x","y","z"]

    def convert_num(number, current_string = ""):
    if number == []:
    print(current_string)
    return
    get_list = num_to_char(int(number[0]))
    for character in get_list:
    current_string += character
    convert_num(number[1:], current_string)
    current_string = current_string[:-1]

    num_to_covert = list("234")
    convert_num(num_to_covert)










    share|improve this question











    $endgroup$














      12












      12








      12





      $begingroup$


      Task



      Old mobile phones had the ability to type characters by pressing a number. The letter a could be typed by pressing 2 once. The letter b could be typed by pressing 2 twice.



      Given a sequence of numbers, give all possible letter combinations.



      For example: The number 23 could give an output ad, ae, af, bd, be, bf, cd, ce, cf



      numeric keypad with letter equivalents



      My recursive solution to this problem is given below.



      def num_to_char(value):
      if value == 2: return ["a","b","c"]
      if value == 3: return ["d","e","f"]
      if value == 4: return ["g","h","i"]
      if value == 5: return ["j","k","l"]
      if value == 6: return ["m","n","o"]
      if value == 7: return ["p","q","r","s"]
      if value == 8: return ["t","u","v"]
      if value == 9: return ["w","x","y","z"]

      def convert_num(number, current_string = ""):
      if number == []:
      print(current_string)
      return
      get_list = num_to_char(int(number[0]))
      for character in get_list:
      current_string += character
      convert_num(number[1:], current_string)
      current_string = current_string[:-1]

      num_to_covert = list("234")
      convert_num(num_to_covert)










      share|improve this question











      $endgroup$




      Task



      Old mobile phones had the ability to type characters by pressing a number. The letter a could be typed by pressing 2 once. The letter b could be typed by pressing 2 twice.



      Given a sequence of numbers, give all possible letter combinations.



      For example: The number 23 could give an output ad, ae, af, bd, be, bf, cd, ce, cf



      numeric keypad with letter equivalents



      My recursive solution to this problem is given below.



      def num_to_char(value):
      if value == 2: return ["a","b","c"]
      if value == 3: return ["d","e","f"]
      if value == 4: return ["g","h","i"]
      if value == 5: return ["j","k","l"]
      if value == 6: return ["m","n","o"]
      if value == 7: return ["p","q","r","s"]
      if value == 8: return ["t","u","v"]
      if value == 9: return ["w","x","y","z"]

      def convert_num(number, current_string = ""):
      if number == []:
      print(current_string)
      return
      get_list = num_to_char(int(number[0]))
      for character in get_list:
      current_string += character
      convert_num(number[1:], current_string)
      current_string = current_string[:-1]

      num_to_covert = list("234")
      convert_num(num_to_covert)







      python python-3.x programming-challenge






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 2 at 20:21









      200_success

      134k21166439




      134k21166439










      asked Jun 2 at 20:14









      EMLEML

      682113




      682113




















          1 Answer
          1






          active

          oldest

          votes


















          18












          $begingroup$

          You're working way too hard:




          • itertools.product() produces cartesian products.

          • You don't need to convert strings to lists; you can iterate over strings directly.

          • Lookups are better done using a dictionary than a chain of if statements.

          from itertools import product

          KEYPAD =
          '2': 'abc', '3': 'def',
          '4': 'ghi', '5': 'jkl', '6': 'mno',
          '7': 'pqrs', '8': 'tuv', '9': 'wxyz',


          def convert_num(number):
          letters = [KEYPAD[c] for c in number]
          return [''.join(combo) for combo in product(*letters)]

          print(convert_num('234'))





          share|improve this answer









          $endgroup$












          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            Jun 2 at 20:48






          • 8




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            Jun 2 at 20:55






          • 5




            $begingroup$
            @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
            $endgroup$
            – Graipher
            Jun 3 at 4:53










          • $begingroup$
            Beautiful code :)
            $endgroup$
            – JollyJoker
            Jun 3 at 7:51











          Your Answer






          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          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%2f221554%2fprint-the-string-equivalents-of-a-phone-number%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          18












          $begingroup$

          You're working way too hard:




          • itertools.product() produces cartesian products.

          • You don't need to convert strings to lists; you can iterate over strings directly.

          • Lookups are better done using a dictionary than a chain of if statements.

          from itertools import product

          KEYPAD =
          '2': 'abc', '3': 'def',
          '4': 'ghi', '5': 'jkl', '6': 'mno',
          '7': 'pqrs', '8': 'tuv', '9': 'wxyz',


          def convert_num(number):
          letters = [KEYPAD[c] for c in number]
          return [''.join(combo) for combo in product(*letters)]

          print(convert_num('234'))





          share|improve this answer









          $endgroup$












          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            Jun 2 at 20:48






          • 8




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            Jun 2 at 20:55






          • 5




            $begingroup$
            @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
            $endgroup$
            – Graipher
            Jun 3 at 4:53










          • $begingroup$
            Beautiful code :)
            $endgroup$
            – JollyJoker
            Jun 3 at 7:51















          18












          $begingroup$

          You're working way too hard:




          • itertools.product() produces cartesian products.

          • You don't need to convert strings to lists; you can iterate over strings directly.

          • Lookups are better done using a dictionary than a chain of if statements.

          from itertools import product

          KEYPAD =
          '2': 'abc', '3': 'def',
          '4': 'ghi', '5': 'jkl', '6': 'mno',
          '7': 'pqrs', '8': 'tuv', '9': 'wxyz',


          def convert_num(number):
          letters = [KEYPAD[c] for c in number]
          return [''.join(combo) for combo in product(*letters)]

          print(convert_num('234'))





          share|improve this answer









          $endgroup$












          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            Jun 2 at 20:48






          • 8




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            Jun 2 at 20:55






          • 5




            $begingroup$
            @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
            $endgroup$
            – Graipher
            Jun 3 at 4:53










          • $begingroup$
            Beautiful code :)
            $endgroup$
            – JollyJoker
            Jun 3 at 7:51













          18












          18








          18





          $begingroup$

          You're working way too hard:




          • itertools.product() produces cartesian products.

          • You don't need to convert strings to lists; you can iterate over strings directly.

          • Lookups are better done using a dictionary than a chain of if statements.

          from itertools import product

          KEYPAD =
          '2': 'abc', '3': 'def',
          '4': 'ghi', '5': 'jkl', '6': 'mno',
          '7': 'pqrs', '8': 'tuv', '9': 'wxyz',


          def convert_num(number):
          letters = [KEYPAD[c] for c in number]
          return [''.join(combo) for combo in product(*letters)]

          print(convert_num('234'))





          share|improve this answer









          $endgroup$



          You're working way too hard:




          • itertools.product() produces cartesian products.

          • You don't need to convert strings to lists; you can iterate over strings directly.

          • Lookups are better done using a dictionary than a chain of if statements.

          from itertools import product

          KEYPAD =
          '2': 'abc', '3': 'def',
          '4': 'ghi', '5': 'jkl', '6': 'mno',
          '7': 'pqrs', '8': 'tuv', '9': 'wxyz',


          def convert_num(number):
          letters = [KEYPAD[c] for c in number]
          return [''.join(combo) for combo in product(*letters)]

          print(convert_num('234'))






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 2 at 20:40









          200_success200_success

          134k21166439




          134k21166439











          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            Jun 2 at 20:48






          • 8




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            Jun 2 at 20:55






          • 5




            $begingroup$
            @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
            $endgroup$
            – Graipher
            Jun 3 at 4:53










          • $begingroup$
            Beautiful code :)
            $endgroup$
            – JollyJoker
            Jun 3 at 7:51
















          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            Jun 2 at 20:48






          • 8




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            Jun 2 at 20:55






          • 5




            $begingroup$
            @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
            $endgroup$
            – Graipher
            Jun 3 at 4:53










          • $begingroup$
            Beautiful code :)
            $endgroup$
            – JollyJoker
            Jun 3 at 7:51















          $begingroup$
          Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
          $endgroup$
          – EML
          Jun 2 at 20:48




          $begingroup$
          Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
          $endgroup$
          – EML
          Jun 2 at 20:48




          8




          8




          $begingroup$
          In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
          $endgroup$
          – 200_success
          Jun 2 at 20:55




          $begingroup$
          In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
          $endgroup$
          – 200_success
          Jun 2 at 20:55




          5




          5




          $begingroup$
          @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
          $endgroup$
          – Graipher
          Jun 3 at 4:53




          $begingroup$
          @200_success And if you don't find it in itertools, more_itertools might have it instead (although you need to install it separately).
          $endgroup$
          – Graipher
          Jun 3 at 4:53












          $begingroup$
          Beautiful code :)
          $endgroup$
          – JollyJoker
          Jun 3 at 7:51




          $begingroup$
          Beautiful code :)
          $endgroup$
          – JollyJoker
          Jun 3 at 7:51

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f221554%2fprint-the-string-equivalents-of-a-phone-number%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to write a 12-bar blues melodyI-IV-V blues progressionHow to play the bridges in a standard blues progressionHow does Gdim7 fit in C# minor?question on a certain chord progressionMusicology of Melody12 bar blues, spread rhythm: alternative to 6th chord to avoid finger stretchChord progressions/ Root key/ MelodiesHow to put chords (POP-EDM) under a given lead vocal melody (starting from a good knowledge in music theory)Are there “rules” for improvising with the minor pentatonic scale over 12-bar shuffle?Confusion about blues scale and chords

          What if the end-user didn't have the required library?What is setup.py?What is a clean, pythonic way to have multiple constructors in Python?What does Ruby have that Python doesn't, and vice versa?What is the reason for having '//' in Python?How do I create a namespace package in Python?How to package shared objects that python modules depend on?setuptools vs. distutils: why is distutils still a thing?Navigation in Windows 10 vs code not going to virtualenv library when the same library is installed at user levelPython create package for local usePackaging a project that uses multiple python versionsWhy is permission denied on pip install except for when “--user” is included at end of command?

          Why did Thanos need his ship to help him in the battle scene?Which actor plays Thanos in the Avengers mid-credits scene?Are there economic implications portrayed in comics where the buildings and cities are ruined almost daily?Old X-Men comic where team travels to alien world with a ring-like sun that needs recharging?Why does Ego need help sleeping?Is there an objective answer to who “the strongest Avenger” is?How did Banner get unstuck?Why did Thanos get hit?How did Thanos (or anyone) know the Infinity Stones would give him this power?Did Thanos leave Eitri alive for his after-sales service?In Avengers 1, why does Thanos need Loki?