A collection of implementations of common algorithms
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

#### 37 lines 825 B Raw Permalink Blame History

 `#! /usr/bin/env python3` ``` ``` `# find all possible permutations of a sequence of phone pad digits (other than 1 and 0)` ``` ``` `from itertools import chain` ``` ``` `mappings = {` ` 2 : "ABC",` ` 3 : "DEF",` ` 4 : "GHI",` ` 5 : "JKL",` ` 6 : "MNO",` ` 7 : "PQRS",` ` 8 : "TUV",` ` 9 : "WXYZ"` ` }` ``` ``` `def generate(letter, letters):` ` return [letter + c for c in letters]` ``` ``` `def genWords(one, two):` ` return list(chain.from_iterable([generate(c, two) for c in one]))` ``` ``` `def genAll(lettersets):` ` if len(lettersets) == 1:` ` return lettersets` ``` ``` ` first = lettersets` ` rest = lettersets[1:]` ` return [genWords(first, ls) for ls in genAll(rest)]` ``` ``` `def numToWords(num):` ` lettersets = [mappings[int(n)] for n in num]` ` return genAll(lettersets)` ``` ``` `for num in numToWords("353346"):` ``` print(num) ``` ``` ```