Skip to content

Commit 12c68f9

Browse files
author
Gonzalo Diaz
committed
[REFACTOR] clean code: no magic numbers, comments removed.
1 parent 4eceae8 commit 12c68f9

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/frequency_queries.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
from typing import Dict
55

6+
__INITIAL__ = 0
7+
8+
__INSERT__ = 1
9+
__DELETE__ = 2
10+
__SELECT__ = 3
11+
612

713
def freq_query(queries):
814
result = []
@@ -11,12 +17,13 @@ def freq_query(queries):
1117
for query in queries:
1218
operation = query[0]
1319
data = query[1]
20+
current = data_map.get(data, __INITIAL__)
1421

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"
22+
if operation == __INSERT__:
23+
data_map[data] = current + 1
24+
elif operation == __DELETE__:
25+
data_map[data] = max(current - 1, 0)
26+
elif operation == __SELECT__:
2027
for value in data_map.values():
2128
if value == data:
2229
result.append(1)

0 commit comments

Comments
 (0)