Skip to content

Commit 6b21afb

Browse files
authored
Merge pull request #544 from sir-gon/develop
Develop
2 parents d7cc3f7 + 98584db commit 6b21afb

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33

44
from typing import Dict
55

6+
__INITIAL__ = 0
7+
8+
__INSERT__ = 1
9+
__DELETE__ = 2
10+
__SELECT__ = 3
11+
12+
__NOT_FOUND__ = 0
13+
__FOUND__ = 1
14+
615

716
def freq_query(queries):
817
result = []
@@ -11,18 +20,19 @@ def freq_query(queries):
1120
for query in queries:
1221
operation = query[0]
1322
data = query[1]
23+
current = data_map.get(data, __INITIAL__)
1424

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__:
2030
for value in data_map.values():
2131
if value == data:
22-
result.append(1)
32+
result.append(__FOUND__)
2333
break
2434
else:
25-
result.append(0)
35+
result.append(__NOT_FOUND__)
2636
else:
2737
raise ValueError('Invalid operation')
2838

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries_test.py

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,75 @@
1515
[2, 5],
1616
[3, 2]
1717
],
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]
1987
}
2088
]
2189

@@ -27,9 +95,9 @@ def test_freq_query(self):
2795
for _, _tt in enumerate(TEST_CASES):
2896

2997
self.assertEqual(
30-
freq_query(_tt['input']), _tt['answer'],
98+
freq_query(_tt['input']), _tt['expected'],
3199
f"{_} | freq_query({_tt['input']}) must be "
32-
f"=> {_tt['answer']}")
100+
f"=> {_tt['expected']}")
33101

34102
def test_freq_query_edge_case(self):
35103

0 commit comments

Comments
 (0)