Uploaded by yoyobrothas

Python Lab Program: Problem Solving Exercises

advertisement
lOMoARcPSD|49218470
Python elab Program
Programming For Problem Solving (SRM Institute of Science and Technology)
Scan to open on Studocu
Studocu is not sponsored or endorsed by any college or university
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
I/O – Session 1
Binita was traveling from Chennai
tot = int(input(''))
hrs = int(tot/60)
mins = int(tot % 60)
print(hrs, 'Hours and', mins,'Minutes')
Compute the area of a triangle
import math
s1=昀氀oat(input())
s2=昀氀oat(input())
s3=昀氀oat(input())
s=(s1+s2+s3)/2
area =math.sqrt(s*(s-s1)*(s-s2)*(s-s3))
print('The area of triangle is {}'.format(area))
Vetrivel wants to convert the
days=int(input())
hours=int(input())
minutes=int(input())
seconds=int(input())
result=(days*24*3600)+(hours*3600)+(minutes*60)+seconds
print(result)
Timothy boom having the 昀椀rst name
fname=input()
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
lname=input()
reversed_name= lname + " " + fname
print("Hello",reversed_name)
The electricity o昀케cer has men琀椀on
uc = int(input())
cpu = int(input())
tb = int(uc ** cpu)
print(tb)
What is the sum of the digits
def calculate(n, power):
return sum([int(i) for i in str(pow(n, power))])
n=2
power = int(input(''))
print (calculate(n,power))
Arif planned to make a room
length=昀氀oat(input())
width=昀氀oat(input())
area=length*width
print(round(area,2))
Laasya bought a new volley ball
r = 昀氀oat(input(''))
pi = 3.14
volume = (4.0/3.0)*pi*r**3
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
print(volume)
In geometry, the area enclosed
r = 昀氀oat(input(""))
pi = 3.142
area = pi * r**2
print("The area of the circle with radius",r,"is:",area)
Pinnaccio went to the school
G=int(input())
S=int(input())
print("The Range of given data =",G-S)
Sajid was booking a train 琀椀cket
num1,num2,num3=input().split()
total=int(num1)+int(num2)+int(num3)
print(total)
Selvan was playing with an object
l = int(input(''))
w = int(input(''))
h = int(input(''))
sa = (2*((w*l)+(l*h)+(h*w)))
print(sa)
Athika and ritu got a nice job
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
bpa = 昀氀oat(input(''))
hra = bpa*0.8
da = bpa*0.4
total = hra+da+bpa
print('{:.02f}'.format(total))
Janaki wants to 昀椀nd the distance
import math
x1=int(input())
y1=int(input())
x2=int(input())
y2=int(input())
d=math.sqrt((x2-x1)**2+(y2-y1)**2)
print(d)
Ra琀椀ka a young millionaire
p=昀氀oat(input())
i=昀氀oat(input())
t=int(input())
interest=(p*i*t)/100
amount=p+interest
print("Interest a昀琀er {c} Years = ${a:.2f}\nTotal Amount a昀琀er {c} Years =
${b:.2f}".format(a=interest,b=amount,c=t))
Compute the area of triangle
import math
s1=昀氀oat(input())
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
s2=昀氀oat(input())
s3=昀氀oat(input())
s=(s1+s2+s3)/2
area=math.sqrt (s*(s-s1)*(s-s2)*(s-s3))
print("The area of triangle is",area)
Nancy bought apples
num1 = int(input())
num2 = int(input())
quo琀椀ent = round(num1/num2)
remainder = num1%num2
print("Quo琀椀ent:",end="")
print(quo琀椀ent)
print("Remainder:",end="")
print(remainder)
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
Flow control - Session 2
Caleb and Salima are living in interior village
x=int(input())
y=int(input())
if x>y:
print(x-y)
else:
print(x+y)
Shree and Harry was living in the town
num1=昀氀oat(input())
num2=昀氀oat(input())
if num1-num2==0.5 or num2-num1==0.5:
print("Approximate Number")
else:
print("Not an Approximate Number")
Mr.Issac the head of Tamilnadu
f=昀氀oat(input())
c=(f-32)*(5/9)
print(round(c,2),"Cen琀椀grade")
if c>=150:
print("Very Hot")
elif 150>=c>=100:
print("Hot")
else:
print("Moderate")
Adhi and Tara travel frequently
m=int(input())
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
r=昀氀oat(input())
d=int(input())
if m==4 or m==5:
r1=r*d
e=(r1*20)/100
print("Rs."+"%.2f"%(r1+e))
else:
print("Rs."+"%.2f"%(r*d))
Mrs.Swathy is a principal
s1=int(input())
s2=int(input())
s3=int(input())
s4=int(input())
s5=int(input())
t=s1+s2+s3+s4+s5
p=t/5
print("%.2f"%p,"Percent")
if p>=90:
print("Grade A")
elif 90>=p>=80:
print("Grade B")
elif 80>=p>=70:
print("Grade C")
elif 70>=p>=60:
print("Grade D")
elif 60>=p>=40:
print("Grade E")
else:
print("Grade F")
A team from royal squatraclub
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
a1=int(input())
w1=int(input())
if a1>=18 and w1>=50:
print("Eligible for Dona琀椀on")
else:
print("Not Eligible for Dona琀椀on")
Arav and Aaron are par琀椀cipa琀椀ng
a1=int(input())
a2=int(input())
if a1>a2:
print(a1-a2)
else:
print(a2-a1)
A paytm announced a cash back
n=int(input())
a=n%10
if a%2==0:
print("Even Currency")
else:
print("Odd Currency")
Three brothers want to take a photo
b1=int(input())
b2=int(input())
b3=int(input())
if b1>b2 and b1>b3:
print(b1)
elif b2>b1 and b2>b3:
print(b2)
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
else:
print(b3)
Selvan is working as a QC
a=input()
if a.isdigit():
print("NOT AN ALPHABET")
else:
print("ALPHABET")
Laasya looking at the friends birthday
a=int(input())
if a%4==0:
print("LEAP YEAR")
else:
print("NOT A LEAP YEAR")
The elec琀椀on commission of India
age = int(input())
if age >= 18:
print("Eligible")
else:
print("Not Eligible")
Caleb and Irfan are purchasing
apple1 = int(input())
apple2 = int(input())
apple3 = int(input())
if apple2 > apple1 and apple3 > apple2:
print("Fit into Budget")
else:
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
print("Doesn't 昀椀t into Budget")
A琀椀fa would like to withdraw
wamt=int(input())
inamt=昀氀oat(input())
if wamt%5==0:
if wamt>inamt:
print("Invalid Withdrawal Request")
print("Ini琀椀al Balance :",format(inamt,".2f"))
else:
x=(inamt-wamt)-0.5
print("Current Balance :",format(x,".2f"))
print("Ini琀椀al Balance :",format(inamt,".2f"))
Yasir techie is working in a military
num = int(input())
if(num>0):
print("POSITIVE")
else:
print("NEGATIVE")
Simon was working in a casa grand
Arunmozhivarman a famous skill trainer
operator=input()
n1=昀氀oat(input())
n2=昀氀oat(input())
sum=n1+n2
di昀昀erence=n1-n2
product=n1*n2
divison=n1/n2
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
if operator=='+':
print(sum)
elif operator=='-':
print(di昀昀ernce)
elif operator=='*':
print(product)
elif operator=='/':
print(divison)
else:
print("Invalid Input")
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
Arrays and Loops – Session 3
Akash the die heart
s=int(input())
for i in range(s,0,-1):
for j in range(i):
print(i,end=" ")
print()
Euler asked the ques琀椀on to ramanujam
def sum_of_mul琀椀ples(N):
total = 0
for i in range(N):
if i % 3 == 0 or i % 5 == 0:
total += i
return total
N = int(input()) # get input from user
print(sum_of_mul琀椀ples(N)) # calculate and print the sum of mul琀椀ples
Matrix is a rectangular arrangement
r=int(input())
c=int(input())
matrix=[]
for i in range(r):
a =[]
for j in range(c):
a.append(int(input()))
matrix.append(a)
for i in range(r):
for j in range(c):
print(matrix[i][j], end = " ")
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
print()
In our ins琀椀tu琀椀ons celebrates
a=int(input())
c=[]
for i in range(a):
c.append(a)
for j in range(i + 1):
if j == i:
print(chr(97 + j), end=" ")
else:
print(chr(97 + j), end=" ")
print()
2520 is the smallest number
import math
# Returns the lcm of 昀椀rst n numbers
def lcm(n):
ans = 1
for i in range(1, n + 1):
ans = int((ans * i)/math.gcd(ans, i))
return ans
if 1>2:
a=5
# main
n = int(input())
print (lcm(n))
Pentagonal numbers are generated
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
length = int(input())
for n in range(1,length):
p = round(n*((3*n)-1)/2)
print(p)
Mukesh and salima was looking
a=int(input())
b=[]
for i in range(a):
c=int(input())
b.append(c)
x=[]
y=[]
for i in b:
if i not in x:
x.append(i)
elif i in x:
y.append(i)
else:
break
print(len(y))
Thanos and iron man had a challenge
a=int(input())
b=[]
for i in range(a):
c=int(input())
b.append(c)
e=[]
o=[]
for i in b:
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
if i%2==0:
e.append(i)
else:
o.append(i)
for i in e:
print(i,end=' ')
print()
for i in o:
print(i,end=' ')
Matrix is a rectangular arrangement
r=int(input())
c=int(input())
matrix=[]
for i in range(r):
a =[]
for j in range(c):
a.append(int(input()))
matrix.append(a)
for i in range(len(matrix)):
k=0
for j in matrix:
print(matrix[k][i], end = " ")
k+=1
print()
In the ba琀琀le of kurukshestra
n=int(input())
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
m=1
for i in range(n):
h='* '
print(h*m)
m+=1
n = int(input()) # get input from user
# iterate over each row
for i in range(n):
# print the appropriate number of stars for this row
for j in range(i+1):
print("* ", end="")
print() # move to the next line
Marvel Studios is known
n = int(input())
# Read the character names
names = []
for i in range(n):
names.append(input())
# Sort the names in alphabe琀椀cal order
names.sort()
# Display the sorted names
for name in names:
print(name)
Mathema琀椀cs teacher gave the home
r=int(input())
c=int(input())
matrix=[]
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
for i in range(r*2):
a =[]
for j in range(c):
a.append(int(input()))
matrix.append(a)
matrix1=matrix[:r]
matrix2=matrix[r:]
for i in range(r):
for j in range(c):
print(matrix1[i][j]+matrix2[i][j],end=' ')
print()
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
Strings – Session 4
Arif likes to play valley ball
def determine_winner(match):
arif_score = 0
opponent_score = 0
score_set = set() # use a set to keep track of the scores
for i in range(len(match)):
if match[i] == '1':
arif_score += 1
else:
opponent_score += 1
if (arif_score >= 11 or opponent_score >= 11) and abs(arif_score - opponent_score) >= 2:
if arif_score > opponent_score:
return "WIN"
else:
return "LOSS"
score_set.add((arif_score, opponent_score)) # add the current score to the set
# check if the match ended with a score less than 11-10
last_score = list(score_set)[-1]
if last_score[0] >= 11 or last_score[1] >= 11:
if last_score[0] > last_score[1]:
return "WIN"
else:
return "LOSS"
else:
return "LOSS"
# main func琀椀on
if __name__ == "__main__":
t=int(input()) # number of test cases
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
for i in range(t):
matchscenario=str(input()) # input string describing match
result = determine_winner(matchscenario)
print(result) # print the winner of the match
Janaki set the password
s=str(input())
# Prin琀椀ng all substrings of the string s
for i in range(len(s)):
for j in range(i+1, len(s)+1):
print(s[i:j])
Lokesh usually likes to play
def can_be_iden琀椀cal(n, s1, s2):
s1_count = s1.count('1')
s2_count = s2.count('1')
if s1_count != s2_count:
return "NO"
else:
return "YES"
t = int(input())
for i in range(t):
n=int(input())
s1=str(input())
s2=str(input())
print(can_be_iden琀椀cal(n, s1, s2))
Janaki has the extra ability
t=int(input())
# Loop through each test case
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
for i in range(t):
# Get the string
test_string=str(input())
# Reverse the string
string_reversed = test_string[::-1]
# Print the reversed string
print(string_reversed)
A long 琀椀me age, there resided
t = int(input())
# Looping through each test case
for i in range(t):
# Reading the input string
str1=str(input())
# Ini琀椀alizing the vowel count
count = 0
# Looping through each character in the string
for j in range(len(str1)):
# Checking if the character is a vowel
if str1[j] in "aeiouAEIOU":
count += 1
# Prin琀椀ng the vowel count for the current string
print(count)
Sudan and siva are classmates
# Taking input for the number of test cases
t = int(input())
# Looping through each test case
for i in range(t):
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
# Taking input for the string
str1=str(input())
# Sor琀椀ng the characters in the string using the sorted() func琀椀on and the key parameter
sorted_string = sorted(str1, key=lambda x: x.isupper())
# Joining the sorted characters back to form the 昀椀nal string
昀椀nal_string = ''.join(sorted_string)
# Prin琀椀ng the 昀椀nal string
print(昀椀nal_string)
Aaron has a number D containing
def check_all_same(s):
# Count number of 0s and 1s
count_0 = s.count('0')
count_1 = s.count('1')
# If all digits are already same, return Yes
if count_0 == len(s) or count_1 == len(s):
return "YES"
# If there is only one 0 or one 1, return Yes
if count_0 == 1 or count_1 == 1:
return "YES"
# If there is more than one 0 and more than one 1, return No
if count_0 > 1 and count_1 > 1:
return "NO"
# If there is exactly one 0 and more than one 1, or exactly one 1 and more than one 0, return Yes
else:
return "YES"
if __name__ == "__main__":
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
lOMoARcPSD|49218470
# Get number of test cases
T = int(input())
# Loop over all test cases
for i in range(T):
# Get input string
s=str(input())
# Call func琀椀on to check if all digits can be made same by 昀氀ipping one digit
result = check_all_same(s)
# Print the result
print(result)
Downloaded by Maram Pujith (marampujithkumar@gmail.com)
Download