Skip to content

Refactored codes. Domain: Python | Subdomain: NumPy #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions Collections/CollectionsOrderedDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
Title : Collections.OrderedDict()
Subdomain : Collections
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 08 February 2023
Updated : 26 November 2024
Problem : https://www.hackerrank.com/challenges/py-collections-ordereddict/problem
"""

import collections
import re
from collections import OrderedDict

n = int(input())
item_od = collections.OrderedDict()
for _ in range(n):
record_list = re.split(r"(\d+)$", input().strip())
item_name = record_list[0]
item_price = int(record_list[1])
if item_name not in item_od:
item_od[item_name] = item_price
else:
item_od[item_name] = item_od[item_name] + item_price
for i in item_od:
print(f"{i}{item_od[i]}")

if __name__ == '__main__':
ordered_dictionary = OrderedDict()

n = int(input())

for _ in range(n):
s = list(input().split())
price = int(s[len(s) - 1])
name = ' '.join(s[:len(s) - 1])

if ordered_dictionary.get(name):
ordered_dictionary[name] += price
else:
ordered_dictionary[name] = price

for key, value in ordered_dictionary.items():
print(key, value)
32 changes: 21 additions & 11 deletions Collections/collectionsCounter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@
Title : collections.Counter()
Subdomain : Collections
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 26 November 2024
Problem : https://www.hackerrank.com/challenges/collections-counter/problem
"""
import collections


if __name__ == '__main__':
total_shoes = int(input())
shoe_sizes = list(map(int, input().split()))
inventory = collections.Counter(shoe_sizes)

total_customers = int(input())
total_revenue = 0

for _ in range(total_customers):
size, price = map(int, input().split())
if inventory.get(size) and inventory[size] > 0:
total_revenue += price
inventory[size] -= 1

print(total_revenue)


x = int(input())
shoe_size = list(map(int, input().split()))
n = int(input())
sell = 0
for _ in range(n):
s, p = map(int, input().split())
if s in shoe_size:
sell = sell + p
shoe_size.remove(s)
print(sell)
34 changes: 15 additions & 19 deletions Numpy/ArrayMathematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,23 @@
Title : Array Mathematics
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-array-mathematics/problem
"""
import numpy as np

import numpy
if __name__ == '__main__':
n, m = map(int, input().split())

n, m = map(int, input().split())
ar1 = []
ar2 = []
for _ in range(n):
tmp = list(map(int, input().split()))
ar1.append(tmp)
for _ in range(n):
tmp = list(map(int, input().split()))
ar2.append(tmp)
np_ar1 = numpy.array(ar1)
np_ar2 = numpy.array(ar2)
print(np_ar1 + np_ar2)
print(np_ar1 - np_ar2)
print(np_ar1 * np_ar2)
print(np_ar1 // np_ar2)
print(np_ar1 % np_ar2)
print(np_ar1**np_ar2)

ar1 = np.array([input().split() for _ in range(n)], int)
ar2 = np.array([input().split() for _ in range(n)], int)

print(ar1 + ar2)
print(ar1 - ar2)
print(ar1 * ar2)
print(ar1 // ar2)
print(ar1 % ar2)
print(ar1 ** ar2)
13 changes: 9 additions & 4 deletions Numpy/Arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
Title : Arrays
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-arrays/problem
"""

import numpy

ar = list(map(float, input().split()))
np_ar = numpy.array(ar, float)
print(np_ar[::-1])
def arrays(arr):
np_ar = numpy.array(arr, float)
return np_ar[::-1]

arr = input().strip().split(' ')
result = arrays(arr)
print(result)
31 changes: 17 additions & 14 deletions Numpy/Concatenate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
Title : Concatenate
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-concatenate/problem
"""

import numpy
import numpy as np

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

ar1 = []
ar2 = []
for _ in range(n):
tmp = list(map(int, input().split()))
ar1.append(tmp)
for _ in range(m):
tmp = list(map(int, input().split()))
ar2.append(tmp)
np_ar1 = numpy.array(ar1)
np_ar2 = numpy.array(ar2)
print(numpy.concatenate((np_ar1, np_ar2), axis=0))
if __name__ == '__main__':
n, m, p = map(int, input().split())

arr1 = []
arr2 = []
for _ in range(n):
tmp = list(map(int, input().split()))
arr1.append(tmp)
for _ in range(m):
tmp = list(map(int, input().split()))
arr2.append(tmp)
np_arr1 = np.array(arr1)
np_arr2 = np.array(arr2)
print(np.concatenate((np_arr1, np_arr2), axis=0))
23 changes: 9 additions & 14 deletions Numpy/DotandCross.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
Title : Dot and Cross
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-dot-and-cross/problem
"""
import numpy as np

import numpy
if __name__ == '__main__':
n = int(input())

n = int(input())
ar1 = []
ar2 = []
for _ in range(n):
tmp = list(map(int, input().split()))
ar1.append(tmp)
np_ar1 = numpy.array(ar1)
for _ in range(n):
tmp = list(map(int, input().split()))
ar2.append(tmp)
np_ar2 = numpy.array(ar2)
print(numpy.dot(np_ar1, np_ar2))
arr1 = np.array([input().split() for _ in range(n)], int)
arr2 = np.array([input().split() for _ in range(n)], int)

print(np.dot(arr1, arr2))
12 changes: 7 additions & 5 deletions Numpy/EyeandIdentity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
Title : Eye and Identity
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 17 March 2021
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-eye-and-identity/problem
"""

import numpy as np

np.set_printoptions(legacy="1.13")
n, m = map(int, input().split())
print(np.eye(n, m, k=0))

if __name__ == '__main__':
np.set_printoptions(legacy="1.13")
n, m = map(int, input().split())
print(np.eye(n, m, k=0))
20 changes: 11 additions & 9 deletions Numpy/FloorCeilandRint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
Title : Floor, Ceil and Rint
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 17 March 2021
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/floor-ceil-and-rint/problem
"""

import numpy as np

np.set_printoptions(legacy="1.13")
A = np.array(input().split(), float)
print(np.floor(A))
print(np.ceil(A))
print(np.rint(A))

# Alternate solution
# print(*(f(A) for f in [np.floor, np.ceil, np.rint]), sep='\n')
if __name__ == '__main__':
np.set_printoptions(legacy="1.13")
arr = np.array(input().split(), float)
print(np.floor(arr))
print(np.ceil(arr))
print(np.rint(arr))

# Alternate solution
# print(*(f(A) for f in [np.floor, np.ceil, np.rint]), sep='\n')
15 changes: 9 additions & 6 deletions Numpy/InnerandOuter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
Title : Inner and Outer
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-inner-and-outer/problem
"""
import numpy as np

import numpy

np_ar1 = numpy.array(list(map(int, input().split())))
np_ar2 = numpy.array(list(map(int, input().split())))
print(numpy.inner(np_ar1, np_ar2))
print(numpy.outer(np_ar1, np_ar2))
if __name__ == '__main__':
arr1 = np.array(list(map(int, input().split())))
arr2 = np.array(list(map(int, input().split())))

print(np.inner(arr1, arr2))
print(np.outer(arr1, arr2))
15 changes: 8 additions & 7 deletions Numpy/LinearAlgebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
Title : Linear Algebra
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 3 April 2021
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-linear-algebra/problem
"""

import numpy as np

np.set_printoptions(legacy="1.13")
n = int(input())
array = np.array([input().split() for _ in range(n)], float)
print(np.linalg.det(array))

if __name__ == '__main__':
np.set_printoptions(legacy="1.13")
n = int(input())
array = np.array([input().split() for _ in range(n)], float)
print(np.linalg.det(array))
22 changes: 10 additions & 12 deletions Numpy/MeanVarandStd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
Title : Mean, Var, and Std
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 24 June 2023
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-mean-var-and-std/problem
"""
import numpy as np

import numpy

n, m = map(int, input().split())
ar = []
for _ in range(n):
tmp = list(map(int, input().split()))
ar.append(tmp)
np_ar = numpy.array(ar)
print(numpy.mean(np_ar, axis=1))
print(numpy.var(np_ar, axis=0))
print(round(numpy.std(np_ar, axis=None), 11))
if __name__ == '__main__':
n, m = map(int, input().split())
arr = np.array([input().split() for _ in range(n)], int)

print(np.mean(arr, axis=1))
print(np.var(arr, axis=0))
print(round(np.std(arr, axis=None), 11))
17 changes: 8 additions & 9 deletions Numpy/MinandMax.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
Title : Min and Max
Subdomain : Numpy
Domain : Python
Author : Ahmedur Rahman Shovon
Author : Md Samshad Rahman
Created : 15 July 2016
Updated : 25 November 2024
Problem : https://www.hackerrank.com/challenges/np-min-and-max/problem
"""
import numpy as np

import numpy

n, m = map(int, input().split())
ar = []
for _ in range(n):
tmp = list(map(int, input().split()))
ar.append(tmp)
np_ar = numpy.array(ar)
print(numpy.max(numpy.min(np_ar, axis=1)))
if __name__ == '__main__':
n, m = map(int, input().split())
arr = np.array([input().split() for _ in range(n)], int)
result = np.max(np.min(arr, axis=1))
print(result)
Loading
Loading