Skip to content

Commit 9502cdc

Browse files
committedJan 20, 2015
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

+8
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)
+54
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

+45
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

+20
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

+13
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
+13
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

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

+4
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

+10
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

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

‎Map and Lambda Function.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
from itertools import islice # For making an iterator that returns selected elements from the iterable.
4+
n = abs(int(raw_input()))
5+
6+
def fib(a=0, b=1):
7+
yield a
8+
while True:
9+
yield b
10+
a, b = b, a + b
11+
12+
lst = list(islice(fib(), n))
13+
cube = lambda x: x * x * x
14+
print list(map(cube, lst))

‎Nested list.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
3+
N = int(raw_input())
4+
final = list()
5+
for i in range(N):
6+
lst = list()
7+
name = str(raw_input())
8+
marks = float(raw_input())
9+
lst.append(name)
10+
lst.append(marks)
11+
final.append(lst)
12+
13+
# print final
14+
# print len(final)
15+
k = list()
16+
for i in range(len(final)):
17+
k.append(final[i][1])
18+
# print k
19+
x = min(k)
20+
k1 = list()
21+
for i in range(len(k)):
22+
if x != k[i]:
23+
k1.append(k[i])
24+
y = min(k1)
25+
# print x
26+
student = list()
27+
for i in range(len(final)):
28+
if y == final[i][1]:
29+
student.append(final[i][0])
30+
student.sort()
31+
for i in range(len(student)):
32+
print student[i]
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import re
5+
N = int(raw_input())
6+
number = [raw_input() for i in range(N)]
7+
check = lambda no:len(re.findall("^[7-9]\d{9}$",no))
8+
match = map(check, number)
9+
10+
for i in match:
11+
if i == 1:
12+
print "YES"
13+
else:
14+
print "NO"

‎Regex 2 - Validate a Roman Number.py

+10
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+
# Author : Vineet
3+
4+
import re
5+
pattern = '^(?=[MDCLXVI])M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
6+
roman = str(raw_input())
7+
if re.search(pattern, roman):
8+
print 'True'
9+
else:
10+
print 'False'

‎Sets - Symmetric Difference.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
#-------------------------------------------------------------------------------
3+
# Name: module1
4+
# Purpose:
5+
#
6+
# Author: Vineet
7+
#
8+
# Created: 05/12/2014
9+
# Copyright: (c) Vineet 2014
10+
# Licence: <your licence>
11+
#-------------------------------------------------------------------------------
12+
13+
M = raw_input()
14+
a = raw_input()
15+
lst1 = a.split()
16+
newlst1 = list(map(int, lst1))
17+
set1 = set(newlst1)
18+
N = raw_input()
19+
b = raw_input()
20+
lst2 = b.split()
21+
newlst2 = list(map(int, lst2))
22+
set2 = set(newlst2)
23+
x = set1.difference(set2)
24+
#print x
25+
y = set2.difference(set1)
26+
#print y
27+
z = set()
28+
z = x.union(y)
29+
#print z
30+
lst = list(z)
31+
lst = sorted(lst)
32+
for i in range(len(lst)):
33+
print lst[i]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
number = list()
5+
N = int(raw_input())
6+
for i in range(N):
7+
number.append(str(raw_input()))
8+
9+
def mobile(function):
10+
def input(number):
11+
return sorted([function(i) for i in number])
12+
return input
13+
14+
@mobile
15+
def standardize(number):
16+
return "+91" + " " + number[-10:-5] + " " + number[-5:]
17+
18+
print '\n'.join(standardize(number))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import re
5+
6+
pattern = "^[a-zA-Z0-9-_]+@[a-zA-Z0-9]+\.[a-z]{1,3}$"
7+
8+
N = int(raw_input())
9+
email = [ raw_input() for i in range(N) ]
10+
valid_email = list()
11+
for i in email:
12+
if re.match(pattern,i):
13+
valid_email.append(i)
14+
15+
print sorted(valid_email)

‎Whats your name!.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
fn = raw_input()
3+
ln = raw_input()
4+
print "Hello %s %s! You just delved into python."%(fn,ln)

‎XML 1 - Find the score.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import xml.etree.ElementTree as etree
5+
6+
N = int(raw_input())
7+
xml = ""
8+
for i in range(N):
9+
xml += str(raw_input())
10+
11+
tree = etree.ElementTree(etree.fromstring(xml))
12+
#root = tree.getroot()
13+
count = 0
14+
#count = len(root.attrib)
15+
for element in tree.iter():
16+
count += len(element.attrib)
17+
print count

‎XML2 - Find the maximum depth.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
# Author : Vineet
3+
4+
import xml.etree.ElementTree as etree
5+
6+
def depth (root):
7+
return max([0] + [depth(child) + 1 for child in root])
8+
9+
N = int(raw_input())
10+
xml = ""
11+
for i in range(N):
12+
xml += str(raw_input())
13+
14+
tree = etree.ElementTree(etree.fromstring(xml))
15+
root = tree.getroot()
16+
17+
print depth(root)

0 commit comments

Comments
 (0)
Please sign in to comment.