Skip to content

Commit 9502cdc

Browse files
committed
HackerRank Python Tutorial Challenges Solutions
Problems can be found over the given link - https://www.hackerrank.com/domains/miscellaneous/python-tutorials
1 parent 38e06f4 commit 9502cdc

20 files changed

+373
-0
lines changed

Basic calculator.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
a = float(raw_input())
3+
b = float(raw_input())
4+
print "%0.2f"%(a+b)
5+
print "%0.2f"%(a-b)
6+
print "%0.2f"%(a*b)
7+
print "%0.2f"%(a/b)
8+
print "%0.2f"%(a//b)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import math
5+
6+
class ComplexNo(object):
7+
def __init__(self, real, imaginary):
8+
self.real = real
9+
self.imaginary = imaginary
10+
11+
def __add__(self, no):
12+
real = self.real + no.real
13+
imaginary = self.imaginary + no.imaginary
14+
return ComplexNo(real, imaginary)
15+
16+
def __sub__(self, no):
17+
real = self.real - no.real
18+
imaginary = self.imaginary - no.imaginary
19+
return ComplexNo(real, imaginary)
20+
21+
def __mul__(self, no):
22+
real = self.real * no.real - self.imaginary * no.imaginary
23+
imaginary = self.real * no.imaginary + self.imaginary * no.real
24+
return ComplexNo(real, imaginary)
25+
26+
def __div__(self, no):
27+
x = float(no.real ** 2 + no.imaginary ** 2)
28+
y = self * ComplexNo(no.real, -no.imaginary)
29+
real = y.real / x
30+
imaginary = y.imaginary / x
31+
return ComplexNo(real, imaginary)
32+
33+
def mod(self):
34+
real = math.sqrt(self.real ** 2 + self.imaginary ** 2)
35+
return ComplexNo(real, 0)
36+
# can also use __repr__ in place of __str__
37+
def __str__(self):
38+
if self.imaginary == 0:
39+
result = "%.2f" % (self.real)
40+
elif self.real == 0:
41+
result = "%.2fi" % (self.imaginary)
42+
elif self.imaginary > 0:
43+
result = "%.2f + %.2fi" % (self.real, self.imaginary)
44+
else:
45+
result = "%.2f - %.2fi" % (self.real, abs(self.imaginary))
46+
return result
47+
48+
49+
C = map(float, raw_input().split())
50+
D = map(float, raw_input().split())
51+
x = ComplexNo(*C)
52+
y = ComplexNo(*D)
53+
final = [x+y, x-y, x*y, x/y, x.mod(), y.mod()]
54+
print '\n'.join(map(str, final))

Class 2 - Find the torsional angle.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import math
5+
6+
class Points(object):
7+
def __init__(self, x, y, z):
8+
self.x = x
9+
self.y = y
10+
self.z = z
11+
12+
def __sub__(self, no):
13+
x = self.x - no.x
14+
y = self.y - no.y
15+
z = self.z - no.z
16+
return Points(x, y, z)
17+
18+
def dot(self, no):
19+
x = self.x * no.x
20+
y = self.y * no.y
21+
z = self.z * no.z
22+
return x + y + z
23+
24+
def cross(self, no):
25+
x = self.y * no.z - self.z * no.y
26+
y = self.z * no.x - self.x * no.z
27+
z = self.x * no.y - self.y * no.x
28+
return Points(x, y, z)
29+
30+
def absolute_scale(self):
31+
return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), .5)
32+
33+
34+
def solve(A, B, C, D):
35+
A, B, C, D = Points(*A), Points(*B), Points(*C), Points(*D)
36+
X = (B - A).cross(C - B)
37+
Y = (C - B).cross(D - C)
38+
angle = math.acos(X.dot(Y) / (X.absolute_scale() * Y.absolute_scale()))
39+
print "%.2f" % math.degrees(angle)
40+
41+
points = list()
42+
for i in range(4):
43+
a = map(float, raw_input().split())
44+
points.append(a)
45+
solve(*points)

Decorators 2 - Name directory.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
from operator import itemgetter
5+
from itertools import groupby
6+
7+
N = int(raw_input())
8+
lst = list()
9+
for i in range(N):
10+
a = raw_input()
11+
lst.append(a.split())
12+
13+
lst.sort(key=itemgetter(2))
14+
15+
for elt, items in groupby(lst, itemgetter(2)):
16+
for i in items:
17+
if i[3] == 'M':
18+
print "Mr.",i[0],i[1]
19+
else:
20+
print "Ms.",i[0],i[1]

Find a string.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
s1 = str(raw_input())
4+
s2 = str(raw_input())
5+
6+
# print s1.count(s2) -- for non-overlapping sequences
7+
# Below is code for overlapping sequences
8+
cnt = 0
9+
for i in range(len(s1)):
10+
if s1[i:].startswith(s2):
11+
cnt += 1
12+
#i += x
13+
print cnt
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
N = int(raw_input())
4+
x = raw_input()
5+
a1 = x.split()
6+
A = list(map(int, a1))
7+
x = max(A)
8+
k1 = list()
9+
for i in range(len(A)):
10+
if x != A[i]:
11+
k1.append(A[i])
12+
y = max(k1)
13+
print y

Finding the percentage.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
N = int(raw_input())
4+
dc = dict()
5+
lst1 = list()
6+
for i in range(N):
7+
a = raw_input()
8+
lst1 = a.split()
9+
d = lst1[0]
10+
lst1.remove(lst1[0])
11+
newlst1 = list(map(float, lst1))
12+
dc[d] = newlst1
13+
name = raw_input()
14+
total = 0
15+
if name in dc:
16+
marks = dc[name]
17+
no = len(marks)
18+
for num in marks:
19+
total += num
20+
avg = total / no
21+
print "%.2f" % avg

Info.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Problems can be found over:
3+
4+
https://www.hackerrank.com/domains/miscellaneous/python-tutorials

Interchange two numbers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
a = raw_input()
3+
b = raw_input()
4+
x = a,
5+
y = b,
6+
z = y
7+
y = x
8+
x = z
9+
print x[0]
10+
print y[0]

List Comprehensions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
3+
#Input
4+
x = int(raw_input())
5+
y = int(raw_input())
6+
z = int(raw_input())
7+
N = int(raw_input())
8+
#Solve
9+
arr = [[X, Y, Z] for X in range(x+1) for Y in range(y+1) for Z in range(z+1) if X + Y + Z != N]
10+
#Output
11+
print(arr)

0 commit comments

Comments
 (0)