forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert_commas.py
More file actions
130 lines (107 loc) · 3.21 KB
/
insert_commas.py
File metadata and controls
130 lines (107 loc) · 3.21 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""
This is a Python implementation of the how to insert commas
between a given sequence if string digit
For doctests run following command:
python -m doctest -v insert_commas.py
or
python3 -m doctest -v insert_commas.py
For manual testing run:
python insert_commas.py
"""
class StringBuilder(object):
def __init__(self, val: str) -> None:
self.store = [val]
def __iadd__(self, value):
"""appends a character to the sequence"""
self.store.append(value)
return self
def __str__(self) -> str:
"""string representation from the built sequence"""
return "".join(self.store)
class Node:
def __init__(self, info: str) -> None:
self.info = info
self.next = None
class Queue:
def __init__(self) -> None:
self.head = None
self.tail = None
def push(self, val: str) -> None:
"""add a new value to the queue"""
if self.head is None:
self.head = Node(val)
self.tail = self.head
else:
node = Node(val)
self.tail.next = node
self.tail = node
def pop(self) -> None:
"""removes a node from a queue"""
node = self.head
self.head = self.head.next
node.next = None
def peek(self) -> str:
"""retrieve the content of the queue's head"""
return self.head.info
def empty(self) -> bool:
"""checks if queue is empty"""
return True if self.head is None else False
def __str__(self) -> str:
"""string representation of a queue"""
ret = StringBuilder("")
current = self.head
while current:
ret += current.info
current = current.next
return str(ret)
def build_string_queue(string: str) -> Queue:
"""build a queue from the content of a string
:param string: sequence of digit
"""
ret = Queue()
i = 0
for idx, ch in enumerate(reversed(string)):
ret.push(ch)
i += 1
if i % 3 == 0:
if len(string) - 1 - idx == 0:
continue
ret.push(",")
i = 0
return ret
def extract_string_from_queue(queue: Queue) -> str:
"""extract content of a queue
:param queue: a queue object
"""
ret = StringBuilder("")
while not queue.empty():
ret += queue.peek()
queue.pop()
return str(ret)[::-1]
def insert_commas(string: str) -> str:
"""implementation of the how to insert commas
:param string: the string representation of a number.
:return: the string representation of the number delimited by commas.
Examples:
>>> insert_commas("1")
'1'
>>> insert_commas("100")
'100'
>>> insert_commas("100000")
'100,000'
>>> insert_commas("10000000")
'10,000,000'
>>> insert_commas("100000000000")
'100,000,000,000'
>>> insert_commas("634829301184902")
'634,829,301,184,902'
"""
q = build_string_queue(string)
return extract_string_from_queue(q)
if __name__ == "__main__":
print(insert_commas("1"))
print(insert_commas("100"))
print(insert_commas("1000"))
print(insert_commas("100000"))
print(insert_commas("10000000"))
print(insert_commas("634829301184902"))