Print the string equivalents of a phone numberTesting 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

Avoiding cliches when writing gods

Deformation of rectangular plot

When conversion from Integer to Single may lose precision

What does the "c." listed under weapon length mean?

What risks are there when you clear your cookies instead of logging off?

Do you need type ratings for private flying?

Implement Homestuck's Catenative Doomsday Dice Cascader

How is it possible that Gollum speaks Westron?

My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?

Did the first version of Linux developed by Linus Torvalds have a GUI?

Why does Kathryn say this in 12 Monkeys?

Does an ice chest packed full of frozen food need ice?

Question about JavaScript Math.random() and basic logic

What are the words for people who cause trouble believing they know better?

Do any instruments not produce overtones?

Building a road to escape Earth's gravity by making a pyramid on Antartica

Etymology of 'calcit(r)are'?

Is it possible to (7 day) schedule sleep time of a hard drive?

When writing an error prompt, should we end the sentence with a exclamation mark or a dot?

How were concentration and extermination camp guards recruited?

Secure offsite backup, even in the case of hacker root access

Quickest way to find characteristic polynomial from a given matrix

How to retract an idea already pitched to an employer?

How many pairs of subsets can be formed?



Print the string equivalents of a phone number


Testing 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;








3












$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$


















    3












    $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$














      3












      3








      3





      $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 8 hours ago









      200_success

      133k20162432




      133k20162432










      asked 8 hours ago









      EMLEML

      3167




      3167




















          1 Answer
          1






          active

          oldest

          votes


















          5












          $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
            8 hours ago






          • 3




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            8 hours ago










          • $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
            10 mins ago











          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









          5












          $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
            8 hours ago






          • 3




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            8 hours ago










          • $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
            10 mins ago















          5












          $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
            8 hours ago






          • 3




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            8 hours ago










          • $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
            10 mins ago













          5












          5








          5





          $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 8 hours ago









          200_success200_success

          133k20162432




          133k20162432











          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            8 hours ago






          • 3




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            8 hours ago










          • $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
            10 mins ago
















          • $begingroup$
            Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
            $endgroup$
            – EML
            8 hours ago






          • 3




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            8 hours ago










          • $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
            10 mins ago















          $begingroup$
          Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
          $endgroup$
          – EML
          8 hours ago




          $begingroup$
          Brilliant. Sadly I actually had no idea what a cartesian product was so thanks for educating me :)
          $endgroup$
          – EML
          8 hours ago




          3




          3




          $begingroup$
          In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
          $endgroup$
          – 200_success
          8 hours ago




          $begingroup$
          In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
          $endgroup$
          – 200_success
          8 hours ago












          $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
          10 mins ago




          $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
          10 mins ago

















          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

          Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

          Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

          Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її