Skip to content

Commit

Permalink
Merge pull request #52 from Kushal997-das/master
Browse files Browse the repository at this point in the history
added hackerrank (python) all problems.
  • Loading branch information
manishbisht authored Oct 1, 2020
2 parents 624e732 + 654fdb1 commit 547859d
Show file tree
Hide file tree
Showing 119 changed files with 1,646 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("Hello, World!")

24 changes: 24 additions & 0 deletions Hackerrank_python/1.introduction/02.Python If-Else.py
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")

8 changes: 8 additions & 0 deletions Hackerrank_python/1.introduction/03.Arithmetic Operators.py
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)


7 changes: 7 additions & 0 deletions Hackerrank_python/1.introduction/04.Python_ Division.py
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)


6 changes: 6 additions & 0 deletions Hackerrank_python/1.introduction/05.Loops.py
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)


16 changes: 16 additions & 0 deletions Hackerrank_python/1.introduction/06.Write a function.py
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())

6 changes: 6 additions & 0 deletions Hackerrank_python/1.introduction/07.Print Function.py
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='')


8 changes: 8 additions & 0 deletions Hackerrank_python/10.Built-Ins/69.Zipped!.py
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))
6 changes: 6 additions & 0 deletions Hackerrank_python/10.Built-Ins/70.Input().py
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")
2 changes: 2 additions & 0 deletions Hackerrank_python/10.Built-Ins/71.Python Evaluation.py
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())
13 changes: 13 additions & 0 deletions Hackerrank_python/10.Built-Ins/72.Athlete Sort.py
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)
6 changes: 6 additions & 0 deletions Hackerrank_python/10.Built-Ins/73.Any or All.py
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]))

18 changes: 18 additions & 0 deletions Hackerrank_python/10.Built-Ins/74.ginortS.py
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="")
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')
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))

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))))
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 Hackerrank_python/12.Python Functionals/79.Reduce Function.py
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)
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)))
3 changes: 3 additions & 0 deletions Hackerrank_python/13.Regex and Parsing/81.Re.split().py
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())))
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)

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 Hackerrank_python/13.Regex and Parsing/84.Re.start() & Re.end().py
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 Hackerrank_python/13.Regex and Parsing/85.Regex Substitution.py
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)
'''

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()))))
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}$
'''
Loading

0 comments on commit 547859d

Please sign in to comment.