Monday 31 July 2017

PPS 1


Practice Problems 1 Fall 17

1) 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:
print('Hello')

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:
START
1) Read the number 'n'
2) s=0, m=n
3) if(len(str(n))==3 and n>=100 and n<=999), continue
4) while(m), s=s+(m%10)**3 and m=m//10 => Finding the sum of the cubes of digits
5) if n==s, print('Armstrong') else print('Not armstrong')
6) Else print('Invalid input')
END

Flow Chart:



******************************************************************************************


2) Devise an algorithm and draw a flowchart to simulate the working of an AND gate. AND gate takes two bits as input and output a bit as shown in the following table. Check for validity of input and print ‘Invalid input’ when user gives out of boundary values.

Code:
print('Hello')

Input:
Read two bits 'x' and 'y'

Processing:
if(x==1 and y==1):
     print(1)
else:
     print(0)

Output:
Print 0 or 1

Pseudo Code:
START
1) Read two bits 'x' and 'y'
2) If x=0 or x=1 continue, else print 'Invalid input'
3) If x=0 or x=1 continue, else print 'Invalid input'
4) if x=1 and y=1, print 1
5) else, print 0
END


Flow Chart:




******************************************************************************************

3) A health drink company, gives a festival offer to its retail customers. The health drink is packed in 1 kg pack. For every 5, one kg pack purchase, one 1kg pack will be free. (i.e. buy 5 get 1 free). Given the number of health drink packets purchased, design an algorithm and draw a flowchart to determine the total number of packets that the customer will get.

Code:
print('Hello')

=>
packets=int(input())
if(packets>0 and packets<=500):
   free = packets//5
   total = packets + free
   print(total)
else:
   print('Invalid input')


Input:
Number of health drink packets purchased

Processing:
if(n>0 and n<=500):
   free = packets//5
   total = packets + free
   print(totaal)
else:
   print('Invalid input')

Output:
Print the Total number of packets that the customer will get.

Pseudo Code:
START
1) Read the number of packets purchased 'n'
2) if n>0 and n<=500 ,continue else print 'Enter valid number of packets'
3) free = packets//5
4) total = packets + free
5) print (total)
END

Flow Chart:



******************************************************************************************

4) Every day morning and evening milk is brought from ‘n’ farms to a milk booth for sales. Given the amount of milk from ‘n’ farms, write an algorithm to compute total quantity of milk in the booth. For example, if milk comes from 3 farms in quantities 2 litres 300ml, 3 litres 700ml and 4 litres 600ml then the total quantity of milk in booth is 10litre 600ml.


Code:
print('Hello')

=>
n=int(input())
ml=0
for i in range(n):
l=int(input())
m=int(input())
ml=ml+m+l*1000
litre=ml//1000
ml=ml%1000
print(str(litre)+'litre'+str(ml)+' '+'ml')


Input:
Number of farms 'n'
Amount of milk from each of the 'n' farms

Processing:
for i in range(n):
l=int(input())
m=int(input())
ml=ml+m+l*1000
litre=ml//1000
ml=ml%1000


print(str(litre)+'litre'+str(ml)+' '+'ml')

Output:
Print the total quantity of milk in booth

Pseudo Code:
START
1) Read the number of farms
2) let count = 0 and total = 0
3) while(count<n):
4) Read the amount of milk from each of the n farms, litres
5) count = count + 1
6) total = total + litres
END

Flow Chart:



******************************************************************************************

5) Arjun, amith and sharma are friends. Given their marks in Maths, design an algorithm and draw a flowchart to find minimum of their marks.

Code:
print('Hello')

Input:
Read marks of Arjun in maths
Read marks of Amit in maths
Read marks of Sharma in maths

Processing:
m1=int(input())
m2=int(input())
m3=int(input())
if(m1>=0 and m1<=100 and m2>=0 and m2<=100 and m3>=0 and m3<=100):
min=m1
if(m2<min):
min=m2
if(m3<min):
min=m3
print(min)
else:
print('Invalid input')


Output:
Print the name of student who scored minimum marks in maths
Print the minimum marks in maths

Pseudo Code:
START
1) Read marks of Arjun in maths
2) Read marks of Amit in maths
3) Read marks of Sharma in maths
4) if(m1>=0 and m1<=100 and m2>=0 and m2<=100 and m3>=0 and m3<=100), continue
5) let min=m1
6) if(m2<min), update min=m2
7) if(m3<min), update min=m3
8) Print min
END

Flow Chart:



******************************************************************************************
THANK YOU FOR VISITING...
******************************************************************************************

Saturday 29 July 2017

INLAB 1


Given a four digit number 'x', write an algorithm to check whether ‘x’ is divisible by the sum of the digits of ‘x’. A number ‘a’ is said to be divisible by ‘b’, if the remainder is zero when ‘a’ divides ‘b’.
For example, the number ‘x’ is 1234 is not divisible by 10, because if we divide ‘1234’ with the sum of its individual digits (1+2+3+4 = 10), the remainder is not zero.  
Code:
n=int(input())
m=n
s=0
while(n):
last=n%10
s=s+last
n=n//10
if(m%s==0):
print('Divisible')
else:
print('Not divisibe')

Input:
The number 'n'

Processing:
while(n):
last=n%10
s=s+last
n=n//10

Output:
Print either 'Divisible' or 'Not divisible'

Pseudo Code:
1) Read the number 'n' to be checked
2) Find the sum of the digits of 'n' and let it be 's'
3) If 'n' is divisible by the sum => n%s==0
4) If 'n' is divisible by 's' then print 'Divisible' else print 'Not divisible'

Flow Chart:




******************************************************************************************

INLAB 2



Given two adjacent vertices of a square, write an algorithm and the subsequent Python code to determine the length of the diagonal. Pythagorean theorem states that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. For example, if the coordinates of the adjacent vertices of a square are given as (10, 15) and (20, 15) then the other two vertices in sorted order are (10, 25) and (20, 25) and length of the diagonal is 14.14. Print only two decimal places for length of the diagonal.
Input Format
X-coordinate of first vertex of the square
Y-coordinate of first vertex of the square
X-coordinate of second vertex of the square
Y-coordinate of second vertex of the square
Output Format
Other two vertices of the square as a tuple, one in each line
Diagonal of the square with two decimal places only
Code:
x1=int(input())
y1=int(input())
x2=int(input())
y2=int(input())
s=int(((x1-x2)**2+(y1-y2)**2)**0.5)
d=(s**2+s**2)**0.5
if(y1==y2):
tup1=(x1,y1+s)
tup2=(x2,y2+s)
else:
tup1=(x1+s,y1)
tup2=(x2+s,y2)
print(tup1)
print(tup2)
print(round(d,2))

Input:
x-coordinate of first vertex
y-coordinate of first vertex
x-coordinate of second vertex
y-coordinate of second vertex

Processing:
if(y1==y2):
tup1=(x1,y1+s)
tup2=(x2,y2+s)
else:
tup1=(x1+s,y1)
tup2=(x2+s,y2)

Output:
Other two vertices of the square as a tuple, one in each line
Diagonal of the square with two decimal places only

Pseudo Code:
1) Read the x and y coordinates of the two given vertices
2) Find the other two vertices by:
           V3 =(x1,y1+s)
   V4 =(x2,y2+s)
3) Print the other two vertices as tuples
4) Print the diagonal of the square

******************************************************************************************

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.