File tree Expand file tree Collapse file tree 2 files changed +88
-10
lines changed
src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps Expand file tree Collapse file tree 2 files changed +88
-10
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
Original file line number Diff line number Diff line change 15
15
[2 , 5 ],
16
16
[3 , 2 ]
17
17
],
18
- 'answer' : [0 , 1 ]
18
+ 'expected' : [0 , 1 ]
19
+ },
20
+ {
21
+ 'title' : 'Sample Test Case 2' ,
22
+ 'input' : [
23
+ [1 , 3 ],
24
+ [2 , 3 ],
25
+ [3 , 2 ],
26
+ [1 , 4 ],
27
+ [1 , 5 ],
28
+ [1 , 5 ],
29
+ [1 , 4 ],
30
+ [3 , 2 ],
31
+ [2 , 4 ],
32
+ [3 , 2 ]
33
+ ],
34
+ 'expected' : [0 , 1 , 1 ]
35
+ },
36
+ {
37
+ 'title' : 'Sample Test Case 3' ,
38
+ 'input' : [
39
+ [1 , 3 ],
40
+ [1 , 38 ],
41
+ [2 , 1 ],
42
+ [1 , 16 ],
43
+ [2 , 1 ],
44
+ [2 , 2 ],
45
+ [1 , 64 ],
46
+ [1 , 84 ],
47
+ [3 , 1 ],
48
+ [1 , 100 ],
49
+ [1 , 10 ],
50
+ [2 , 2 ],
51
+ [2 , 1 ],
52
+ [1 , 67 ],
53
+ [2 , 2 ],
54
+ [3 , 1 ],
55
+ [1 , 99 ],
56
+ [1 , 32 ],
57
+ [1 , 58 ],
58
+ [3 , 2 ]
59
+ ],
60
+ 'expected' : [1 , 1 , 0 ]
61
+ },
62
+ {
63
+ 'title' : 'Sample Test Case 3' ,
64
+ 'input' : [
65
+ [1 , 3 ],
66
+ [1 , 38 ],
67
+ [2 , 1 ],
68
+ [1 , 16 ],
69
+ [2 , 1 ],
70
+ [2 , 2 ],
71
+ [1 , 64 ],
72
+ [1 , 84 ],
73
+ [3 , 1 ],
74
+ [1 , 100 ],
75
+ [1 , 10 ],
76
+ [2 , 2 ],
77
+ [2 , 1 ],
78
+ [1 , 67 ],
79
+ [2 , 2 ],
80
+ [3 , 1 ],
81
+ [1 , 99 ],
82
+ [1 , 32 ],
83
+ [1 , 58 ],
84
+ [3 , 2 ]
85
+ ],
86
+ 'expected' : [1 , 1 , 0 ]
19
87
}
20
88
]
21
89
@@ -27,9 +95,9 @@ def test_freq_query(self):
27
95
for _ , _tt in enumerate (TEST_CASES ):
28
96
29
97
self .assertEqual (
30
- freq_query (_tt ['input' ]), _tt ['answer ' ],
98
+ freq_query (_tt ['input' ]), _tt ['expected ' ],
31
99
f"{ _ } | freq_query({ _tt ['input' ]} ) must be "
32
- f"=> { _tt ['answer ' ]} " )
100
+ f"=> { _tt ['expected ' ]} " )
33
101
34
102
def test_freq_query_edge_case (self ):
35
103
You can’t perform that action at this time.
0 commit comments