-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Kushal997-das/master
added hackerrank (python) all problems.
- Loading branch information
Showing
119 changed files
with
1,646 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
Hackerrank_python/1.introduction/01.Say _Hello, World!_ With Python.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
print("Hello, World!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/python3 | ||
|
||
|
||
import math | ||
import os | ||
import random | ||
import re | ||
import sys | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
n = int(input().strip()) | ||
#own code | ||
if n%2==1: | ||
print ("Weird") | ||
else: | ||
if n>=2 and n<=5: | ||
print ("Not Weird") | ||
elif n>=6 and n<=20: | ||
print ("Weird") | ||
elif n>20: | ||
print ("Not Weird") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
if __name__ == '__main__': | ||
a = int(input()) | ||
b = int(input()) | ||
print (a+b) | ||
print (a-b) | ||
print (a*b) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
if __name__ == '__main__': | ||
a = int(input()) | ||
b = int(input()) | ||
print(a//b) | ||
print(a/b) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if __name__ == '__main__': | ||
n = int(input()) | ||
for i in range(n): | ||
print(i**2) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def is_leap(year): | ||
leap = False | ||
|
||
# Write your logic here | ||
|
||
if year%400==0: | ||
leap=True | ||
elif year%100==0: | ||
leap=False | ||
elif year%4==0: | ||
leap=True | ||
|
||
return leap | ||
|
||
year = int(input()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
if __name__ == '__main__': | ||
n = int(input()) | ||
for i in range(1,n+1): | ||
print(i,end='') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
n,x = map(int,input().split()) | ||
|
||
sheet = [] | ||
for _ in range(x): | ||
sheet.append( map(float, input().split()) ) | ||
|
||
for i in zip(*sheet): | ||
print( sum(i)/len(i)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
x,k=map(int,input().split()) | ||
fx=eval(input()) | ||
if fx==k: | ||
print("True") | ||
else: | ||
print("False") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
eval(input()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import math | ||
import os | ||
import random | ||
import re | ||
import sys | ||
|
||
N, M = map(int, input().split()) | ||
|
||
rows = [input()for _ in range(N)] | ||
K = int(input()) | ||
|
||
for row in sorted(rows, key=lambda row: int(row.split()[K])): | ||
print(row) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
N=int(input()) | ||
x=input().split() | ||
#N,n = int(raw_input()),raw_input().split() | ||
print (all([int(i)>0 for i in x]) and any([j == j[::-1] for j in x])) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
s=input() | ||
lower,upper,odd,even=[],[],[],[] | ||
for i in s: | ||
if(i.islower()): | ||
lower.append(ord(i)) | ||
elif(i.isupper()): | ||
upper.append(ord(i)) | ||
|
||
elif(int(i)%2==1): | ||
odd.append(ord(i)) | ||
else: | ||
even.append(ord(i)) | ||
lower.sort(),upper.sort(),odd.sort(),even.sort() | ||
marge=lower+upper+odd+even | ||
|
||
for i in marge: | ||
print(chr(i),end="") |
58 changes: 58 additions & 0 deletions
58
Hackerrank_python/11.Classes/75.Classes_ Dealing with Complex Numbers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import math | ||
class Complex(object): | ||
def __init__(self, real, imaginary): | ||
self.real=real | ||
self.imaginary=imaginary | ||
|
||
def __add__(self, no): | ||
real=self.real+no.real | ||
imaginary=self.imaginary+no.imaginary | ||
return(Complex(real,imaginary)) | ||
|
||
|
||
def __sub__(self, no): | ||
real=self.real-no.real | ||
imaginary=self.imaginary-no.imaginary | ||
|
||
return(Complex(real,imaginary)) | ||
|
||
def __mul__(self, no): | ||
real=self.real*no.real-self.imaginary*no.imaginary | ||
imaginary=self.real*no.imaginary+self.imaginary*no.real | ||
return(Complex(real,imaginary)) | ||
|
||
|
||
def __truediv__(self, no): | ||
x=float(no.real**2+no.imaginary**2) | ||
y=self*Complex(no.real,-no.imaginary) | ||
real=y.real/x | ||
imaginary=y.imaginary/x | ||
return(Complex(real,imaginary)) | ||
|
||
|
||
def mod(self): | ||
real=math.sqrt(self.real**2+self.imaginary**2) | ||
return(Complex(real,0)) | ||
|
||
|
||
def __str__(self): | ||
if self.imaginary == 0: | ||
result = "%.2f+0.00i" % (self.real) | ||
elif self.real == 0: | ||
if self.imaginary >= 0: | ||
result = "0.00+%.2fi" % (self.imaginary) | ||
else: | ||
result = "0.00-%.2fi" % (abs(self.imaginary)) | ||
elif self.imaginary > 0: | ||
result = "%.2f+%.2fi" % (self.real, self.imaginary) | ||
else: | ||
result = "%.2f-%.2fi" % (self.real, abs(self.imaginary)) | ||
return result | ||
|
||
|
||
if __name__ == '__main__': | ||
c = map(float, input().split()) | ||
d = map(float, input().split()) | ||
x = Complex(*c) | ||
y = Complex(*d) | ||
print(*map(str, [x+y, x-y, x*y, x/y, x.mod(), y.mod()]), sep='\n') |
44 changes: 44 additions & 0 deletions
44
Hackerrank_python/11.Classes/76.Class 2 - Find the Torsional Angle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import math | ||
|
||
class Points(object): | ||
def __init__(self, x, y, z): | ||
self.x=x | ||
self.y=y | ||
self.z=z | ||
|
||
def __sub__(self, no): | ||
x=self.x-no.x | ||
y=self.y-no.y | ||
z=self.z-no.z | ||
return Points(x,y,z) | ||
|
||
def dot(self, no): | ||
x=self.x*no.x | ||
y=self.y*no.y | ||
z=self.z*no.z | ||
return (x+y+z) | ||
|
||
|
||
|
||
def cross(self, no): | ||
x=self.y*no.z-self.z*no.y | ||
y=self.x*no.z-self.z*no.x | ||
z=self.x*no.y-self.y*no.x | ||
return Points(x,y,z) | ||
|
||
def absolute(self): | ||
return pow((self.x ** 2 + self.y ** 2 + self.z ** 2), 0.5) | ||
|
||
if __name__ == '__main__': | ||
points = list() | ||
for i in range(4): | ||
a = list(map(float, input().split())) | ||
points.append(a) | ||
|
||
a, b, c, d = Points(*points[0]), Points(*points[1]), Points(*points[2]), Points(*points[3]) | ||
x = (b - a).cross(c - b) | ||
y = (c - b).cross(d - c) | ||
angle = math.acos(x.dot(y) / (x.absolute() * y.absolute())) | ||
|
||
print("%.2f" % math.degrees(angle)) | ||
|
12 changes: 12 additions & 0 deletions
12
Hackerrank_python/12.Python Functionals/77.Map and Lambda Function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
cube = lambda x: x**3# complete the lambda function | ||
def fibonacci(n): | ||
# return a list of fibonacci numbers | ||
lis = [0,1] | ||
for i in range(2,n): | ||
lis.append(lis[i-2] + lis[i-1]) | ||
return(lis[0:n]) # complete the lambda function | ||
|
||
|
||
if __name__ == '__main__': | ||
n = int(input()) | ||
print(list(map(cube, fibonacci(n)))) |
16 changes: 16 additions & 0 deletions
16
Hackerrank_python/12.Python Functionals/78.Validating Email Addresses With a Filter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
def fun(s): | ||
try: | ||
username,url=s.split('@') | ||
website,extension=url.split('.') | ||
except ValueError: | ||
return False | ||
if username.replace('-','').replace('_','').isalnum() is False: | ||
return False | ||
elif website.isalnum() is False: | ||
return False | ||
elif len(extension)>3: | ||
return False | ||
else: | ||
return True | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
Hackerrank_python/12.Python Functionals/79.Reduce Function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from fractions import Fraction | ||
from functools import reduce | ||
|
||
def product(fracs): | ||
t =reduce(lambda numerator,denominator:numerator*denominator,fracs) | ||
# complete this line with a reduce statement | ||
return t.numerator, t.denominator | ||
|
||
if __name__ == '__main__': | ||
fracs = [] | ||
for _ in range(int(input())): | ||
fracs.append(Fraction(*map(int, input().split()))) | ||
result = product(fracs) |
6 changes: 6 additions & 0 deletions
6
Hackerrank_python/13.Regex and Parsing/80.Detect Floating Point Number.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
import re | ||
for i in range(int(input())): | ||
x=input() | ||
#print(bool(re.match([r'^[-+]?[0-9]*.[0-9]+$'],input()))) | ||
print(bool(re.match(r'^[-+]?[0-9]*[.][0-9]*$',x))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
regex_pattern = r'[,.]' | ||
import re | ||
print("\n".join(re.split(regex_pattern, input()))) |
9 changes: 9 additions & 0 deletions
9
Hackerrank_python/13.Regex and Parsing/82.Group(), Groups() & Groupdict().py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
import re | ||
m = re.search(r'([a-zA-Z0-9])\1', input()) | ||
#print(m.group(1) if m else -1) | ||
if m: | ||
print(m.group(1)) | ||
else: | ||
print(-1) | ||
|
14 changes: 14 additions & 0 deletions
14
Hackerrank_python/13.Regex and Parsing/83.Re.findall() & Re.finditer().py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUTi | ||
import re | ||
sub_string=re.findall(r'(?<=[qwrtypsdfghjklzxcvbnm])([aeiou]{2,})(?=[qwrtypsdfghjklzxcvbnm])',input(),re.I) | ||
''' | ||
if sub_string: | ||
for s in sub_string: | ||
print(s) | ||
else: | ||
print(-1) | ||
''' | ||
print('\n'.join(sub_string or ['-1'])) | ||
#print('\n'.join(sub_string or ['-1'])) | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
Hackerrank_python/13.Regex and Parsing/84.Re.start() & Re.end().py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
|
||
''' | ||
S = input() | ||
k = input() | ||
import re | ||
pattern = re.compile(k) | ||
r = pattern.search(S) | ||
if not r: print((-1, -1)) | ||
while r: | ||
print ('({0}, {1})'.format(r.start(), r.end() - 1)) | ||
r = pattern.search(S,r.start() + 1) | ||
''' | ||
import re | ||
s = input() | ||
k = input() | ||
index = 0 | ||
|
||
if re.search(k, s): | ||
while index+len(k) < len(s): | ||
|
||
m = re.search(k, s[index:]) #begins search with new index | ||
|
||
print("({0}, {1})".format(index+m.start(), index+m.end()-1)) | ||
|
||
|
||
index += m.start() + 1 | ||
# c+=1 | ||
#print(index) #assign new index by +1 | ||
#elif re.search(k, s)==None: | ||
else: | ||
print((-1,-1)) |
18 changes: 18 additions & 0 deletions
18
Hackerrank_python/13.Regex and Parsing/85.Regex Substitution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
import re | ||
|
||
for _ in range(int(input())): | ||
s = input() | ||
s = re.sub(r" \&\&(?= )", " and",s) | ||
s = re.sub(r" \|\|(?= )", " or", s) | ||
print(s) | ||
''' | ||
for _ in range(int(input())): | ||
line = input() | ||
while ' && ' in line or ' || ' in line: | ||
line = line.replace(" && ", " and ").replace(" || ", " or ") | ||
print(line) | ||
''' | ||
|
4 changes: 4 additions & 0 deletions
4
Hackerrank_python/13.Regex and Parsing/86.Validating Roman Numerals.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
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'. | ||
|
||
import re | ||
print(str(bool(re.match(regex_pattern, input())))) |
22 changes: 22 additions & 0 deletions
22
Hackerrank_python/13.Regex and Parsing/87.Validating phone numbers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Enter your code here. Read input from STDIN. Print output to STDOUT | ||
import re | ||
for i in range(int(input())): | ||
x=input() | ||
|
||
if re.match(r"[7,8,9]\d{9}$",x): | ||
print("YES") | ||
else: | ||
print("NO") | ||
''' | ||
[789]\d{9}$ | ||
The above statement is validating the number/count of digits is 10 or not also | ||
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 | ||
previously 1 digit and later on 9 digits ==> total 10 digits were counted and validated | ||
if you want to make a phone number with 11 digit validation try below and check so that doubts will get resolved | ||
[789]\d{10}$ | ||
''' |
Oops, something went wrong.