Write a program to design a dice throw function
import random
def dice():
return random.choice([1,2,3,4,5,6])
### Write a program to print perfect numbers from the given list of integers
def checkPerfectNum(n) :
i = 2;sum = 1;
while(i <= n//2 ) :
if (n % i == 0) :
sum += i
i += 1
if sum == n :
print(n,end=' ')
if __name__ == "__main__" :
print("Enter list of integers: ")
list_of_intgers = list(map(int,input().split()))
print("Given list of integers:",list_of_intgers)
print("Perfect numbers present in the list is: ")
for num in list_of_intgers :
checkPerfectNum(num)
Write a program to convert meters into yards
num = float(input("Enter the distance measured in centimeter : "))
inc = num/2.54
print("Distance in inch : ", inc)
Write a program Tower of Hanoi
def hanoi(x):
global repN
repN += 1
if x == 1:
return 2
else:
return 3*hanoi(x-1) + 2
x = int(input("ENTER THE NUMBER OF DISKS: "))
global repN
repN =0
print('NUMBER OF STEPS: ', hanoi(x), ' :', repN)
Write a program to find variance of a dataset
def variance(X):
mean = sum(X)/len(X)
tot = 0.0
for x in X:
tot = tot + (x - mean)**2
return tot/len(X)
# main code
# a simple data-set
sample = [1, 2, 3, 4, 5]
print("variance of the sample is: ", variance(sample))
Write a program to find winner of the day
def find_winner_of_the_day(*match_tuple):
team1_count = 0
team2_count = 0
for team_name in match_tuple :
if team_name == "Team1" :
team1_count += 1
else :
team2_count += 1
if team1_count == team2_count :
return "Tie"
elif team1_count > team2_count :
return "Team1"
else :
return "Team2"
if __name__ == "__main__" :
print(find_winner_of_the_day("Team1","Team2","Team1"))
print(find_winner_of_the_day("Team1","Team2","Team1","Team2"))
print(find_winner_of_the_day("Team1","Team2","Team2","Team1","Team2"))
Write a program for swapping the value of two integers without third variable
x = int(input("Enter the value of x :"))
y = int(input("Enter the value of y :"))
(x,y) = (y,x)
print('Value of x: ', x, '\nValue of y: ', y, '\nWOO!! Values SWAPPEDDD')
Write a program to check eligibility for voting
# input age
age = int(input("Enter Age : "))
if age>=18:
status="Eligible"
else:
status="Not Eligible"
print("You are ",status," for Vote.")
import sys
print("Python version: ", sys.version)
print("Python version info: ", sys.version_info)
### Write a program to find sum of all digits of a number
def sumDigits(num):
if num == 0:
return 0
else:
return num % 10 + sumDigits(int(num / 10))
x = 0
print("Number: ", x)
print("Sum of digits: ", sumDigits(x))
print()
Write a program to print double quotes with the string variable
str1 = "Hello world";
print("\"%s\"" % str1)
print('"%s"' % str1)
print('"{}"'.format(str1))
Write a program to Remove leading zeros from an IP address
import re
def removeLeadingZeros(ip):
modified_ip = re.sub(regex, '.', ip)
print(modified_ip)
if __name__ == '__main__' :
ip = "216.08.094.196"
removeLeadingZeros(ip)
Write a program for binary search
def binary_search(l, num_find):
start = 0
end = len(l) - 1
mid = (start + end) // 2
found = False
position = -1
while start <= end:
if l[mid] == num_find:
found = True
position = mid
break
if num_find > l[mid]:
start = mid + 1
mid = (start + end) // 2
else:
end = mid - 1
mid = (start + end) // 2
return (found, position)
if __name__=='__main__':
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
num = 6
found = binary_search(l, num)
if found[0]:
print('Number %d found at position %d'%(num, found[1]+1))
else:
print('Number %d not found'%num)