Permutations for digits represented by Phone Number -
I have an interview in 2 days and I have been having a very difficult time finding solutions to this question: What do I want What is it .. For any phone number .. The program should print out all the possible wires it represents. For example, the number can be replaced by A2 'A' or 'B' or 'C', 3 by 'D' E 'F' etc. In this way, how many possible passions can be given from a given phone number I do not want to write code for anyone ... a good algorithm or psuedocode would be great.
Thanks
d = {'2': "ABC", "3": "DEF", "4": "TMAV", "9": "WXYZ",}
"G.I.", "5": "JKL", "6 ":" MNO "," 7 ":" PQRS ", '8'
Given this, or any other D
, (executable) pseudo-code, all possible characters To change a string of digits in the string:
def Digstolets (digs): if lane (digs) == 0: yield '' returned earlier, rest = dig [0], dig [ 1:] if first Not in Dosage: Digstolets in X (rest): Come back to the first + x: For D in D in [first]: For distortlets in the rest: x + y
For example,
and9
(This version just resounds them!) Code> PR is emitted in this list
['1ADG', '1ADH', '1ADI', '1 AEG' 1 AEEH ',' 1 AE ',' 1 AFG ',' 1 AFH ',' 1 AF E ',' 1 BDG ',' 1 BDH ',' 1 BDI ',' 1 BGE ',' 1 BEH ',' 1 BEE ',' 1 BFG '' 1 BFH ',' 1 BFI ',' 1 CDG ',' 1 cdh ',' 1 cdi ',' 1 ceg ',' 1 cih ',' 1 siii ',' 1 cfg ',' 1 cfh ',' 1 cfi ']
Edit : Op asks for more clarification, here's an attempt. The function
digstolets
(letter by digit) takes a string of digitsdigs
and generates a sequence of strings of characters that can be letters or "non-digits" here But characters are not expanded because they are not like spaces and punctuation marks - only the digits2
and1
code> to9 < / Code> was extended (in most cases three possibilities, four in two cases, a Because the
7
can extend in anyPQRS
can expand and9
anyWXYZ
).First of all, the base case: if nothing is left (string
digs
is empty), the only possible result is an empty string, and all this, this made a recursive call. Goes, ends, concreteIf it is
dig
then it is not empty, a "head", first character, and a "tail", divided into all the rest (first 0 or more characters after the first one) Ho."head" stays either because this output, if any non-digit; Or any three or four possibilities, if one point, in any case, one, three or four potential extensions of the head should be connected to every possible detail of the tail - tail to get all the possible expansions of the tail. After this, recursive calls again, (That's why we probably all of the loop tail extension, and each of the possible expansions of one or three or four possible expansions of the head aligned with each possible extension of the tail. Production of each). And then, once again, th-that all people, people.
I do not know how to put it in any more primary form - If OP is still lost, then I only recommend a serious, total review of everything in relation to the recurrent. May be able to simplify this conceptual exhibition by removing recycling in favor of a clearly drawn heap - depending on the language involved in it (it would be good to hear how OP is completelyIs the language!), Recursion eradication can be an important adaptation, but it is a conceptual simplification ...! -)
Comments
Post a Comment