Tuesday 15 August 2017

PPS 4

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

Practice Problems 4 Fall 17

1) CSE1701 Finding a Friend with Longest Name (Id-1994)
Write an algorithm and the subsequent Python program to store the names of your friends, count the number of friends, identify the longest name and the number of letters in the longest name. Get the names of friends till 'stop' is entered. For example, if the input sequence is Ravi, Raj, Puela, stop,  then the output is 3, Puela and 5.
When you are coding in the online judge (SkillRack), use rstrip() function to remove carriage return from the input string.
Input Format:
First line is the name of  the first friend
Second line is the  name of the second friend
Third line is the  name of the third friend
….
stop
Output Format:
Number of friends
Friend’s name with longest length
Number of characters in that longest name
Code:(Verified👍)
list_of_names=[]
name=input().rstrip()
while(name!="Stop"):
list_of_names.append(name)
name=input().rstrip()
print(len(list_of_names))
temp=""
m=0
for name in list_of_names:
if(len(name)>m):
m=(len(name))
temp=name
print(temp)
print(m)

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

2) CSE1701 Polynomial Addition (Id-1993)
Write an algorithm and the subsequent Python program to add the two given polynomials. Assume that the coefficients of each polynomial are stored in a separate list.
Eg: 4x+ 3x + 1 can be stored as [4,0,3,1]
2x2 - 3x - 4 can be stored as [2,-3,-4]
Output is [4, 2, 0, -3]
Input Format:
First line contains the degree of the first polynomial
Next line contains the coefficient of xn
Next line contains the coefficient of xn-1
...
Next line contains the coefficient of x0
Next line contains the degree of the second equation
Next line contains the coefficient of xn
Next line contains the coefficient of xn-1
...
Next line contains the coefficient of x0
Output Format:
Coefficients as a list from the highest degree to the lowest degree

Code:(Verified👍)
d=int(input())
p1=[]
p2=[]
for i in range(d+1):
c=int(input())
p1=[c]+p1    #first element is the coefficient of x^0
d=int(input())
for i in range(d+1):
c=int(input())
p2=[c]+p2    #first element is the coefficient of x^0
l1=len(p1)
l2=len(p2)
greater_degree=l2;  #let
pf=[]  #final list with the sum of coefficients


#Reversing the list
p1.reverse()   #first element is the coefficient of x^n
p2.reverse()   #first element is the coefficient of x^n


if(l1>l2):
greater_degree=l1;
for i in range(l1-l2):
p2=[0]+p2
else:
for i in range(l2-l1):
p1=[0]+p1
for i in range(greater_degree):
pf.append(p1[i]+p2[i])
print(pf)

3) CSE1701 Creating Unique List and Searching (Id-1996)
A login register is maintained in the library of VITCC in which, the register number of students are recorded when they enter the library. Sometimes it happens that the students visit the library more than once in a day and hence duplicate entries occur so frequently in the register. The librarian wants to have a report of all students who have visited on a particular day, ‘x’. Given the list  of students who visited the library on the day ‘x’, write an algorithm and the subsequent Python program to prepare a report with unique register number of students. Also read a register number ‘r’ and search for it in the list. Print ‘Found’ if ‘r’ is in the list and print ‘Not found’ otherwise.
Input Format:
First line contains the number of students visited, ‘n’
Second line contains the register number of first entry
Third line contains the register number of second entry
. . .
N+1th line contains the register number of n-th entry
Next line contains the register number  ‘r’ that has to be searched
Output Format:
A list without duplicate entries
Print either ‘Found’ or ‘Not found’
Boundary Conditions:

Number of students visited >=0
Code:(Verified👍)
n=int(input())
p=[]
for i in range(n):
    reg=input().rstrip().upper()
    if(reg not in p):
        p.append(reg)
r=input().rstrip().upper()
print(p)
if(r in p):
 print('Found')
else:
 print('Not found')

4) CSE1701 Arrangement of Plants (Id-1997)
A gardener has the practice of assigning ID to the plants during plantation. One day, he makes a note of the heights of plants in his garden. He writes the height of the plant against the ID of the plant. He then instructs his employee to keep the plants, in ascending order of its height. Design an algorithm and write the Python code to display the list of ID numbers of plants in ascending order of their height. IDs are also numbers. Check for boundary conditions and print 'Invalid input' for wrong input. For example, if there are three trees with IDs as 175, 160, 120 and height as 47, 73 and 23 then the output should be [120, 175, 160].
Input Format:
First line contains the number of plants, n
Next line contains the ID of the plant-1
Next line contains the height of plant-1
. . .
Next line contains the ID of the plant-n
Next line contains the height of plantn
Output Format:
IDs sorted according to height. Print one id in one line
Boundary Conditions:
All inputs >=0

Code:(Verified👍)
n=int(input())
ids=[]
heights=[]
for i in range(n):
ids.append(int(input()))
heights.append(int(input()))
sorted_ids=[x for (y,x) in sorted(zip(heights,ids))]
#The zip() function returns an iterator of tuples based on the iterable object.
for i in sorted_ids:
print(i)

5) CSE1701 Sparse Matrix (Id-1995)
Write an algorithm and the subsequent Python program to check whether the given matrix is sparse or not. A matrix is said to be a “Sparse” if the number  of zero entries  in the matrix,  is greater than or equal to the number  of non-zero entries. Otherwise it is  “Not sparse”. Check for boundary conditions and print 'Invalid input' when not satisfied.

Input Format:
The first line will contain the number of rows in the matrix, rows
The Second line will contain the number of columns in the matrix, cols
The next (rows * cols) lines will contain the elements of the matrix, entered row wise
Output Format:
Print either Sparse or Not sparse
Boundary Conditions:
Number of rows and columns > 0

Code:(Verified👍)
r=int(input())  #number of rows in matrix
c=int(input())  #number of columns in matrix
if(r<=0 or c<=0):
print('Invalid input')
exit
else:
vals=[]
flag=0
for i in range(r*c):
vals.append(int(input()))
zero_entries=vals.count(0)
if(zero_entries>=r*c-zero_entries):
print('Sparse')
else:
print('Not sparse')
******************************************************************************************
THANK YOU FOR VISITING...
Best of luck. 👍
******************************************************************************************


11 comments:

  1. Replies
    1. PPS 5 is not yet accessible on Skillrack. It will open up on 26-Aug

      Delete
    2. 2nd question of pps4 polynomial type is not working

      Delete
    3. Please check the code properly. It is correct and has been verified.

      Delete
  2. Polynomial question is not working at second last line

    ReplyDelete
    Replies
    1. Please check the code properly. It is correct and has been verified.

      Delete
  3. SKILL COPY NOT WORKING!!!!!!!

    ReplyDelete
  4. I am getting EOF error for reading any string

    ReplyDelete
  5. In 1 st question i am getting EOF error

    ReplyDelete