File tree Expand file tree Collapse file tree 1 file changed +17
-7
lines changed
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 1 file changed +17
-7
lines changed Original file line number Diff line number Diff line change 3
3
4
4
from typing import Dict
5
5
6
+ __INITIAL__ = 0
7
+
8
+ __INSERT__ = 1
9
+ __DELETE__ = 2
10
+ __SELECT__ = 3
11
+
12
+ __NOT_FOUND__ = 0
13
+ __FOUND__ = 1
14
+
6
15
7
16
def freq_query (queries ):
8
17
result = []
@@ -11,18 +20,19 @@ def freq_query(queries):
11
20
for query in queries :
12
21
operation = query [0 ]
13
22
data = query [1 ]
23
+ current = data_map .get (data , __INITIAL__ )
14
24
15
- if operation == 1 : # insert
16
- data_map [data ] = data_map . get ( data , 0 ) + 1
17
- elif operation == 2 and data_map . get ( data , 0 ) > 0 : # delete
18
- data_map [data ] -= 1
19
- elif operation == 3 : # "select"
25
+ if operation == __INSERT__ :
26
+ data_map [data ] = current + 1
27
+ elif operation == __DELETE__ :
28
+ data_map [data ] = max ( current - 1 , 0 )
29
+ elif operation == __SELECT__ :
20
30
for value in data_map .values ():
21
31
if value == data :
22
- result .append (1 )
32
+ result .append (__FOUND__ )
23
33
break
24
34
else :
25
- result .append (0 )
35
+ result .append (__NOT_FOUND__ )
26
36
else :
27
37
raise ValueError ('Invalid operation' )
28
38
You can’t perform that action at this time.
0 commit comments