-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcodechallenge_129.py
244 lines (202 loc) · 5.94 KB
/
codechallenge_129.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'''
Date: 08/07/2002
Task description:
-----------------
Asked by Dropbox.
Create a data structure that performs all the following operations in O(1) time:
- plus: Add a key with value 1. If the key already exists, increment its value by one.
- minus: Decrement the value of a key. If the key's value is currently 1, remove it.
- get_max: Return a key with the highest value.
- get_min: Return a key with the lowest value.
Pseudo Code:
------------
Complexity order O(1) can be achieved using dictionary.
* plus: O(1)
Add a key of value 1 while checking key in dict
* minus: O(1)
Delete/update a key if value exists in dict and value is 1
* Retrieval: O(1)
Return a key with the highest value
Return a key with the lowest value
Return all keys in dict
Obervation:
* The key facts about python complexities for dictionary:
adict[key] = value --> O(1)
adict[key] --> O(1)
adict.get(key) --> O(1)
key in adict --> O(1)
for key in adict --> O(N)
* Likewise for set:
aset.add(val) --> O(1)
val in aset --> O(1)
for val in aset --> O(N)
However, set is not applicable to key,value pairs.
'''
import unittest
import os, time
from unittest.runner import TextTestResult
# Solution using dictionary
class Solution():
def __init__(self):
self.d = {}
# Add a key with value 1. If the key already exists, increment its value by one.
def plus(self, key):
if key in self.d:
self.d[key] += 1
# print(self.d[key])
else:
self.d[key] = 1
# Decrement the value of a key. If the key's value is currently 1, remove it.
def minus(self, key):
if key in self.d:
if self.d[key] == 1:
del self.d[key]
else:
self.d[key] -= 1
# return a key with the highest value
def get_max(self):
if (len(self.d) == 0):
return None
return max(self.d, key=self.d.get)
# return a key with the lowest value
def get_min(self):
if (len(self.d) == 0):
return None
return min(self.d, key=self.d.get)
# return a list of keys
def get_all(self):
return self.d.values()
class Test(unittest.TestCase):
testD = Solution();
def test_plus(self):
self.testD.plus(0)
self.testD.plus(1)
self.testD.plus(2)
self.testD.plus(3)
self.testD.plus(3)
def test_get_max(self):
self.testD.plus(0)
self.testD.plus(1)
self.testD.plus(2)
self.testD.plus(3)
self.testD.plus(3)
self.assertEqual(self.testD.get_max(), 3)
def test_get_min(self):
self.testD.plus(0)
self.testD.plus(1)
self.testD.plus(2)
self.testD.plus(3)
self.testD.plus(3)
self.assertEqual(self.testD.get_min(), 0)
def test_minus(self):
self.testD.minus(0)
self.testD.minus(1)
self.testD.minus(2)
self.testD.minus(3)
self.testD.minus(3)
self.testD.minus(0)
self.testD.minus(1)
self.testD.minus(2)
self.testD.minus(3)
self.testD.minus(3)
self.assertEqual(self.testD.get_min(), None)
def run_dev_tests():
testD = Solution();
testD.plus(0)
testD.plus(1)
testD.plus(2)
testD.plus(3)
testD.plus(3)
all = testD.get_all()
for key,value in enumerate(all):
print("Key: {} -- Value: {}".format(key,value))
print(testD.get_max())
print(testD.get_min())
def main():
# add some keys to the dictionary
D = Solution()
for i in range(10):
D.plus(i)
for i in range(4,9):
D.plus(i)
for i in range(3,10):
D.plus(i)
for i in range(8,10):
D.plus(i)
# check all
all = D.get_all()
for key,value in enumerate(all):
print("Key: {} -- Value: {}".format(key,value))
# get the highest and lowest value
print("Key with max value: {}".format(D.get_max()))
print("Key with min value: {}".format(D.get_min()))
# minus a non-existing key
D.minus(10)
# check all
all = D.get_all()
for key,value in enumerate(all):
print("Key: {} -- Value: {}".format(key,value))
# reverse the process. expecting none to be returned
print("Reversing the process...")
for i in range(10):
D.minus(i)
for i in range(4,9):
D.minus(i)
for i in range(3,10):
D.minus(i)
for i in range(8,10):
D.minus(i)
# check all
print("Now, expect None to be returned.:")
all = D.get_all()
for key,value in enumerate(all):
print("Key: {} -- Value: {}".format(key,value))
print(D.get_max())
if __name__ == '__main__':
if os.environ.get('UNITTEST_ONLY') != False:
main()
# run_dev_tests()
else:
try:
unittest.main()
except Exception as e:
print(e)
'''
Runtime Output:
----------------
* Dev_tests:
PS D:\DEVEL\GIT\DailyCodingChallenge> pipenv run python .\codechallenge_129.py
Loading .env environment variables...
Key: 0 -- Value: 1
Key: 1 -- Value: 1
Key: 2 -- Value: 1
Key: 3 -- Value: 2
Key: 4 -- Value: 3
Key: 5 -- Value: 3
Key: 6 -- Value: 3
Key: 7 -- Value: 3
Key: 8 -- Value: 4
Key: 9 -- Value: 3
Key with max value: 8
Key with min value: 0
Key: 0 -- Value: 1
Key: 1 -- Value: 1
Key: 2 -- Value: 1
Key: 3 -- Value: 2
Key: 4 -- Value: 3
Key: 5 -- Value: 3
Key: 6 -- Value: 3
Key: 7 -- Value: 3
Key: 8 -- Value: 4
Key: 9 -- Value: 3
Reversing the process...
Now, expect None to be returned.:
None
* Unit tests:
PS D:\DEVEL\GIT\DailyCodingChallenge> pipenv run python .\codechallenge_129.py
Loading .env environment variables...
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK
'''