Skip to content

Commit 547859d

Browse files
authored
Merge pull request manishbisht#52 from Kushal997-das/master
added hackerrank (python) all problems.
2 parents 624e732 + 654fdb1 commit 547859d

File tree

119 files changed

+1646
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+1646
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print("Hello, World!")
2+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/python3
2+
3+
4+
import math
5+
import os
6+
import random
7+
import re
8+
import sys
9+
10+
11+
12+
if __name__ == '__main__':
13+
n = int(input().strip())
14+
#own code
15+
if n%2==1:
16+
print ("Weird")
17+
else:
18+
if n>=2 and n<=5:
19+
print ("Not Weird")
20+
elif n>=6 and n<=20:
21+
print ("Weird")
22+
elif n>20:
23+
print ("Not Weird")
24+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if __name__ == '__main__':
2+
a = int(input())
3+
b = int(input())
4+
print (a+b)
5+
print (a-b)
6+
print (a*b)
7+
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
if __name__ == '__main__':
2+
a = int(input())
3+
b = int(input())
4+
print(a//b)
5+
print(a/b)
6+
7+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if __name__ == '__main__':
2+
n = int(input())
3+
for i in range(n):
4+
print(i**2)
5+
6+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def is_leap(year):
2+
leap = False
3+
4+
# Write your logic here
5+
6+
if year%400==0:
7+
leap=True
8+
elif year%100==0:
9+
leap=False
10+
elif year%4==0:
11+
leap=True
12+
13+
return leap
14+
15+
year = int(input())
16+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if __name__ == '__main__':
2+
n = int(input())
3+
for i in range(1,n+1):
4+
print(i,end='')
5+
6+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
n,x = map(int,input().split())
2+
3+
sheet = []
4+
for _ in range(x):
5+
sheet.append( map(float, input().split()) )
6+
7+
for i in zip(*sheet):
8+
print( sum(i)/len(i))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
x,k=map(int,input().split())
2+
fx=eval(input())
3+
if fx==k:
4+
print("True")
5+
else:
6+
print("False")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
eval(input())
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import math
2+
import os
3+
import random
4+
import re
5+
import sys
6+
7+
N, M = map(int, input().split())
8+
9+
rows = [input()for _ in range(N)]
10+
K = int(input())
11+
12+
for row in sorted(rows, key=lambda row: int(row.split()[K])):
13+
print(row)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
N=int(input())
3+
x=input().split()
4+
#N,n = int(raw_input()),raw_input().split()
5+
print (all([int(i)>0 for i in x]) and any([j == j[::-1] for j in x]))
6+
Lines changed: 18 additions & 0 deletions
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+
s=input()
3+
lower,upper,odd,even=[],[],[],[]
4+
for i in s:
5+
if(i.islower()):
6+
lower.append(ord(i))
7+
elif(i.isupper()):
8+
upper.append(ord(i))
9+
10+
elif(int(i)%2==1):
11+
odd.append(ord(i))
12+
else:
13+
even.append(ord(i))
14+
lower.sort(),upper.sort(),odd.sort(),even.sort()
15+
marge=lower+upper+odd+even
16+
17+
for i in marge:
18+
print(chr(i),end="")
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import math
2+
class Complex(object):
3+
def __init__(self, real, imaginary):
4+
self.real=real
5+
self.imaginary=imaginary
6+
7+
def __add__(self, no):
8+
real=self.real+no.real
9+
imaginary=self.imaginary+no.imaginary
10+
return(Complex(real,imaginary))
11+
12+
13+
def __sub__(self, no):
14+
real=self.real-no.real
15+
imaginary=self.imaginary-no.imaginary
16+
17+
return(Complex(real,imaginary))
18+
19+
def __mul__(self, no):
20+
real=self.real*no.real-self.imaginary*no.imaginary
21+
imaginary=self.real*no.imaginary+self.imaginary*no.real
22+
return(Complex(real,imaginary))
23+
24+
25+
def __truediv__(self, no):
26+
x=float(no.real**2+no.imaginary**2)
27+
y=self*Complex(no.real,-no.imaginary)
28+
real=y.real/x
29+
imaginary=y.imaginary/x
30+
return(Complex(real,imaginary))
31+
32+
33+
def mod(self):
34+
real=math.sqrt(self.real**2+self.imaginary**2)
35+
return(Complex(real,0))
36+
37+
38+
def __str__(self):
39+
if self.imaginary == 0:
40+
result = "%.2f+0.00i" % (self.real)
41+
elif self.real == 0:
42+
if self.imaginary >= 0:
43+
result = "0.00+%.2fi" % (self.imaginary)
44+
else:
45+
result = "0.00-%.2fi" % (abs(self.imaginary))
46+
elif self.imaginary > 0:
47+
result = "%.2f+%.2fi" % (self.real, self.imaginary)
48+
else:
49+
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary))
50+
return result
51+
52+
53+
if __name__ == '__main__':
54+
c = map(float, input().split())
55+
d = map(float, input().split())
56+
x = Complex(*c)
57+
y = Complex(*d)
58+
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n')
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import math
2+
3+
class Points(object):
4+
def __init__(self, x, y, z):
5+
self.x=x
6+
self.y=y
7+
self.z=z
8+
9+
def __sub__(self, no):
10+
x=self.x-no.x
11+
y=self.y-no.y
12+
z=self.z-no.z
13+
return Points(x,y,z)
14+
15+
def dot(self, no):
16+
x=self.x*no.x
17+
y=self.y*no.y
18+
z=self.z*no.z
19+
return (x+y+z)
20+
21+
22+
23+
def cross(self, no):
24+
x=self.y*no.z-self.z*no.y
25+
y=self.x*no.z-self.z*no.x
26+
z=self.x*no.y-self.y*no.x
27+
return Points(x,y,z)
28+
29+
def absolute(self):
30+
return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5)
31+
32+
if __name__ == '__main__':
33+
points = list()
34+
for i in range(4):
35+
a = list(map(float, input().split()))
36+
points.append(a)
37+
38+
a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3])
39+
x = (b - a).cross(c - b)
40+
y = (c - b).cross(d - c)
41+
angle = math.acos(x.dot(y) / (x.absolute() * y.absolute()))
42+
43+
print("%.2f" % math.degrees(angle))
44+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cube = lambda x: x**3# complete the lambda function
2+
def fibonacci(n):
3+
# return a list of fibonacci numbers
4+
lis = [0,1]
5+
for i in range(2,n):
6+
lis.append(lis[i-2] + lis[i-1])
7+
return(lis[0:n]) # complete the lambda function
8+
9+
10+
if __name__ == '__main__':
11+
n = int(input())
12+
print(list(map(cube, fibonacci(n))))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def fun(s):
2+
try:
3+
username,url=s.split('@')
4+
website,extension=url.split('.')
5+
except ValueError:
6+
return False
7+
if username.replace('-','').replace('_','').isalnum() is False:
8+
return False
9+
elif website.isalnum() is False:
10+
return False
11+
elif len(extension)>3:
12+
return False
13+
else:
14+
return True
15+
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from fractions import Fraction
2+
from functools import reduce
3+
4+
def product(fracs):
5+
t =reduce(lambda numerator,denominator:numerator*denominator,fracs)
6+
# complete this line with a reduce statement
7+
return t.numerator, t.denominator
8+
9+
if __name__ == '__main__':
10+
fracs = []
11+
for _ in range(int(input())):
12+
fracs.append(Fraction(*map(int, input().split())))
13+
result = product(fracs)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
import re
3+
for i in range(int(input())):
4+
x=input()
5+
#print(bool(re.match([r'^[-+]?[0-9]*.[0-9]+$'],input())))
6+
print(bool(re.match(r'^[-+]?[0-9]*[.][0-9]*$',x)))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
regex_pattern = r'[,.]'
2+
import re
3+
print("\n".join(re.split(regex_pattern, input())))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
import re
3+
m = re.search(r'([a-zA-Z0-9])\1', input())
4+
#print(m.group(1) if m else -1)
5+
if m:
6+
print(m.group(1))
7+
else:
8+
print(-1)
9+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUTi
2+
import re
3+
sub_string=re.findall(r'(?<=[qwrtypsdfghjklzxcvbnm])([aeiou]{2,})(?=[qwrtypsdfghjklzxcvbnm])',input(),re.I)
4+
'''
5+
if sub_string:
6+
for s in sub_string:
7+
print(s)
8+
else:
9+
print(-1)
10+
'''
11+
print('\n'.join(sub_string or ['-1']))
12+
#print('\n'.join(sub_string or ['-1']))
13+
14+
Lines changed: 32 additions & 0 deletions
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+
'''
4+
S = input()
5+
k = input()
6+
import re
7+
pattern = re.compile(k)
8+
r = pattern.search(S)
9+
if not r: print((-1, -1))
10+
while r:
11+
print ('({0}, {1})'.format(r.start(), r.end() - 1))
12+
r = pattern.search(S,r.start() + 1)
13+
'''
14+
import re
15+
s = input()
16+
k = input()
17+
index = 0
18+
19+
if re.search(k, s):
20+
while index+len(k) < len(s):
21+
22+
m = re.search(k, s[index:]) #begins search with new index
23+
24+
print("({0}, {1})".format(index+m.start(), index+m.end()-1))
25+
26+
27+
index += m.start() + 1
28+
# c+=1
29+
#print(index) #assign new index by +1
30+
#elif re.search(k, s)==None:
31+
else:
32+
print((-1,-1))
Lines changed: 18 additions & 0 deletions
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+
import re
3+
4+
for _ in range(int(input())):
5+
s = input()
6+
s = re.sub(r" \&\&(?= )", " and",s)
7+
s = re.sub(r" \|\|(?= )", " or", s)
8+
print(s)
9+
'''
10+
for _ in range(int(input())):
11+
line = input()
12+
13+
while ' && ' in line or ' || ' in line:
14+
line = line.replace(" && ", " and ").replace(" || ", " or ")
15+
16+
print(line)
17+
'''
18+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
regex_pattern = r"^M{0,3}(CD|CM|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$" # Do not delete 'r'.
2+
3+
import re
4+
print(str(bool(re.match(regex_pattern, input()))))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Enter your code here. Read input from STDIN. Print output to STDOUT
2+
import re
3+
for i in range(int(input())):
4+
x=input()
5+
6+
if re.match(r"[7,8,9]\d{9}$",x):
7+
print("YES")
8+
else:
9+
print("NO")
10+
'''
11+
[789]\d{9}$
12+
13+
The above statement is validating the number/count of digits is 10 or not also
14+
15+
one digit out of [7,8,9] ==> One digit count done \d means any number of digits but here we used \d{9} which means 9 digits
16+
17+
previously 1 digit and later on 9 digits ==> total 10 digits were counted and validated
18+
19+
if you want to make a phone number with 11 digit validation try below and check so that doubts will get resolved
20+
21+
[789]\d{10}$
22+
'''

0 commit comments

Comments
 (0)