Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the popular correspondence table:</p> <pre><code>d = { '2': "ABC", '3': "DEF", '4': "GHI", '5': "JKL", '6': "MNO", '7': "PQRS", '8': "TUV", '9': "WXYZ", } </code></pre> <p>Given this, or any other <code>d</code>, (executable) pseudocode to transform a string of digits into all possible strings of letters:</p> <pre><code>def digstolets(digs): if len(digs) == 0: yield '' return first, rest = digs[0], digs[1:] if first not in d: for x in digstolets(rest): yield first + x return else: for x in d[first]: for y in digstolets(rest): yield x + y </code></pre> <p>tweakable depending on what you want to do for characters in the input string that aren't between <code>2</code> and <code>9</code> included (this version just echoes them out!-).</p> <p>For example,</p> <pre><code>print list(digstolets('1234')) </code></pre> <p>in this version emits</p> <pre><code>['1ADG', '1ADH', '1ADI', '1AEG', '1AEH', '1AEI', '1AFG', '1AFH', '1AFI', '1BDG', '1BDH', '1BDI', '1BEG', '1BEH', '1BEI', '1BFG', '1BFH', '1BFI', '1CDG', '1CDH', '1CDI', '1CEG', '1CEH', '1CEI', '1CFG', '1CFH', '1CFI'] </code></pre> <p><strong>Edit</strong>: the OP asks for more explanation, here's an attempt. Function <code>digstolets</code> (digits to letters) takes a string of digits <code>digs</code> and yields a sequence of strings of characters which can be letters or "non-digits". <code>0</code> and <code>1</code> count as non-digits here because they don't expand into letters, just like spaces and punctuations don't -- only digits <code>2</code> to <code>9</code> included expand to letters (three possibilities each in most cases, four in two cases, since <code>7</code> can expand to any of <code>PQRS</code> and <code>9</code> can expand to any of <code>WXYZ</code>).</p> <p>First, the base case: if nothing is left (string <code>digs</code> is empty), the only possible result is the empty string, and that's all, this recursive call is done, finished, kaput.</p> <p>If <code>digs</code> is non-empty it can be split into a "head", the first character, and a "tail", all the rest (0 or more characters after the first one).</p> <p>The "head" either stays as it is in the output, if a non-digit; or expands to any of three or four possibilities, if a digit. In either case, the one, three, or four possible expansions of the head must be concatenated with every possible expansion of the tail -- whence, the recursive call, to get all possible expansions of the tail (so we loop over all said possible expansion of the tail, and yield each of the one, three, or four possible expansions of the head concatenated with each possible expansion of the tail). And then, once again, th-th-that's all, folks.</p> <p>I don't know how to put this in terms that are any more elementary -- if the OP is still lost after THIS, I can only recommend a serious, total review of everything concerning recursion. Removing the recursion in favor of an explicitly maintained stack cannot <strong>simplify</strong> this conceptual exposition -- depending on the language involved (it would be nice to hear about what languages the OP is totally comfortable with!), recursion elimination can be an important optimization, but it's never a conceptual <em>simplification</em>...!-)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload