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

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
$endgroup$
add a comment |
$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

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
$endgroup$
add a comment |
$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

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

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
python python-3.x programming-challenge
edited Jun 2 at 20:21
200_success
134k21166439
134k21166439
asked Jun 2 at 20:14
EMLEML
682113
682113
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$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
ifstatements.
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'))
$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 atitertoolsfirst.
$endgroup$
– 200_success
Jun 2 at 20:55
5
$begingroup$
@200_success And if you don't find it initertools,more_itertoolsmight 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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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
ifstatements.
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'))
$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 atitertoolsfirst.
$endgroup$
– 200_success
Jun 2 at 20:55
5
$begingroup$
@200_success And if you don't find it initertools,more_itertoolsmight 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
add a comment |
$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
ifstatements.
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'))
$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 atitertoolsfirst.
$endgroup$
– 200_success
Jun 2 at 20:55
5
$begingroup$
@200_success And if you don't find it initertools,more_itertoolsmight 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
add a comment |
$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
ifstatements.
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'))
$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
ifstatements.
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'))
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 atitertoolsfirst.
$endgroup$
– 200_success
Jun 2 at 20:55
5
$begingroup$
@200_success And if you don't find it initertools,more_itertoolsmight 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
add a comment |
$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 atitertoolsfirst.
$endgroup$
– 200_success
Jun 2 at 20:55
5
$begingroup$
@200_success And if you don't find it initertools,more_itertoolsmight 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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