Professional Documents
Culture Documents
Pengrams are words or sentences containing every letter of the English alphabet at
the most once. Write an algorithm and a subsequent Python code to check whether
a string is a pengram or not. Write a function to check if a given string is a pengram.
For example, “He is at work” is a pengram. Since every letter of the english alphabet
occurs at the most once
Input format
Output Format
INPUT
First line contains the string to be checked
PROCESSING
In the string check whether each letter
OUTPUT
Print Pengram or Not pengram
PSEUDOCODE
1.Start
2.
s=input()
for i in s:
cnt=0
for j in s:
cnt+=1
if cnt>1:
print("Not pengram")
break
if cnt==1:
print("Pengram")
3.End
CODE
s=input()
for i in s:
cnt=0
for j in s:
cnt+=1
if cnt>1:
print("Not pengram")
break
if cnt==1:
print("Pengram")
Q2 Oddophic Numbers
Given ‘n’ integers, write an algorithm and the subsequent Python code to print all
numbers that are oddophic to the first number. Two numbers with non-distinct
(numbers in which digits get repeated) digits are said to be oddophic if they have the
same number of digits in it and the sets of positions having the same digits contains
only odd positions. Positions of digits are numbered from left to right starting from 1.
For example:
Input Format
Output Format
Next few lines contain the numbers that are oddophic to first number.
If the none of the numbers are not oddophic, then Print “No oddophic”
INPUT
First line contains the number of elements, n
PROCESSING
for i in range(0,len(str1)):
for j in range(i+1,len(str1)):
if(str1[i]==str1[j]):
if((i+1)%2==0 or (j+1)%2==0):
return -1
OUTPUT
Print first number in the first line
Next few lines contain the numbers that are oddophic to first number.
If the none of the numbers are not oddophic, then Print “No oddophic”
PSEUDOCODE
Start
flag=1
def odd(str1,str2,wd):
count=0
if((len(set(str1))==len(str1)) or (len(set(str1))==len(str2))):
return -1
if(len(str1)!=len(str2)):
return -1
for i in range(0,len(str1)):
for j in range(i+1,len(str1)):
if(str1[i]==str1[j]):
if((i+1)%2==0 or (j+1)%2==0):
return -1
for i in range(0,len(str2)):
for j in range(i+1,len(str2)):
if(str2[i]==str2[j]):
if((i+1)%2==0 or (j+1)%2==0):
return -1
if(wd==0):
print(str1)
print(str2)
return 0
n=int(input())
a=input()
ad=0
count=0
for i in range(1,n):
w=odd(a,input(),count)
if(w==0):
ad=ad+1
count=1
if(ad==0):
print("No oddophic")
Stop
CODE
flag=1
def odd(str1,str2,wd):
count=0
if((len(set(str1))==len(str1)) or (len(set(str1))==len(str2))):
return -1
if(len(str1)!=len(str2)):
return -1
for i in range(0,len(str1)):
for j in range(i+1,len(str1)):
if(str1[i]==str1[j]):
if((i+1)%2==0 or (j+1)%2==0):
return -1
for i in range(0,len(str2)):
for j in range(i+1,len(str2)):
if(str2[i]==str2[j]):
if((i+1)%2==0 or (j+1)%2==0):
return -1
if(wd==0):
print(str1)
print(str2)
return 0
n=int(input())
a=input()
ad=0
count=0
for i in range(1,n):
w=odd(a,input(),count)
if(w==0):
ad=ad+1
count=1
if(ad==0):
print("No oddophic")
Q3 Heterosquare Numbers
Heterosquare numbers are the numbers having “n” digits such that the last n digits of
the square of the number will not be the number itself. .Write an algorithm and the
subsequent Python code to check if the given number is a Heterosquare number.
Write a function to find the square of a given number. For example, 22 is a 2 digit
heterosquare number with a square of 484 and 111 with its square 12321, is a 3 digit
heterosquare number.
Input Format
Output Format
PROCESSING
square the number with n digits and check whether the last n digits are same as the
number given.
OUTPUT
Print Heterosquare or Not Heterosquare
PSEUDOCODE
1. Start
2.n=m*m
m=str(m)
n=str(n)
if n[len(n)-len(m):len(n)]!=m:
print("Heterosquare")
else:
print("Not heterosquare")
3.End
CODE
m=int(input())
n=m*m
m=str(m)
n=str(n)
if n[len(n)-len(m):len(n)]!=m:
print("Heterosquare")
else:
print("Not Heterosquare")
Q4 Diff-adam number
A number ‘n’ is said to be a Diffadam number if the absolute value of the difference
between the number ‘n’ and the reverse of ‘n’ is zero. For example, 121 is Diffadam
number. Reverse of a number is the number got by writing the digits of the number in
the reverse order (from right to left). Reverse of 132 is 231.
Write an Algorithm and the subsequent Python code to check whether the given
number is a Diffadam number or not.
Input Format:
Output Format:
INPUT
The first line will contain the number.
PROCESSING
for all the number given check whether each
OUTPUT
Print Diffadam number or Not a Diffadam number
PSEUDOCODE
1.Start
2.
def reverse(string):
if len(string) == 0:
return string
else:
a=input()
if (int(a)-int(reverse(a))==0):
print("Diffadam number")
else:
3.End
CODE
def reverse(string):
if len(string) == 0:
return string
else:
a=input()
if (int(a)-int(reverse(a))==0):
print("Diffadam number")
else:
Q5 Elizabeth’s exercise
Elizabeth visits her friend Andrew and then returns home by the same route. She
always walks 4 kilometers per hour (km/h) when walking uphill, 8 km/h when walking
downhill, and 6 km/h when walking on level ground. Suppose the path from
Elizabeth’s home to Andrew’s home consists of ‘x’ meter in the level ground, ‘y’
meter in the uphill, ‘z’ meter in the downhill and Elizabeth starts from home by 6 a.m.
Write an algorithm and the subsequent Python code to determine when Elizabeth will
reach Andrew’s home and when she will reach her home back if she spends ‘m1’
minutes in Andrew’s home. For example, if x is 1000, ‘y’ is 500, ‘z’ is 300 and m1 is
’30’ minutes, then Elizabeth takes 38 min to reach Andrew’s home so she will reach
Andrew’s home by 6 hour 21 min. Elizabeth will take 19 min to walk from Andrew’s
home to her home and time when will reach her home back is 7 hour 10 min.
The hour can be expressed in 12-hour format (not 24-hour format). The minutes
elapsed should be rounded down to the nearest integer.
Input Format
Next line contains the value for ‘ml’ minutes spent at Andrew’s home
Output Format
Print time by which Elizabeth will reach Andrew’s home. Print hours and minutes
separated by a space
Print time by which Elizabeth will reach back her home. Print hours and minutes
separated by a space
INPUT
x- Distance on level ground
y- Distance uphill
z- Distance downhill
m- Minutes spent at Andrew’s home
PROCESSING
Using the basic time speed distance formula, we calculate the time spent in travelling
to and fro between the house and the destination.
OUTPUT
Time at which she reaches Andrew’s home
PSEUDOCODE
BEGIN
Read x, y, z and m
t=(x/6+y/8+z/4)*(3/50)
tx=(x/6+y/4+z/8)*(3/50)
print(int(6+(t//60)),int((t)%60))
print(int(6+((t+tx+m)//60)),(int(t)+int(tx)+m)%60)
END
Question 1 (Letter Identification)
Given three words, write an algorithm and the subsequent Python code to identify
the following letters:
Hint: Use sets in Python. While reading input, use rstrip() function to remove extra
spaces
Input Format
Output Format
List of letters common to first two words but not in third word in lexicographical order
List of letters in first word but not in second or third word in lexicographical order
INPUT
First line contains word1
List of letters common to first two words but not in third word in lexicographical order
List of letters in first word but not in second or third word in lexicographical order
Solution
s1=set(input().rstrip())
s2=set(input().rstrip())
s3=set(input().rstrip())
s=list(s1&s2&s3)
s.sort()
print(s)
s=list((s1&s2)-s3)
s.sort()
print(s)
s=list(s1-(s2|s3))
s.sort()
print(s)
s=list(s1|s2|s3)
s.sort()
print(s)
Question 2 (Minimum distance points)
Given ‘n’ points in an X-Y plane , write an algorithm and the subsequent Python code
to determine the pair of points that are closer. Distance between two points (x1, y1)
and (x2, y2) is determined using the formula.
Consider only two decimal places of distance for comparison. When there are
multiple points with the same minimum distance print all points.
Input Format
Number of points
x coordinate of point1
y coordinate of point1
x coordinate of point2
y coordinate of point2
Output format
INPUT
n, number of points
x coordinates of n points
y coordinates of n points
PROCESSING
Using a pair of nested for loops, we find the distance between all different points and
find the points with the least distance between them.
OUTPUT
Points with least seperation.
Solution
n=int(input())
l=[]
min=999
for i in range(n) :
x=int(input())
y=int(input())
l.append((x,y))
for i in range(0,n-1) :
for j in range(i+1,n) :
x1,y1=l[i]
x2,y2=l[j]
d=(((x2-x1)**2)+((y2-y1)**2))**(0.5)
if d < min :
min=d
m=[]
m.append((x1,y1))
m.append((x2,y2))
elif d==min :
m.append((x1,y1))
m.append((x2,y2))
for i in m :
print(i)
{‘a’: 1,
‘are’: 1,
‘assets’: 1,
‘bankable’: 1,
‘demand’: 1,
‘in’: 1,
‘is’: 1,
‘programming’: 2,
‘skill’: 1,
‘skills’: 1,
‘your’: 1}
pprint(dictionary name)
Include the line “from pprint import pprint” in the top of your program]
Input format:
enter a paragraph
Output format:
Boundary conditions:
INPUT
enter a paragraph
PROCESSING
OUTPUT
Dictionary of words in the passage and their count
Solution
import pprint
w=str(input())
l=w.split()
t=[]
d={}
f=1
for i in l :
k=''
for j in i :
k+=j.lower()
t.append(k)
for i in t :
if i.isalpha() :
if i in d :
d[i]+=1
else :
d[i]=1
elif i[:len(i)-1].isalpha() :
i=i[:len(i)-1]
if i in d :
d[i]+=1
else :
d[i]=1
else :
f=0
break
if f==1 :
pprint.pprint(d)
else :
print('Invalid input')
Print dictionary in sorted order by key. Sort the values of each key in the inverted
dictionary.
pprint(dictionary name)
Include the line “from pprint import pprint” in the top of your program
Check for boundary conditions and print ‘Invalid input’ if conditions are not met.
Input Format:
Output Format:
Dictionary of characters in the word as keys and count as values in sorted order by
key.
Boundary Conditions:
INPUT
First line contains the input word
PROCESSING
We find the frequency of each character in the given string using a pair of nested for
loops.
OUTPUT
Dictionary of characters in the word as keys and count as values in sorted order by
key.
Solution
import pprint
d={}
f={}
w=input()
if w.isalpha() :
for i in w :
if i.isupper() :
i=i.lower()
if i in d :
d[i]+=1
else :
d[i]=1
for i in d :
if d[i] in f :
f[d[i]].append(i)
else :
f[d[i]]=[i]
for i in f :
f[i].sort()
pprint.pprint(d)
pprint.pprint(f)
else :
print('Invalid input')
The output is a set of common board positions where both queen and rook can be
placed. The positions must be printed in sorted order, sort it by row. When rows are
same, sort it by column.
Input Format
Output Format
Common position1
Common position2
INPUT
Row and column position of rook
Row and column position of queen
PROCESSING
We check all the possible next moves of the queen and the the rook and sort and
display them.
OUTPUT
Common positions of intersection.
Solution
r={0}
r2={0}
xr=int(input())
yr=int(input())
xq=int(input())
yq=int(input())
s1=xq+yq
a=xq-yq
b=yq-xq
for i in range(1,9) :
r2.add((xr,i))
r2.add((i,yr))
r2=r2-r
r2=r2-{(xr,yr)}
q={0}
q2={0}
for i in range(1,9) :
for j in range(1,9) :
if i+j==s1 :
q2.add((i,j))
elif xq>yq :
if i-j==a :
q2.add((i,j))
elif yq>xq :
if j-i==b :
q2.add((i,j))
for i in range(1,9) :
q2.add((xq,i))
q2.add((i,yq))
q2=q2-q
q2=q2-{(xq,yq)}
a=r2&q2
c=list(a)
c.sort()
for i in c :
print(i)
Input Format:
A number ‘n’
Output Format:
Print Armstrong or Not armstrong
Boundary Conditions:
n>=100 and <=999
#note: Type the following statement to pass the code successfully.
print(‘Hello’)
Answer:
Input for the problem
A number n
Processing involved
Using modulus and integer division we extract individual digits and calculate the
summation of their cubes and compare with the original input.
Solution alternatives
We could have alternatively have written a program for any digit number.
Pseudocode (Algorithm)
Step 1: Start
Step 2: Input n
Step 3: Compute y=(n%10)^3+((n//10)%10)^3+(n//100)^3
Step 4: If y equals n
Step 4.1 : Display “Armstrong”
Step 5: Else
Step 5.1 Display “Not Armstrong”
Step 6: Stop
Flowchart:
Input Format:
First input is number of farms ‘n’
Next ‘n’ input is the amount of milk from ‘n’ farms
Output Format
Total quantity of milk in booth
Boundary Condition:
All input > 0
#note: Type the following statement to pass the code successfully.
print(‘Hello’)
Processing involved
We convert the total amount of milk from n farms into ml and add.
We then use modulus by 1000 to find the resulting ml and integer division by 1000 to
find the resulting litres
Solution alternatives
We could alternatively have added litres seperately and milliliters seperately and
then adjusted the excess ml into litres.
Pseudocode
Step 1: Start
Step 2: Input n
Step 3: Assign sum=0
Step 4: Assign i=0
Step 5: If(i<n)
Step 5.1 sum=sum+input() in ml
Step 5.2 i=i+1
Step 5.3 go to step 5
Step 6: Display( sum//1000 litres and sum%1000 ml)
Step 7: Stop
Flowchart:
3) CSE1701 AND gate (Id-1762)
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.
X Y Output
0 0 0
0 1 0
1 0 0
1 1 1
Input Format:
Read input for ‘X’ as,
0
0
1
1
0
1
0
1
Output Format:
Print output as,
0
0
0
1
Boundary Condition:
X,Y should be either 0 or 1
print(‘Hello’)
Processing involved
We multiply the two bits to get the output
Solution alternatives
This is the best solution.We could have otherwise derived else if cases from the table
given in the question and found the solution.
Pseudocode
Step 1: Start
Step 2: Input x and y
Step 3: Check if x and y are valid
Step 4: Compute z=x*y
Step 5: Display y
Step 6: Stop
Flowchart:
4) CSE1701 Minimum marks (Id-1759)
Arjun, amith and sharma are friends. Given their marks in Maths, design an
algorithm and draw a flowchart to find minimum of their marks.
Input Format:
Get 3 inputs as marks for students.
Output Format:
Print the name of the student, who secured minimum marks in maths and marks
secured
Boundary Conditions:
Marks for each student to be >=0 and <=100
print(‘Hello’)
Solution alternatives
None
Pseudocode
Step 1: Start
Step 2: Input m1, m2 , m3
Step 3: if(m1>m3 and m2>m3)
Step 3.1 assign min=m3
Step 4: otherwise if(m1>m2 and m3>m2)
Step 4.1 assign min=m2
Step 5: otherwise
Step 5.1 assign min=m1
Step 6: Display min
Step 7: Stop
Flowchart:
5) CSE1701 Health drink Festival Bonanza (Id-1761
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
Example: If one small retail shop owner buys 125 one kg health drink pack, he will
get 150 one kg packs.
Explanation : Because for the 125 packs he purchased, he gets 25 packs free.
Input format:
Number of health drink packets purchased
Output format:
Number of packets delivered
Boundary conditions :
0< complan pack <= 500
Processing involved
We calculate the number of packets the customer gets as n + n/5
Solution alternatives
None
Pseudocode
Step 1: Start
Step 2: Input n
Step 3: Compute a=n+n/5
Step 4: Display a
Step 5: Stop
Flowchart:
1) Water in a Dam
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
be always 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.
Input Format
Number of places from where water comes, n
Output Format
Total litres of water in the dam
Step 8: Stop
CODE:
n=int(input())
l=0
ml=0
for i in range(0,n):
l=l+int(input())
ml=ml+int(input())
l=l+ml//1000
ml=ml%1000
print(l)
print(ml)
Flowchart:
2) CoPrime
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).
Input Format
Number m
Number n
Output Format
Print either Coprime or Not coprime
Processing involved:
Using iterative statements, we check for any number that divides both m and n
Solution alternatives:
We could have alternatively found factors of both the numbers and compared them.
Pseudocode:
Step 1: Start
Step 4: If(i<m)
Step 5: If(f==0)
Step 7: Stop
Flowchart:
CODE:
m=int(input())
n=int(input())
t=max(m,n)
flag=0
for i in range(2,t+1):
flag=1
if(flag==1):
print("Not coprime")
else:
print("Coprime")
Better alternative:
m=int(input())
n=int(input())
flag=0
for i in range(2,int(m**.5)+1):
flag=1
if(flag==1):
print("Not coprime")
else:
print("Coprime")
3) Pattern
Input for the problem:
A number, n
Processing involved:
Using an iterational statement we print appropriate “**” in each line
Solution alternatives:
None
Pseudocode:
Step 1: Start
Step 1: Input n
Step 3: If(i<n)
Step 4: Stop
Flowchart:
CODE:
n=int(input())
for i in range(1,n+1):
print('**'*i)
4) Age
Input for the problem:
Number of days, n
Processing involved:
Seconds=n*24*60*60
Solution alternatives:
None
Pseudocode:
Step 1: Start
Step 2: Input n
Step 4: Stop
Flowchart:
CODE:
a=int(input())
print(a*24*60*60)
else:
print("Invalid input")
5) Roman
Input for the problem:
A roman letter
Processing involved:
Using conditional statements, we find and display the value of the letter.
Solution alternatives:
None
Pseudocode:
Step 1: Start
Step 2: a=input()
Step 3: If(a=='I'):
Flowchart:
CODE:
a=input()
if(a=='I'):
print("1")
elif(a=='V'):
print("5")
elif(a=='X'):
print("10")
elif(a=='L'):
print("50")
elif(a=='C'):
print("100")
elif(a=='D'):
print("500")
elif(a=='M'):
print("1000")
else:
Output Format
Digits in the number ‘n’ that divides ‘n’ in reverse order
Input for the problem:
A number, n
Processing involved:
We seperate the digits of the number using the modulus operator and check if it
divides the original number.
Solution alternatives:
None
Pseudocode:
Step 1: Start
Step 2: Input n
Step 4: If(n1>0)
Step 5: Stop
Flowchart:
CODE:
a=int(input())
a1=a
flag=0
while(a1>0):
if(a%(a1%10)==0):
print(a1%10)
flag=1
a1=a1//10
if(flag==0):
print("No factors")