-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_to_num.py
75 lines (60 loc) · 2.59 KB
/
string_to_num.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# code to convert a string(if possible) to a sequence of numbers where each number is an atomic number of an element represented by the string tokens extracted from the original string in order
from mendeleev import element
import sys
class EncodeTreeNode:
def __init__(self, content, operand):
self.content = content
self.single = None
self.double = None
self.triple = None
self.operand = operand
class EncodeTree:
def __init__(self, number):
self.head = EncodeTreeNode("", number) # empty node as head
self.valid_outcomes = []
def get_number(self, element_symbol, check_case = False):
if check_case:
if not (element_symbol[0].isupper() and (element_symbol[1:].islower() or len(element_symbol) == 1)):
return ""
try:
return str(element(element_symbol.capitalize()).atomic_number)+" "
except:
return ""
return ""
def routine(self, node, check_case=False):
if node.operand == "": # empty operand, chain finished
self.valid_outcomes.append(node.content) # add to the list of valid outcomes
return
# assign new nodes and call recursive routine on each routine
if len(node.operand) >= 1: # assign single
addend = self.get_number(node.operand[0], check_case)
if addend != "": # valid element, continue
node.single = EncodeTreeNode(node.content + addend, node.operand[1:])
self.routine(node.single, check_case)
if len(node.operand) >=2: #assign double
addend = self.get_number(node.operand[0:2], check_case)
if addend != "": # valid element, continue
node.double = EncodeTreeNode(node.content + addend, node.operand[2:])
self.routine(node.double, check_case)
if len(node.operand) >=3: #assign triple
addend = self.get_number(node.operand[0:3], check_case)
if addend != "": # valid element, continue
node.triple = EncodeTreeNode(node.content + addend, node.operand[3:])
self.routine(node.triple, check_case)
def populate(self, check_case=False):
self.routine(self.head, check_case)
print("Enter string to encode:")
name = input()
print("case sensitive? y/n")
check_case = input()
if check_case == "y":
check_case = True
elif check_case == "n":
check_case = False
else:
print("Invalid input! Please try again!")
sys.exit(-1)
tree = EncodeTree(name)
tree.populate(check_case)
print("Possible Encodings:")
print(tree.valid_outcomes)