A few days ago someone asked this question. But they quickly deleted the post offering no solution.
Given:
a = ['COP' , '\t\t\t', 'Basis', 'Notl', 'dv01', '6m', '9m', '1y',
'18m', '2y', '3y', "15.6", 'mm', '4.6', '4y', '5y', '10', 'mm',
'4.6', '6y', '7y', '8y', '9y', '10y', '20y', 'TOTAL', '\t\t9.2' ]
They needed to traverse the list and get the following 2D table. Where the first column has only values ending in "m" or "y".
"COB", "Basis", "Notl"
('6m', '', '')
('9m', '', '')
('1y', '', '')
('18m', '', '')
('2y', '', '')
('3y', '15.6', '')
('4y', '', '')
('5y', '10', '')
('6y', '', '')
('7y', '', '')
('8y', '', '')
('9y', '', '')
('10y', '', '')
('20y', '', '')
I answered the question, using a coroutine and a linked list as shown below:
a = ['COP' , '\t\t\t', 'Basis', 'Notl', 'dv01', '6m', '9m', '1y',
'18m', '2y', '3y', "15.6", 'mm', '4.6', '4y', '5y', '10', 'mm',
'4.6', '6y', '7y', '8y', '9y', '10y', '20y', 'TOTAL', '\t\t9.2' ]
headers = ('COP', "Basis", 'Notl', 'dv01')
def is_Number(value):
is_number = False
try:
float(value)
except ValueError:
pass
else:
is_number = True
return is_number
def is_cob(value):
return True if (value[0].isnumeric() and (value[-1] == "m" or value[-1] == "y")) \
else False
class Node:
def __init__(self, data=None, next_cop=None, next_column=None):
self.data = data
self.next_cop = None
self.next_column = None
class LinkedList:
def __init__(self):
self.headNode = None
def printlist(self):
print("This is the list =>:")
temp = self.headNode
while temp:
print("Data-> :", temp.data)
temp1 = temp.next_column
while temp1:
print("|")
print("--->Column: Bias, Notl =", temp1.data)
temp1 = temp1.next_column
temp = temp.next_cop
def isEmpty(self):
return False if self.headNode else True
def insert_at_head(self, node):
if self.isEmpty():
self.headNode = node
else:
node.next_cop = self.headNode
self.headNode = node
def insert_at_tail(self, node):
if self.isEmpty():
self.headNode = node
return
temp = self.headNode
while temp.next_cop:
temp = temp.next_cop
temp.next_cop = node
def insert_at_tail_column_of_nodeX(self, nodeX, node):
temp = self.get_pointer_to_nodeX(nodeX)
while temp.next_column:
temp = temp.next_column
temp.next_column = node
def get_pointer_to_nodeX(self, nodeX):
temp = self.headNode
while temp.next_cop != nodeX:
temp = temp.next_cop
temp = temp.next_cop
return temp
def get_last_node_data(self):
if self.isEmpty():
return self.headNode.data
temp = self.headNode
while temp.next_cop:
temp = temp.next_cop
return temp.data
def get_last_node(self):
if self.isEmpty():
return self.headNode
temp = self.headNode
while temp.next_cop:
temp = temp.next_cop
return temp
def iterate_from(self, node=None):
while node is not None:
yield node
node = node.next_cop
def rows(llist):
lst = []
while True:
value = yield
if value is None:
break
lst.append(value)
is_number = is_Number(value)
if is_cob(value):
llist.insert_at_tail(Node(value))
if is_number and is_cob(llist.get_last_node_data()) \
and (is_cob(lst[-2]) or is_Number(lst[-2])):
llist.insert_at_tail_column_of_nodeX(llist.get_last_node(), Node(value))
return llist
if __name__ == "__main__":
llist = LinkedList()
sent_to_coro = [elem for elem in a if elem not in headers]
coro_rows = rows(llist)
next(coro_rows)
for element in sent_to_coro:
print("Sending....", element)
coro_rows.send(element)
try:
coro_rows.send(None)
except StopIteration as exc:
result = exc.value
out = []
for item in llist.iterate_from(llist.headNode):
cop = item.data
basis = item.next_column.data if item.next_column else ""
notl = item.next_column.next_column.data if basis and item.next_column.next_column else ""
out.append((cop,basis,notl))
for itm in out:
print(itm)
Someone posted a solution with regex that was just a few lines of code, but unfortunately it was deleted because the question itself was just a question with no code and immediately started to be downvoted of course.
I would like to:
- Hear some suggestion on this code. Specifically, I wanted to know if there is any real improvement to this code.
- Is this solution overworked?
- I would like to see other solutions to this problem using Regex