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;
$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 8 hours ago
200_success
133k20162432
133k20162432
asked 8 hours ago
EMLEML
3167
3167
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
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'))
$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 atitertools
first.
$endgroup$
– 200_success
8 hours ago
$begingroup$
@200_success And if you don't find it initertools
,more_itertools
might have it instead (although you need to install it separately).
$endgroup$
– Graipher
10 mins ago
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
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'))
$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 atitertools
first.
$endgroup$
– 200_success
8 hours ago
$begingroup$
@200_success And if you don't find it initertools
,more_itertools
might have it instead (although you need to install it separately).
$endgroup$
– Graipher
10 mins ago
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
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'))
$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 atitertools
first.
$endgroup$
– 200_success
8 hours ago
$begingroup$
@200_success And if you don't find it initertools
,more_itertools
might have it instead (although you need to install it separately).
$endgroup$
– Graipher
10 mins ago
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
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'))
$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'))
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 atitertools
first.
$endgroup$
– 200_success
8 hours ago
$begingroup$
@200_success And if you don't find it initertools
,more_itertools
might have it instead (although you need to install it separately).
$endgroup$
– Graipher
10 mins ago
add a comment |
$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 atitertools
first.
$endgroup$
– 200_success
8 hours ago
$begingroup$
@200_success And if you don't find it initertools
,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
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