HACKER - RANK PROBLEM SOLUTION
UCER _PYTHON
{Coded by: Mohammad Saquib khan}
1. Find the number of Vowels,Consonants,Digits
Source Code:
str=input()
def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i'
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
elif (ch >= '0' and ch <= '9'):
digit +=1
print(vowels)
print(consonant)
print(digit)
countCharacterType(str)
2.Pyramid Star pattern 1
Source Code:
def full_pyramid(rows):
for i in range(rows):
print(' '*(rows-i-1) + '*'*(2*i+1))
n=int(input())
full_pyramid(n)
3. Mirrored Right triangle star pattern
Source code:
k=0
j=0
n=int(input())
for i in range(1,n+1):
for j in range(0,n-i):
print("",end=" ")
for k in range(1,i+1):
print("*",end="")
print()
4. Right triangle star pattern
Source code:
def pypattern(n):
for i in range(0, n):
for j in range(0, i+1):
print("*",end="")
print("\r")
n = int(input())
pypattern(n)
5.Factorial using Recurrsion
Source code:
def factorial(n):
return 1 if (n==1 or n==0) else n * factorial(n - 1);
num = int(input())
print(factorial(num))
6.Print Table
Source code:
n=int(input())
for i in range(1,11):
j=i*n
print(n,"*",i,"=",j)
7.Find the Maximum:
Source code:
a=int(input())
b=int(input())
c=int(input())
if(a>b and a>c):
print(a)
elif(b>a and b>c):
print(b)
else:
print(c)
8.Find the Factorial using Function
Source code:
a=int(input())
f=1
for i in range(1,a+1):
f=f*i
print(f)
9.Arithmetic Operators
Source code:
if __name__ == '__main__':
a = int(input())
b = int(input())
print(a+b)
print(a-b)
print(a*b)
10. Python:Division
Source code:
if __name__ == '__main__':
a = int(input())
b = int(input())
c = a/b
d = int(c)
print(d)
print(c)
11.Find Digits
Source Code:
def finddigits(inputarray):
for element in inputarray:
counter = 0
orig = element
while int(element):
digit = int(element) % 10
if (int(digit) != 0.0 and (orig % digit == 0.0)):
counter = counter + 1
element = int(element)/10
print (counter)
testcase = int(input())
inputarray = []
for x in range(0, testcase):
inputele = int(input())
inputarray.append(inputele)
finddigits(inputarray)
12. List Comprehensions
Source Code:
xlist = range(int(input()) + 1)
ylist = range(int(input()) + 1)
zlist = range(int(input()) + 1)
n = int(input())
lst = [[x,y,z] for x in xlist for y in ylist for z in zlist if x + y + z != n]
print(lst)
13. The Captain's Room
Source Code:
k, arr = int(input()), list(map(int, input().split()))
myset = set(arr)
print(((sum(myset)*k)-(sum(arr)))//(k-1))
14. Nested Lists
Source Code:
def secondlow(students):
grades = []
for student in students:
grades.append(student[1])
sort_grades = sorted(grades)
seclow_grade = sort_grades[0]
for grade in sort_grades:
if grade != seclow_grade:
seclow_grade = grade
break
seclow_stud = []
for student in students:
if student[1] == seclow_grade:
seclow_stud.append(student[0])
for name in sorted(seclow_stud):
print(name)
students = []
for pupil in range(int(input())):
new_stud = [input(), float(input())]
students.append(new_stud)
secondlow(students)
15. sWAP cASE
Source Code:
def swap_case(s):
return s.swapcase()
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
16. Finding the Percentage
Source Code:
N = int(input())
dictionary = {}
for i in range(0, N):
inputArray = input().split()
marks = list(map(float, inputArray[1:]))
dictionary[inputArray[0]] = sum(marks)/float(len(marks))
print("%.2f" % dictionary[input()])
17. String Validators
Source Code:
if __name__ == '__main__':
s = input()
print (any(c.isalnum() for c in s))
print (any(c.isalpha() for c in s))
print (any(c.isdigit() for c in s))
print (any(c.islower() for c in s))
print (any(c.isupper() for c in s))
18. Designer Door Mat
(Compile the program in Python 2)
Source Code:
N,M = map(int,raw_input().split())
20. Map and Lambda Function
Source Code:
cube = lambda x: x**3 # complete the lambda function
def fibonacci(n):
# return a list of fibonacci numbers
initiallist = []
for i in range(n):
if i < 2:
initiallist += [i]
else:
initiallist += [initiallist[-1] + initiallist[-2]]
return initiallist
21. Alphabet Rangoli
Source Code:
def print_rangoli(size):
width = size*4-3
string = ''
for i in range(1,size+1):
for j in range(0,i):
string += chr(96+size-j)
if len(string) < width :
string += '-'
for k in range(i-1,0,-1):
string += chr(97+size-k)
if len(string) < width :
string += '-'
print(string.center(width,'-'))
string = ''
for i in range(size-1,0,-1):
string = ''
for j in range(0,i):
string += chr(96+size-j)
if len(string) < width :
string += '-'
for k in range(i-1,0,-1):
string += chr(97+size-k)
if len(string) < width :
string += '-'
print(string.center(width,'-'))
20: Introduction to sets:
Source Code:
def average(array):
return sum(set(array))/len(set(array))
21: Set.add()
Source code:
#!/usr/bin/env python
N = int(raw_input())
countries = set()
for i in range(N):
countries.add(raw_input())
print len(countries)
22: No Idea!
Source Code:
n, m = (int(i) for i in input().split())
l = map(int, input().strip().split(' '))
a = set(map(int, input().strip().split(' ')))
b = set(map(int, input().strip().split(' ')))
result = 0
for i in l:
if i in a:
result += 1
if i in b:
result += -1
print(result)
23: Triangle Number
Source Code:
#!/bin/python3
import os
import sys
# Complete the solve function below.
def solve(n):
if n%2!=0:
return 2
n-=4
n//=2
if n%2==0:
return 3
return 4
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH']
, 'w')
t = int(input())
for t_itr in range(t):
n = int(input())
result = solve(n)
fptr.write(str(result) + '\n')
fptrclose()
24: Find the Torsional Angle
Source Code:
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.z * no.x - self.x * no.z
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)
25: Triangle Quest
Source Code:
for i in range(1, input()):
print ((10 ** i - 1) / 9) * i
26: Dealing with Complex Numbers
Source Code:
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 __div__(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
Source Code:
str=input()
def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
ch = ch.lower()
if (ch == 'a' or ch == 'e' or ch == 'i'
or ch == 'o' or ch == 'u'):
vowels += 1
else:
consonant += 1
elif (ch >= '0' and ch <= '9'):
digit +=1
print(vowels)
print(consonant)
print(digit)
countCharacterType(str)
2.Pyramid Star pattern 1
Source Code:
def full_pyramid(rows):
for i in range(rows):
print(' '*(rows-i-1) + '*'*(2*i+1))
n=int(input())
full_pyramid(n)
3. Mirrored Right triangle star pattern
Source code:
k=0
j=0
n=int(input())
for i in range(1,n+1):
for j in range(0,n-i):
print("",end=" ")
for k in range(1,i+1):
print("*",end="")
print()
4. Right triangle star pattern
Source code:
def pypattern(n):
for i in range(0, n):
for j in range(0, i+1):
print("*",end="")
print("\r")
n = int(input())
pypattern(n)
5.Factorial using Recurrsion
Source code:
def factorial(n):
return 1 if (n==1 or n==0) else n * factorial(n - 1);
num = int(input())
print(factorial(num))
6.Print Table
Source code:
n=int(input())
for i in range(1,11):
j=i*n
print(n,"*",i,"=",j)
7.Find the Maximum:
Source code:
a=int(input())
b=int(input())
c=int(input())
if(a>b and a>c):
print(a)
elif(b>a and b>c):
print(b)
else:
print(c)
8.Find the Factorial using Function
Source code:
a=int(input())
f=1
for i in range(1,a+1):
f=f*i
print(f)
9.Arithmetic Operators
Source code:
if __name__ == '__main__':
a = int(input())
b = int(input())
print(a+b)
print(a-b)
print(a*b)
10. Python:Division
Source code:
if __name__ == '__main__':
a = int(input())
b = int(input())
c = a/b
d = int(c)
print(d)
print(c)
11.Find Digits
Source Code:
def finddigits(inputarray):
for element in inputarray:
counter = 0
orig = element
while int(element):
digit = int(element) % 10
if (int(digit) != 0.0 and (orig % digit == 0.0)):
counter = counter + 1
element = int(element)/10
print (counter)
testcase = int(input())
inputarray = []
for x in range(0, testcase):
inputele = int(input())
inputarray.append(inputele)
finddigits(inputarray)
12. List Comprehensions
Source Code:
xlist = range(int(input()) + 1)
ylist = range(int(input()) + 1)
zlist = range(int(input()) + 1)
n = int(input())
lst = [[x,y,z] for x in xlist for y in ylist for z in zlist if x + y + z != n]
print(lst)
13. The Captain's Room
Source Code:
k, arr = int(input()), list(map(int, input().split()))
myset = set(arr)
print(((sum(myset)*k)-(sum(arr)))//(k-1))
14. Nested Lists
Source Code:
def secondlow(students):
grades = []
for student in students:
grades.append(student[1])
sort_grades = sorted(grades)
seclow_grade = sort_grades[0]
for grade in sort_grades:
if grade != seclow_grade:
seclow_grade = grade
break
seclow_stud = []
for student in students:
if student[1] == seclow_grade:
seclow_stud.append(student[0])
for name in sorted(seclow_stud):
print(name)
students = []
for pupil in range(int(input())):
new_stud = [input(), float(input())]
students.append(new_stud)
secondlow(students)
15. sWAP cASE
Source Code:
def swap_case(s):
return s.swapcase()
if __name__ == '__main__':
s = input()
result = swap_case(s)
print(result)
16. Finding the Percentage
Source Code:
N = int(input())
dictionary = {}
for i in range(0, N):
inputArray = input().split()
marks = list(map(float, inputArray[1:]))
dictionary[inputArray[0]] = sum(marks)/float(len(marks))
print("%.2f" % dictionary[input()])
17. String Validators
Source Code:
if __name__ == '__main__':
s = input()
print (any(c.isalnum() for c in s))
print (any(c.isalpha() for c in s))
print (any(c.isdigit() for c in s))
print (any(c.islower() for c in s))
print (any(c.isupper() for c in s))
18. Designer Door Mat
(Compile the program in Python 2)
Source Code:
N,M = map(int,raw_input().split())
for i in xrange(1, N, 2):
print ( str('.|.')*i ).center(M, '-')
print str('WELCOME').center(M, '-')
for i in xrange(N-2, -1, -2):
print ( str('.|.')*i ).center(M, '-')
19. Find the Runner-Up-Score
Source Code:
n = int(input())
nums = map(int, input().split())
print(sorted(list(set(nums)))[-2])
print ( str('.|.')*i ).center(M, '-')
print str('WELCOME').center(M, '-')
for i in xrange(N-2, -1, -2):
print ( str('.|.')*i ).center(M, '-')
19. Find the Runner-Up-Score
Source Code:
n = int(input())
nums = map(int, input().split())
print(sorted(list(set(nums)))[-2])
20. Map and Lambda Function
Source Code:
cube = lambda x: x**3 # complete the lambda function
def fibonacci(n):
# return a list of fibonacci numbers
initiallist = []
for i in range(n):
if i < 2:
initiallist += [i]
else:
initiallist += [initiallist[-1] + initiallist[-2]]
return initiallist
21. Alphabet Rangoli
Source Code:
def print_rangoli(size):
width = size*4-3
string = ''
for i in range(1,size+1):
for j in range(0,i):
string += chr(96+size-j)
if len(string) < width :
string += '-'
for k in range(i-1,0,-1):
string += chr(97+size-k)
if len(string) < width :
string += '-'
print(string.center(width,'-'))
string = ''
for i in range(size-1,0,-1):
string = ''
for j in range(0,i):
string += chr(96+size-j)
if len(string) < width :
string += '-'
for k in range(i-1,0,-1):
string += chr(97+size-k)
if len(string) < width :
string += '-'
print(string.center(width,'-'))
20: Introduction to sets:
Source Code:
def average(array):
return sum(set(array))/len(set(array))
21: Set.add()
Source code:
#!/usr/bin/env python
N = int(raw_input())
countries = set()
for i in range(N):
countries.add(raw_input())
print len(countries)
22: No Idea!
Source Code:
n, m = (int(i) for i in input().split())
l = map(int, input().strip().split(' '))
a = set(map(int, input().strip().split(' ')))
b = set(map(int, input().strip().split(' ')))
result = 0
for i in l:
if i in a:
result += 1
if i in b:
result += -1
print(result)
23: Triangle Number
Source Code:
#!/bin/python3
import os
import sys
# Complete the solve function below.
def solve(n):
if n%2!=0:
return 2
n-=4
n//=2
if n%2==0:
return 3
return 4
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH']
t = int(input())
for t_itr in range(t):
n = int(input())
result = solve(n)
fptr.write(str(result) + '\n')
fptrclose()
24: Find the Torsional Angle
Source Code:
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.z * no.x - self.x * no.z
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)
25: Triangle Quest
Source Code:
for i in range(1, input()):
print ((10 ** i - 1) / 9) * i
26: Dealing with Complex Numbers
Source Code:
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 __div__(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
hii
ReplyDeleteI have tested a few and the best hackers for hire on the dark web are the guys at dark web hackers, download Torbrowser and then go to this dark
ReplyDeleteweb site with Torbrowser:
http://ziagmjbpt47drkrk.onion/