Skip to content

Commit 24e5b18

Browse files
authored
Merge pull request #59 from samshad/samshad
Refactored codes. Domain: Python | Subdomain: NumPy
2 parents bc8921a + a420934 commit 24e5b18

17 files changed

+193
-162
lines changed

Collections/CollectionsOrderedDict.py

+21-16
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

+21-11
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)

Numpy/ArrayMathematics.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@
22
Title : Array Mathematics
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-array-mathematics/problem
89
"""
10+
import numpy as np
911

10-
import numpy
12+
if __name__ == '__main__':
13+
n, m = map(int, input().split())
1114

12-
n, m = map(int, input().split())
13-
ar1 = []
14-
ar2 = []
15-
for _ in range(n):
16-
tmp = list(map(int, input().split()))
17-
ar1.append(tmp)
18-
for _ in range(n):
19-
tmp = list(map(int, input().split()))
20-
ar2.append(tmp)
21-
np_ar1 = numpy.array(ar1)
22-
np_ar2 = numpy.array(ar2)
23-
print(np_ar1 + np_ar2)
24-
print(np_ar1 - np_ar2)
25-
print(np_ar1 * np_ar2)
26-
print(np_ar1 // np_ar2)
27-
print(np_ar1 % np_ar2)
28-
print(np_ar1**np_ar2)
15+
16+
ar1 = np.array([input().split() for _ in range(n)], int)
17+
ar2 = np.array([input().split() for _ in range(n)], int)
18+
19+
print(ar1 + ar2)
20+
print(ar1 - ar2)
21+
print(ar1 * ar2)
22+
print(ar1 // ar2)
23+
print(ar1 % ar2)
24+
print(ar1 ** ar2)

Numpy/Arrays.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
Title : Arrays
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-arrays/problem
89
"""
910

1011
import numpy
1112

12-
ar = list(map(float, input().split()))
13-
np_ar = numpy.array(ar, float)
14-
print(np_ar[::-1])
13+
def arrays(arr):
14+
np_ar = numpy.array(arr, float)
15+
return np_ar[::-1]
16+
17+
arr = input().strip().split(' ')
18+
result = arrays(arr)
19+
print(result)

Numpy/Concatenate.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22
Title : Concatenate
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-concatenate/problem
89
"""
910

10-
import numpy
11+
import numpy as np
1112

12-
n, m, p = map(int, input().split())
1313

14-
ar1 = []
15-
ar2 = []
16-
for _ in range(n):
17-
tmp = list(map(int, input().split()))
18-
ar1.append(tmp)
19-
for _ in range(m):
20-
tmp = list(map(int, input().split()))
21-
ar2.append(tmp)
22-
np_ar1 = numpy.array(ar1)
23-
np_ar2 = numpy.array(ar2)
24-
print(numpy.concatenate((np_ar1, np_ar2), axis=0))
14+
if __name__ == '__main__':
15+
n, m, p = map(int, input().split())
16+
17+
arr1 = []
18+
arr2 = []
19+
for _ in range(n):
20+
tmp = list(map(int, input().split()))
21+
arr1.append(tmp)
22+
for _ in range(m):
23+
tmp = list(map(int, input().split()))
24+
arr2.append(tmp)
25+
np_arr1 = np.array(arr1)
26+
np_arr2 = np.array(arr2)
27+
print(np.concatenate((np_arr1, np_arr2), axis=0))

Numpy/DotandCross.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
Title : Dot and Cross
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-dot-and-cross/problem
89
"""
10+
import numpy as np
911

10-
import numpy
12+
if __name__ == '__main__':
13+
n = int(input())
1114

12-
n = int(input())
13-
ar1 = []
14-
ar2 = []
15-
for _ in range(n):
16-
tmp = list(map(int, input().split()))
17-
ar1.append(tmp)
18-
np_ar1 = numpy.array(ar1)
19-
for _ in range(n):
20-
tmp = list(map(int, input().split()))
21-
ar2.append(tmp)
22-
np_ar2 = numpy.array(ar2)
23-
print(numpy.dot(np_ar1, np_ar2))
15+
arr1 = np.array([input().split() for _ in range(n)], int)
16+
arr2 = np.array([input().split() for _ in range(n)], int)
17+
18+
print(np.dot(arr1, arr2))

Numpy/EyeandIdentity.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
Title : Eye and Identity
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7-
Updated : 17 March 2021
7+
Updated : 25 November 2024
88
Problem : https://www.hackerrank.com/challenges/np-eye-and-identity/problem
99
"""
1010

1111
import numpy as np
1212

13-
np.set_printoptions(legacy="1.13")
14-
n, m = map(int, input().split())
15-
print(np.eye(n, m, k=0))
13+
14+
if __name__ == '__main__':
15+
np.set_printoptions(legacy="1.13")
16+
n, m = map(int, input().split())
17+
print(np.eye(n, m, k=0))

Numpy/FloorCeilandRint.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
Title : Floor, Ceil and Rint
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7-
Updated : 17 March 2021
7+
Updated : 25 November 2024
88
Problem : https://www.hackerrank.com/challenges/floor-ceil-and-rint/problem
99
"""
1010

1111
import numpy as np
1212

13-
np.set_printoptions(legacy="1.13")
14-
A = np.array(input().split(), float)
15-
print(np.floor(A))
16-
print(np.ceil(A))
17-
print(np.rint(A))
1813

19-
# Alternate solution
20-
# print(*(f(A) for f in [np.floor, np.ceil, np.rint]), sep='\n')
14+
if __name__ == '__main__':
15+
np.set_printoptions(legacy="1.13")
16+
arr = np.array(input().split(), float)
17+
print(np.floor(arr))
18+
print(np.ceil(arr))
19+
print(np.rint(arr))
20+
21+
# Alternate solution
22+
# print(*(f(A) for f in [np.floor, np.ceil, np.rint]), sep='\n')

Numpy/InnerandOuter.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
Title : Inner and Outer
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-inner-and-outer/problem
89
"""
10+
import numpy as np
911

10-
import numpy
1112

12-
np_ar1 = numpy.array(list(map(int, input().split())))
13-
np_ar2 = numpy.array(list(map(int, input().split())))
14-
print(numpy.inner(np_ar1, np_ar2))
15-
print(numpy.outer(np_ar1, np_ar2))
13+
if __name__ == '__main__':
14+
arr1 = np.array(list(map(int, input().split())))
15+
arr2 = np.array(list(map(int, input().split())))
16+
17+
print(np.inner(arr1, arr2))
18+
print(np.outer(arr1, arr2))

Numpy/LinearAlgebra.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
Title : Linear Algebra
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7-
Updated : 3 April 2021
7+
Updated : 25 November 2024
88
Problem : https://www.hackerrank.com/challenges/np-linear-algebra/problem
99
"""
10-
1110
import numpy as np
1211

13-
np.set_printoptions(legacy="1.13")
14-
n = int(input())
15-
array = np.array([input().split() for _ in range(n)], float)
16-
print(np.linalg.det(array))
12+
13+
if __name__ == '__main__':
14+
np.set_printoptions(legacy="1.13")
15+
n = int(input())
16+
array = np.array([input().split() for _ in range(n)], float)
17+
print(np.linalg.det(array))

Numpy/MeanVarandStd.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
Title : Mean, Var, and Std
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7-
Updated : 24 June 2023
7+
Updated : 25 November 2024
88
Problem : https://www.hackerrank.com/challenges/np-mean-var-and-std/problem
99
"""
10+
import numpy as np
1011

11-
import numpy
1212

13-
n, m = map(int, input().split())
14-
ar = []
15-
for _ in range(n):
16-
tmp = list(map(int, input().split()))
17-
ar.append(tmp)
18-
np_ar = numpy.array(ar)
19-
print(numpy.mean(np_ar, axis=1))
20-
print(numpy.var(np_ar, axis=0))
21-
print(round(numpy.std(np_ar, axis=None), 11))
13+
if __name__ == '__main__':
14+
n, m = map(int, input().split())
15+
arr = np.array([input().split() for _ in range(n)], int)
16+
17+
print(np.mean(arr, axis=1))
18+
print(np.var(arr, axis=0))
19+
print(round(np.std(arr, axis=None), 11))

Numpy/MinandMax.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
Title : Min and Max
33
Subdomain : Numpy
44
Domain : Python
5-
Author : Ahmedur Rahman Shovon
5+
Author : Md Samshad Rahman
66
Created : 15 July 2016
7+
Updated : 25 November 2024
78
Problem : https://www.hackerrank.com/challenges/np-min-and-max/problem
89
"""
10+
import numpy as np
911

10-
import numpy
1112

12-
n, m = map(int, input().split())
13-
ar = []
14-
for _ in range(n):
15-
tmp = list(map(int, input().split()))
16-
ar.append(tmp)
17-
np_ar = numpy.array(ar)
18-
print(numpy.max(numpy.min(np_ar, axis=1)))
13+
if __name__ == '__main__':
14+
n, m = map(int, input().split())
15+
arr = np.array([input().split() for _ in range(n)], int)
16+
result = np.max(np.min(arr, axis=1))
17+
print(result)

0 commit comments

Comments
 (0)