Last active
November 19, 2020 09:43
-
-
Save wildonion/e4bdc8cde72e7d330778d7ec705b0b48 to your computer and use it in GitHub Desktop.
total combination of phone number digits' chars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
digit_letter = {2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5:['j', 'k', 'l'], | |
6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z']} | |
digits = input("enter digits >>> ") | |
digits_list = list(digits) | |
assert 0 <= len(digits_list) <= 4, "length must be between 0 and 4" | |
digit_char = [] | |
for n in digits_list: | |
if int(n) in digit_letter.keys(): | |
digit_char.append(digit_letter[int(n)]) | |
# ============================================================================================= | |
# FUNCTIONS & CLASSES | |
def convertTobase(n, base): | |
if n == 0: | |
return 0 | |
else: | |
rim = [] | |
while n: # you can use recursive approach - because n is updating at the end of each iteration | |
# n, r = divmod(n, base) # divide and collect the remainder. | |
n = n // base | |
r = n % base | |
rim.append(r) | |
return ''.join(reversed(rim)) | |
total_combination = 1 | |
def total_combo(digit_char, idx): | |
global total_combination | |
if idx < 0: | |
return total_combination | |
else: | |
total_combination *= len(digit_char[idx]) | |
return total_combo(digit_char, idx-1) | |
def _convertTobase(n, base): | |
reminders = "0123456789ABCDEF" | |
if n < base: | |
return reminders[n] # because the reminder is n | |
else: | |
return _convertTobase(n//base, base) + reminders[n%base] # math rule >>> n = n // base * base + reminder | |
class Node: | |
def __init__(self, val): | |
self.val = val | |
self.children = None | |
def get_child(node): | |
for c in node.children: | |
n_c = {} | |
if c.children: | |
n_c[c.val] = [_c.val for _c in c.children] | |
yield n_c | |
get_child(c) | |
else: | |
n_c[c.val] = [_c.val for _c in c.children] | |
yield n_c | |
def is_leaf(char): | |
global COMBO, COMBOS | |
print(f"\t\t[⚫] processing for child of node {char}") | |
c = [n_d for n_d in nodes_child_dict if list(n_d.keys())[0] == char] | |
if c: | |
print(f"\t[_______{c}_______]") | |
for e in c: | |
for k in e: | |
for _c in e[k]: | |
is_leaf(_c) | |
else: | |
print(f"\t\t[⊝ ] node {char} is a leaf") | |
# ============================================================================================= | |
# METHODS | |
if len(digit_char) == 1: | |
print(f"Combination >>>> {digit_char[0]}") | |
elif len(digit_char) == 0: | |
print([]) | |
else: | |
# -------------- method 1 -------------- | |
total = total_combo(digit_char, len(digit_char)-1) | |
indices = [] | |
for idx in range(total): | |
ind = _convertTobase(idx, 4) if max([len(i) for i in digit_char]) == 4 else _convertTobase(idx, 3) # BUG ___ TODO | |
if len(ind) != len(digit_char): | |
zeros = ['0' for _ in range(len(digit_char)-len(ind))] | |
zeros = ''.join(zeros) | |
ind = zeros+ind | |
indices.append(ind) | |
combos = [] | |
for ind in indices: | |
c = '' | |
for i in range(len(ind)): | |
c+=digit_char[i][int(ind[i])] | |
combos.append(c) | |
print(f"-------method 1------- \n \t {combos}") | |
# -------------- method 2 -------------- | |
nodes = [] | |
COMBOS = [] | |
COMBO = '' | |
_N = '' | |
nodes_child_dict = [] | |
for d in range(len(digits)): | |
parent = Node(digits[d]) | |
parent.children = [Node(_c) for _c in digit_letter[int(digits[d])]] | |
idx = d+1 if d < len(digits) - 1 else False | |
if not idx: break | |
for c in range(len(parent.children)): | |
parent.children[c].children = [Node(_c) for _c in digit_letter[int(digits[idx])]] | |
nodes.append(parent) | |
for n in nodes: | |
for n_c in get_child(n): | |
nodes_child_dict.append(n_c) | |
print(f"-------method 2-------\n") | |
for node_dict in nodes_child_dict: | |
for node in node_dict: | |
_N = node | |
print(f"[☢ ] finding child of node {node}") | |
print("↳") | |
for c in node_dict[node]: | |
print(f"\t[⚫] processing for node {c}") | |
print("\t↳") | |
is_leaf(c) # TODO | |
print("\n") | |
print(f"\t\t{COMBOS}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment