Saturday 29 July 2017

PPS 2


Practice Problems 2 Fall 17

1) 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.

******************************************************************************************
2) 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")
-----------------------------------------------------------------------------------
(If no restriction is given to use any inbuilt functions:)
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

******************************************************************************************
3) 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

******************************************************************************************
4) 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.

******************************************************************************************
5) 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"
******************************************************************************************
6) 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: (Verified👍)
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.


******************************************************************************************
YOU ARE SMART ENOUGH TO DRAW TO FLOWCHARTS.


20 comments:

  1. Since you guys are supposed to learn upto the flowcharts by now, The flow charts will not be uploaded from now onwards. (And after some time your teacher will also ask you not to draw them.)

    ReplyDelete
    Replies
    1. Flow charts bro. We know how to make it but for just in case if we did somewhere wrong.

      Delete
    2. No problem man! You will learn like this only. And also flow chart doesn't count much. the main thing is the code.

      Delete
  2. Replies
    1. INLAB 3 solutions are uploaded.

      https://myskillrack.blogspot.in/2017/08/inlab-3.html

      Delete
  3. what type of questions come in assessment 1

    ReplyDelete
    Replies
    1. Questions are similar to the practice problems.

      Delete
  4. i want to ask that my two two problems are uploaded without flowchart is there any problem?

    ReplyDelete
    Replies
    1. Depends on your faculty, though it doesn't count much.

      Delete
  5. Is the code for Roman numerals verified?

    ReplyDelete
    Replies
    1. I hope the code is correct, but if it is not getting executed, let me know.

      Delete
  6. How will assessment 1 be?What are we required to learn?

    ReplyDelete
    Replies
    1. It's easy and is already over for some of the batches. Ask them.
      The level of the assessments goes parallel with the practice problems.

      Delete
  7. please upload flowcharts for pps2

    ReplyDelete
    Replies
    1. Flowcharts will not be uploaded, u can do it by your own. And also after some time your teacher will ask you not to do it, because it doesn't carry much weightage.

      Delete
  8. It had written a code for digit factor program in pp 2 but it shows error hidden test case failed

    ReplyDelete
    Replies
    1. In the question it is clearly mentioned=> (Use only conditional and iterational statements for designing the algorithm and implementation).

      Delete
  9. use import.math for using gdc function
    call gdc like this: x=math.gdc(m,n)
    or type this in the beginning:
    from math import gdc

    ReplyDelete
    Replies
    1. In the question it is clearly mentioned=> (Use only conditional and iterational statements for designing the algorithm and implementation).

      Delete