Skip to content

Commit bf1eebf

Browse files
committed
Refactor: Improved variable names and used collections.Counter for shoe inventory management
1 parent 3cdf264 commit bf1eebf

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

Collections/CollectionsOrderedDict.py

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,29 @@
22
Title : Collections.OrderedDict()
33
Subdomain : Collections
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7-
Updated : 08 February 2023
7+
Updated : 26 November 2024
88
Problem : https://www.hackerrank.com/challenges/py-collections-ordereddict/problem
99
"""
1010

11-
import collections
12-
import re
11+
from collections import OrderedDict
1312

14-
n = int(input())
15-
item_od = collections.OrderedDict()
16-
for _ in range(n):
17-
record_list = re.split(r"(\d+)$", input().strip())
18-
item_name = record_list[0]
19-
item_price = int(record_list[1])
20-
if item_name not in item_od:
21-
item_od[item_name] = item_price
22-
else:
23-
item_od[item_name] = item_od[item_name] + item_price
24-
for i in item_od:
25-
print(f"{i}{item_od[i]}")
13+
14+
if __name__ == '__main__':
15+
ordered_dictionary = OrderedDict()
16+
17+
n = int(input())
18+
19+
for _ in range(n):
20+
s = list(input().split())
21+
price = int(s[len(s) - 1])
22+
name = ' '.join(s[:len(s) - 1])
23+
24+
if ordered_dictionary.get(name):
25+
ordered_dictionary[name] += price
26+
else:
27+
ordered_dictionary[name] = price
28+
29+
for key, value in ordered_dictionary.items():
30+
print(key, value)

Collections/collectionsCounter.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
Title : collections.Counter()
33
Subdomain : Collections
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 26 November 2024
78
Problem : https://www.hackerrank.com/challenges/collections-counter/problem
89
"""
10+
import collections
11+
12+
13+
if __name__ == '__main__':
14+
total_shoes = int(input())
15+
shoe_sizes = list(map(int, input().split()))
16+
inventory = collections.Counter(shoe_sizes)
17+
18+
total_customers = int(input())
19+
total_revenue = 0
20+
21+
for _ in range(total_customers):
22+
size, price = map(int, input().split())
23+
if inventory.get(size) and inventory[size] > 0:
24+
total_revenue += price
25+
inventory[size] -= 1
26+
27+
print(total_revenue)
28+
929

10-
x = int(input())
11-
shoe_size = list(map(int, input().split()))
12-
n = int(input())
13-
sell = 0
14-
for _ in range(n):
15-
s, p = map(int, input().split())
16-
if s in shoe_size:
17-
sell = sell + p
18-
shoe_size.remove(s)
19-
print(sell)

0 commit comments

Comments
 (0)