Skip to content

Commit 1ca9470

Browse files
committed
Refactoring
1 parent ad06c8e commit 1ca9470

22 files changed

+304
-297
lines changed

Heap/HeapUtil.py

-52
This file was deleted.

Heap/__init__.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from heapq import heappush, heappop, heapify
2+
from functools import total_ordering
3+
from enum import Enum
4+
from typing import TypeVar, List, Generic
5+
6+
T = TypeVar('T')
7+
8+
9+
# why total_ordering: https://www.geeksforgeeks.org/python-functools-total_ordering/
10+
@total_ordering
11+
class __MinHeapElt__(Generic[T]):
12+
def __init__(self, data: T):
13+
self.data = data
14+
15+
def __lt__(self, other: T):
16+
return self.data < other.data
17+
18+
def __eq__(self, other: T):
19+
return self.data == other.data
20+
21+
22+
@total_ordering
23+
class __MaxHeapElt__(Generic[T]):
24+
def __init__(self, data: T):
25+
self.data = data
26+
27+
def __lt__(self, other: T):
28+
return self.data > other.data
29+
30+
def __eq__(self, other: T):
31+
return self.data == other.data
32+
33+
34+
class HeapType(Enum):
35+
MIN = "MIN"
36+
MAX = "MAX"
37+
38+
39+
class Heap(Generic[T]):
40+
def __int__(self, heap_type: HeapType):
41+
self._heap_: List[T] = []
42+
self._heapType_: HeapType = heap_type
43+
self._heapClass_ = __MinHeapElt__ if heap_type == HeapType.MIN else __MaxHeapElt__
44+
45+
def push(self, data: T):
46+
heappush(self._heap_, self._heapClass_(data))
47+
48+
def pop(self) -> T:
49+
return heappop(self._heap_)
50+
51+
def heapify(self):
52+
heapify(self._heap_)

LinkedList/addLinkedLists.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ def __init__(self, data, next=None):
66
def __str__(self):
77
return "{} | next -> ".format(self.data, self.next.data if self.next else None)
88

9+
910
"""
1011
Algorithm -
1112
@@ -25,6 +26,8 @@ def __str__(self):
2526
2627
References- https://www.geeksforgeeks.org/sum-of-two-linked-lists/
2728
"""
29+
30+
2831
class Adder(object):
2932
def __init__(self, head1, head2):
3033
self.head1 = head1
@@ -97,7 +100,7 @@ def __propagate_carry__(self, head, sublist_head):
97100
self.result_head = new_node
98101

99102
self.__sum__ += str(new_node.data)
100-
#temp = temp.next
103+
# temp = temp.next
101104

102105
@classmethod
103106
def get_size(cls, head):
@@ -112,9 +115,9 @@ def get_size(cls, head):
112115
def print_list(cls, head):
113116
cur = head
114117
while cur is not None:
115-
print str(cur.data) + "->",
118+
print(str(cur.data) + "->")
116119
cur = cur.next
117-
print None
120+
print(None)
118121

119122

120123
def create_linked_list(data=None):
@@ -126,6 +129,7 @@ def create_linked_list(data=None):
126129
prev = cur
127130
return head
128131

132+
129133
# Input:
130134
# First List: 5->6->3 // represents number 563
131135
# Second List: 8->4->2 // represents number 842
@@ -135,15 +139,15 @@ def create_linked_list(data=None):
135139
head1 = create_linked_list([2, 8, 9, 7])
136140
head2 = create_linked_list([1, 8])
137141

138-
print "List-1: ",
142+
print("List-1: ")
139143
Adder.print_list(head1)
140-
print "List-2: ",
144+
print("List-2: ")
141145
Adder.print_list(head2)
142146

143147
adder = Adder(head1, head2)
144148
result_sum, result_head = adder.add()
145149

146-
print "Result list: ",
150+
print("Result list: ")
147151
Adder.print_list(result_head)
148152

149-
print "Total sum: {}".format(result_sum)
153+
print("Total sum: {}".format(result_sum))

LinkedList/multiplyLinkedLists.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def __str__(self):
2323
2424
References- https://www.geeksforgeeks.org/multiply-two-numbers-represented-linked-lists/
2525
"""
26+
27+
2628
class Multiplier(object):
2729

2830
def __init__(self, head1, head2):
@@ -57,9 +59,9 @@ def get_size(cls, head):
5759
def print_list(cls, head):
5860
cur = head
5961
while cur is not None:
60-
print str(cur.data) + "->",
62+
print(str(cur.data) + "->")
6163
cur = cur.next
62-
print None
64+
print(None)
6365

6466

6567
def create_linked_list(data=[]):
@@ -75,12 +77,12 @@ def create_linked_list(data=[]):
7577
head1 = create_linked_list([1, 0, 5])
7678
head2 = create_linked_list([1, 2])
7779

78-
print "List-1: ",
80+
print("List-1: ")
7981
Multiplier.print_list(head1)
80-
print "List-2: ",
82+
print("List-2: ")
8183
Multiplier.print_list(head2)
8284

8385
multiplier = Multiplier(head1, head2)
8486
result = multiplier.multiply()
8587

86-
print "Result: {}".format(result)
88+
print("Result: {}".format(result))
File renamed without changes.

costExplorer/driver.py Misc/costExplorer/driver.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from costExplorer.entity.customer import Customer
2-
from costExplorer.entity.plan import Plan
3-
from costExplorer.entity.product_subscription import ProductSubscription
4-
from costExplorer.service.cost_service import CostService
1+
from Misc.costExplorer.entity.customer import Customer
2+
from Misc.costExplorer.entity.plan import Plan
3+
from Misc.costExplorer.entity.product_subscription import ProductSubscription
4+
from Misc.costExplorer.service.cost_service import CostService
55

66
if __name__ == '__main__':
77
costService: CostService = CostService()
File renamed without changes.

costExplorer/entity/customer.py Misc/costExplorer/entity/customer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from costExplorer.entity.product_subscription import ProductSubscription
1+
from Misc.costExplorer.entity.product_subscription import ProductSubscription
22
from typing import List
33

44
class Customer(object):
File renamed without changes.

costExplorer/entity/product_subscription.py Misc/costExplorer/entity/product_subscription.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from costExplorer.entity.plan import Plan
1+
from Misc.costExplorer.entity.plan import Plan
22

33

44
class ProductSubscription(object):
File renamed without changes.

costExplorer/service/cost_service.py Misc/costExplorer/service/cost_service.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from costExplorer.entity.customer import Customer
1+
from Misc.costExplorer.entity.customer import Customer
22
from typing import List
33
from datetime import date
4-
from costExplorer.entity.product_subscription import ProductSubscription
4+
from Misc.costExplorer.entity.product_subscription import ProductSubscription
55

66

77
class CostService(object):
File renamed without changes.

interview/m.py Misc/voting/m.py

File renamed without changes.
File renamed without changes.
File renamed without changes.

interview/main/main.py Misc/voting/main/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
Vote(["Mykola"]),
4646
]
4747
"""
48-
from interview.main.vote import Vote
49-
from interview.main.voteUtility import VoteUtility
48+
from Misc.voting.main.vote import Vote
49+
from Misc.voting.main.voteUtility import VoteUtility
5050

5151
if __name__ == '__main__':
5252
votes = [

interview/main/vote.py Misc/voting/main/vote.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import List
22

3-
from interview.main.candidate import Candidate
4-
53

64
class Vote(object):
75
def __init__(self, candidates: List[str]):

interview/main/voteUtility.py Misc/voting/main/voteUtility.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from interview.main.candidate import Candidate
1+
from Misc.voting.main.candidate import Candidate
22
from typing import Dict, List
33
from collections import defaultdict
44

0 commit comments

Comments
 (0)