Print the string equivalents of a phone numberTesting if numbers in the array can be added up to equal the...

How many times can you cast a card exiled by Release to the Wind?

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

Implement Homestuck's Catenative Doomsday Dice Cascader

Java guess the number

Is it possible to express disjunction through conjunction and implication?

Last survivors from different time periods living together

Do any instruments not produce overtones?

How hard would it be to convert a glider into an powered electric aircraft?

Payment instructions from HomeAway look fishy to me

Phone number to a lounge, or lounges generally

2.8 is missing the Carve option in the Boolean Modifier

Can a user sell my software (MIT license) without modification?

How to generate random points without duplication?

Do the English have an ancient (obsolete) verb for the action of the book opening?

How bad would a partial hash leak be, realistically?

Does the growth of home value benefit from compound interest?

Bent spoke design wheels — feasible?

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

Russian equivalents of "no love lost"

After the loss of Challenger, why weren’t Galileo and Ulysses launched by Centaurs on expendable boosters?

What are the peak hours for public transportation in Paris?

Why is the application of an oracle function not a measurement?

How can drunken, homicidal elves successfully conduct a wild hunt?

Notation of last measure of a song with a pickup measure



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


















          4












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






          • 2




            $begingroup$
            In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
            $endgroup$
            – 200_success
            7 hours 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









          4












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






          • 2




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
















          4












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






          • 2




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














          4












          4








          4





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






          • 2




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


















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






          • 2




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
















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




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




          2




          2




          $begingroup$
          In general, any time you want to do some kind of fancy iteration in Python, look at itertools first.
          $endgroup$
          – 200_success
          7 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
          7 hours 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

          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

          Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m