Skip to main content

PPS2


PRACTICE PROBLEMS 2 FALL 18

Q1) A city has a dam of capacity 'x' litres, water comes to the dam from 'n' places. Given the value of 'n' and the quantity of water (in litres and millilitres) that comes from 'n' places. Write an algorithm and the corresponding Python code to determine the total amount of water in the dam. Assume that the total quantity of water in the dam, will always be less than the capacity of the dam. For example, if there are three places from which water comes to the dam and the water from place 1 is 2 litres 500 ml, water from second place is 3 litres 400 ml and water from third place is 1 litre 700 ml, then the total quantity of water in dam will be 7 litres 600 ml.

Code:
n=int(input())
lit=0
ml=0
for i in range(n):
 lit=lit+int(input())
 ml=ml+int(input())
print(lit+(ml//1000))
print(ml%1000)

Input:
'n'
number of litres and ml of water from each of the 'n' places

Processing:
for i in range(n):
lit=lit+int(input())
ml=ml+int(input())

Output:
Print the total litres of water in the dam
Print the total millilitres of water in the dam

Pseudo Code:
1) Read n
2) Read the number of litres and ml of water from each of the 'n' places
3) Calculate the total water in the dam using for loop
4) water in litres = litres + ml//1000
5) water in ml = ml%1000
6) Print water in dam in litres and millilitres.


Q2) Given two numbers ‘m’ and ‘n’, design an algorithm and write the Python code to check if they are relatively prime. Two integers ‘a’ and ‘b’ are said to be relatively prime, mutually prime, or coprime, if the only positive integer that divides both of them is 1. For example, 14 and 15 are coprime since the factors of 14 are 1, 2, 7, & 14 and factors of 15 are 1, 3, 5, & 15 and there is no common factor other than 1. (Use only conditional and iterational statements for designing the algorithm and implementation).

Code:
m = int(input())
n = int(input())
i = 1
f = 1
if m >= n:
    min = n
else:
    min = m
while i <= min and f == 1:
    if m%i==0 and n%i == 0:
        f = i
    i += 1

if f == 1:
    print("Coprime")
else:
    print("Not coprime")

                                      (OR)
(If GCD Function can be used)

m=int(input())

n=int(input())
from fractions import gcd
x=gcd(m,n)
if(x==1):
print('Coprime')
else:
print('Not coprime')

Input:
Read 'm'
Read 'n'

Processing:
x=gcd(m,n)
if(x==1):
print('Coprime')
else:
print('Not coprime')

Output:
Print either Prime or Not coprime

Pseudo Code:
1) Read 'm' and 'n'
2) Find the GCD of 'm' and 'n'
3) If GCD = 1, they are Coprime
4) If GCD = 1, print Coprime, else print Coprime


Q3) Houseflies have an approximate life of four weeks. Given the number of days a housefly lived, design an algorithm and write the Python code to determine its approximate age in seconds. Check for boundary conditions and print ‘Invalid input’ if condition is not satisfied. For example, if a housefly lived for 21 days then its approximate age in seconds is 21*24*60*60 is 1814400.


Code:
n=int(input())
if(n>28 or n<=0):
print('Invalid input')
else:
s=n*24*60*60
print(s)

Input:
the number of days 'n' the fly lived

Processing:
hours = days*24
minutes = hours*60
seconds = minutes*60
Output:
Number of seconds the fly lived

Pseudo Code:
1) Read 'n'
2) Calculate the number of seconds the fly lived by:
     hours = days*24
     minutes = hours*60
     seconds = minutes*60
3) Print the number of seconds, the fly lived


Q4) Given ‘n’, the number of rows, design an algorithm and write a Python code to draw a pattern. If n is ‘5’, the pattern looks as shown below:


**
****
******
********
**********
Code:
n=int(input())
for i in range(1,n+1):
j=2*i
print('*'*j)

Input:
'n'

Processing:
for i in range(1,n+1):
j=2*i
print('*'*j)

Output:
Draw the pattern

Pseudo Code:
1) Read 'n'
2) Print the ** pattern.


Q5) The numeric system represented by Roman numerals originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the late Middle Ages. Numbers in this system are represented by combinations of letters as shown below:







Given a letter in Roman numeral, develop an algorithm and write the Python code to print the value of it. If some other letter other than Roman numeral is given as input then print ‘Enter a roman numeral’ and terminate.
Code:
r = input()
if r == 'I':
    print(1)
elif r == 'V':
    print(5)
elif r == 'X':
    print(10)
elif r == 'L':
    print(50)
elif r == 'C':
    print(100)
elif r == 'D':
    print(500)
elif r == 'M':
    print(1000)
else:
    print("Enter a roman numeral")

Input:
Roman Letter

Output:
Numeric value equivalent to the roman letter

Pseudo Code:
1) Read the Roman letter
2) If the letter is one among the given roman letters print its numeral value
3) Else print "Enter a roman numeral"

Q6) Given a number ‘n’, design an algorithm and write the Python program to  print the digits of ‘n’ that  divides ‘n’. Print the digits in reverse order of their appearance in the number ‘n’.  For example, if n is 122 then print 2, 2, 1. Use only conditional and iterative statements to write the code. If none of the digits divide the number, then print ‘No factors’
Code: 
n=int(input())
m=n
x=[]
for i in range(len(str(n))):
 last=n%10
 if(m%last==0):
  x.append(last)
 n=n//10
for i in range(len(x)):
 print(x[i])
if(len(x)==0):
    print('No factors')

Input:
the number 'n'

Processing:
for i in range(len(str(n))):
last=n%10
if(m%last==0):
x.append(last)
n=n//10

Output:
Print the Digits (in reverse order) of 'n' that divide it.

Pseudo Code:
1) Read the number 'n'
2) Make a list of digits of the number which divide it.
3) Print the entries of the list.

Comments

Popular posts from this blog

VIT SKILLRACK SOLUTIONS

     VIT SKILLRACK SOLUTIONS WITH        PSEUDOCODES PPS 1 FALL 18 Q1)  Design an algorithm and draw a flow chart to check the given number is Armstrong number of three digits or not. A number is said to be Armstrong number if the summation of cube of digits in a three digit number is equal to the number. Check for boundary conditions, if the value entered is outside boundary conditions then enter 'Invalid input'. Code: n=int(input('Enter the three digit number:')) if(len(str(n))==3 and n>=100 and n<=999): while(m): s=s+(m%10)**3 m=m//10 if(s==n): print('Yes') else: print('No') else: print('Invalid') Input: A number 'n' Processing: if(len(str(n))==3 and n>=100 and n<=999): while(m): s=s+(m%10)**3 m=m//10 if(s==n): print('Yes') else: print('No') else: print('Invalid') Output: Print Armstrong or Not armstrong Pseudo Code: STAR...