From b40f953f25a9ed9b9c918191c5d50f713f09c406 Mon Sep 17 00:00:00 2001 From: JAPJEET SINGH <95476734+JAPJEET01@users.noreply.github.com> Date: Mon, 24 Oct 2022 11:10:52 +0530 Subject: [PATCH] HackerRank python solutions --- AnyorAll.py | 4 +++ AthleteSort.py | 14 ++++++++++ CompresstheString.py | 13 +++++++++ Findingthepercentage.py | 10 +++++++ FindtheSecondLargestNumber.py | 5 ++++ Input.py | 5 ++++ IterablesandIterators.py | 8 ++++++ ListComprehensions.py | 9 +++++++ Lists.py | 33 +++++++++++++++++++++++ MaximizeIt.py | 15 +++++++++++ NestedLists.py | 14 ++++++++++ PythonEvaluation.py | 2 ++ Tuples.py | 6 +++++ Zipped.py | 8 ++++++ ginortS.py | 4 +++ itertoolscombinations.py | 8 ++++++ itertoolscombinations_with_replacement.py | 7 +++++ itertoolspermutations.py | 6 +++++ itertoolsproduct.py | 7 +++++ 19 files changed, 178 insertions(+) create mode 100644 AnyorAll.py create mode 100644 AthleteSort.py create mode 100644 CompresstheString.py create mode 100644 Findingthepercentage.py create mode 100644 FindtheSecondLargestNumber.py create mode 100644 Input.py create mode 100644 IterablesandIterators.py create mode 100644 ListComprehensions.py create mode 100644 Lists.py create mode 100644 MaximizeIt.py create mode 100644 NestedLists.py create mode 100644 PythonEvaluation.py create mode 100644 Tuples.py create mode 100644 Zipped.py create mode 100644 ginortS.py create mode 100644 itertoolscombinations.py create mode 100644 itertoolscombinations_with_replacement.py create mode 100644 itertoolspermutations.py create mode 100644 itertoolsproduct.py diff --git a/AnyorAll.py b/AnyorAll.py new file mode 100644 index 0000000..593e37b --- /dev/null +++ b/AnyorAll.py @@ -0,0 +1,4 @@ + +n = input() +ar = input().split() +print(all([int(i)>0 for i in ar]) and any([i==i[::-1] for i in ar])) diff --git a/AthleteSort.py b/AthleteSort.py new file mode 100644 index 0000000..e8cc88a --- /dev/null +++ b/AthleteSort.py @@ -0,0 +1,14 @@ + + +if __name__ == '__main__': + n, m = map(int, input().split()) + + arr = [] + + for _ in range(n): + arr.append(list(map(int, input().rstrip().split()))) + + k = int(input()) + + for i in sorted(arr, key= lambda x: x[k]): + print(*i) diff --git a/CompresstheString.py b/CompresstheString.py new file mode 100644 index 0000000..58bfb44 --- /dev/null +++ b/CompresstheString.py @@ -0,0 +1,13 @@ + +import itertools +s = input().strip() +s_unique_element = list(set(s)) +group = [] +key = [] +for k,g in itertools.groupby(s): + group.append(list(g)) + key.append(k) +for i in range(len(group)): + group_length = len(group[i]) + k = int(key[i]) + print(tuple((group_length,k)),end=' ') diff --git a/Findingthepercentage.py b/Findingthepercentage.py new file mode 100644 index 0000000..4ceabdc --- /dev/null +++ b/Findingthepercentage.py @@ -0,0 +1,10 @@ +if __name__ == '__main__': + n = int(input()) + student_marks = {} + for _ in range(n): + name, *line = input().split() + scores = list(map(float, line)) + student_marks[name] = scores + query_name = input() + res = sum(student_marks[query_name]) / len(student_marks[name]) + print("{:.2f}".format(res)) diff --git a/FindtheSecondLargestNumber.py b/FindtheSecondLargestNumber.py new file mode 100644 index 0000000..4ed3a0e --- /dev/null +++ b/FindtheSecondLargestNumber.py @@ -0,0 +1,5 @@ + +if __name__ == '__main__': + n = int(input()) + arr = map(int, input().split()) + print(sorted(set(arr), reverse=True)[1]) diff --git a/Input.py b/Input.py new file mode 100644 index 0000000..e4a1b39 --- /dev/null +++ b/Input.py @@ -0,0 +1,5 @@ + +if __name__ == "__main__": + x, k = map(int, input().strip().split()) + equation = input().strip() + print(eval(equation) == k) diff --git a/IterablesandIterators.py b/IterablesandIterators.py new file mode 100644 index 0000000..71dc9f5 --- /dev/null +++ b/IterablesandIterators.py @@ -0,0 +1,8 @@ + +from itertools import combinations +n = int(input()) +ar = input().split() +k = int(input()) +comb_list = list(combinations(ar,k)) +a_list = [e for e in comb_list if 'a' in e] +print(len(a_list) / len(comb_list)) diff --git a/ListComprehensions.py b/ListComprehensions.py new file mode 100644 index 0000000..a37f8f0 --- /dev/null +++ b/ListComprehensions.py @@ -0,0 +1,9 @@ + +if __name__ == '__main__': + x = int(input()) + y = int(input()) + z = int(input()) + n = int(input()) + ar = [[i, j, k] for i in range(x + 1) for j in range(y + 1) for k in + range(z + 1) if i + j + k != n] + print(ar) diff --git a/Lists.py b/Lists.py new file mode 100644 index 0000000..8a17443 --- /dev/null +++ b/Lists.py @@ -0,0 +1,33 @@ + + +if __name__ == '__main__': + N = int(input()) + ar = [] + for i in range(0, N): + tmp_str = input() + tmp_str_ar = tmp_str.strip().split(" ") + cmd = tmp_str_ar[0] + if cmd == "print": + print(ar) + elif cmd == "sort": + ar.sort() + elif cmd == "reverse": + ar.reverse() + elif cmd == "pop": + ar.pop() + elif cmd == "count": + val = int(tmp_str_ar[1]) + ar.count(val) + elif cmd == "index": + val = int(tmp_str_ar[1]) + ar.index(val) + elif cmd == "remove": + val = int(tmp_str_ar[1]) + ar.remove(val) + elif cmd == "append": + val = int(tmp_str_ar[1]) + ar.append(val) + elif cmd == "insert": + pos = int(tmp_str_ar[1]) + val = int(tmp_str_ar[2]) + ar.insert(pos, val) diff --git a/MaximizeIt.py b/MaximizeIt.py new file mode 100644 index 0000000..686ef44 --- /dev/null +++ b/MaximizeIt.py @@ -0,0 +1,15 @@ + +import itertools + +k, m = map(int,input().split()) + +main_ar = [] +for i in range(k): + ar = list(map(int,input().split())) + main_ar.append(ar[1:]) + +all_combination = itertools.product(*main_ar) +result = 0 +for single_combination in all_combination: + result = max(sum([x*x for x in single_combination])%m,result) +print(result) diff --git a/NestedLists.py b/NestedLists.py new file mode 100644 index 0000000..9bb3039 --- /dev/null +++ b/NestedLists.py @@ -0,0 +1,14 @@ + +if __name__ == '__main__': + students = [] + scores = [] + for _ in range(int(input())): + name = input() + score = float(input()) + student = [name, score] + students.append(student) + scores.append(score) + second_min_score = sorted(set(scores))[1] + student_names = sorted( + [student[0] for student in students if student[1] == second_min_score]) + print("\n".join(student_names)) diff --git a/PythonEvaluation.py b/PythonEvaluation.py new file mode 100644 index 0000000..96e7d4e --- /dev/null +++ b/PythonEvaluation.py @@ -0,0 +1,2 @@ + +eval(input()) diff --git a/Tuples.py b/Tuples.py new file mode 100644 index 0000000..9a5ddb5 --- /dev/null +++ b/Tuples.py @@ -0,0 +1,6 @@ + +if __name__ == '__main__': + n = int(input()) + integer_list = map(int, input().split()) + t = tuple(integer_list) + print(hash(t)) diff --git a/Zipped.py b/Zipped.py new file mode 100644 index 0000000..f92b63c --- /dev/null +++ b/Zipped.py @@ -0,0 +1,8 @@ + + +N, X = map(int, input().split()) +scores = [] +for _ in range(X): + scores.append(list(map(float, input().split()))) +for i in zip(*scores): + print(sum(i)/len(i)) diff --git a/ginortS.py b/ginortS.py new file mode 100644 index 0000000..2dfc323 --- /dev/null +++ b/ginortS.py @@ -0,0 +1,4 @@ + + +s = input() +print(''.join(sorted(s, key=lambda x: (x.isdigit() and int(x)%2==0, x.isdigit(), x.isupper(), x.islower(), x)))) diff --git a/itertoolscombinations.py b/itertoolscombinations.py new file mode 100644 index 0000000..cb20ae1 --- /dev/null +++ b/itertoolscombinations.py @@ -0,0 +1,8 @@ + +from itertools import * +s,n = input().split() +n = int(n) + 1 +s = sorted(s) +for i in range(1,n): + for j in combinations(s,i): + print(''.join(j)) diff --git a/itertoolscombinations_with_replacement.py b/itertoolscombinations_with_replacement.py new file mode 100644 index 0000000..85cbeb8 --- /dev/null +++ b/itertoolscombinations_with_replacement.py @@ -0,0 +1,7 @@ + +from itertools import * +s,n = input().split() +n = int(n) +s = sorted(s) +for j in combinations_with_replacement(s,n): + print(''.join(j)) diff --git a/itertoolspermutations.py b/itertoolspermutations.py new file mode 100644 index 0000000..b1c5e1b --- /dev/null +++ b/itertoolspermutations.py @@ -0,0 +1,6 @@ + +import itertools +s,n = list(map(str,input().split(' '))) +s = sorted(s) +for p in list(itertools.permutations(s,int(n))): + print(''.join(p)) diff --git a/itertoolsproduct.py b/itertoolsproduct.py new file mode 100644 index 0000000..e9e3ea5 --- /dev/null +++ b/itertoolsproduct.py @@ -0,0 +1,7 @@ + +import itertools +ar1 = list(map(int,input().split())) +ar2 = list(map(int,input().split())) +cross = list(itertools.product(ar1,ar2)) +for i in cross: + print(i,end=' ')