Вы находитесь на странице: 1из 6

University of Mauritius Faculty of Engineering

Department of Computer Science & Eng


CSE 1003 Computer Programming
Tutorial 2 -2011/2012

Question 2 From Test May 2010 (a) Identify and correct the errors in the following piece of code:
Mychoice = input(Please enter your choice) while mychoice= y if mychoice!= y break mychoice = input(Please enter your choice)

[3 marks] Consider the program shown below:


#nested_loop.py # x = 1 more_integers = "y" while more_integers == "y": a,b,c = eval(input ("Enter more integers : ")) sum = a + b + c while sum > 5: if b > a: x = b**c else: x = c**b print x sum = sum - 1 b = b - 1 more_integers = input("More integers? (y/n):")

Predict the output for each of the following sets of input for a,b,c : (i) (ii) (iii) 1,2,3 [1.5 marks] 3,2,1 [1.5 marks] 3,2,2 [2 marks]

Page | 1

Question 3 From Exam May 2010 Q1 (a) Using proper examples, explain the difference between pre-test loops and post-test loops in Python. [4 marks]
(b)

Consider the piece of code below and briefly describe what it is doing. n = eval(input(Please enter a whole number: )) factorial = 1 factor = 1 while factor <= n : factorial = factorial * factor print (The factorial of , n, is , factorial) [4 marks] Predict the output of the following program assuming you have the following values to be used as input to the program: 3, 18, -1, 0, 6, 100, 0 x=1 while 1: if x==0: break else: if x<0: print (No computation will be done!) elif x<10: print (Correct value) else: print (Value too large) x = eval(input(Enter a new value for x)) [5 marks]

(c)

(d)

Write a program which asks the user to input 2 values, m and n. The program should then ask the user to enter the details for m modules. For each module, the details that should be entered are the module name and marks for n tests. After entering the details for each module, the total marks per module and the average test marks per module should be displayed. The program should also include proper validation for m and n. [10 marks]

Question 4 From Nov 2010 Q5


(a.) Write a program which allows the input of a positive integer N and displays all the integers from N to 0 inclusive. The program should make use of a while loop. Page | 2

Given that the user enters 5 as value of N, the display should be 5,4,3,2,1,0 [3 marks] (b.) Write a program which allows the user to continuously enter positive numbers and finally prints the sum and average of all the numbers. Use a while loop with an appropriate sentinel value. [5 marks] (c.) Consider the following code and predict the output given that the user inputs the value 5 for x. x=eval(input("Enter an integer ") ) for i in range(1,x): for j in range(i): print ("*", ) print [5 marks]

(d.)

The flowchart for the program to calculate factorial of a number is given below. (i) Fill in the missing statements in blocks A and B. [2 marks] (ii) Convert the design to a program. [5 marks]

Page | 3

Start

i=1,factorial=1 Input n

False

A
True

Output factorial

B
End

n=n+1

Note: The Factorial of a number is the product of all integers between 1 up to and including the number.

Question 5 From Nov 2010 Q6:


(a) Dry run the following code for variables listA, listB, i, listA[i], listB[i], listC, using an appropriate trace table: listA= [10, 20, 30] listB= [5, 10, 15, 5, 20] listC=[] for i in range(1,3): listA.append(i*2) listB.remove(5) listB.insert(0,4) for i in range(5): listC.append(listA[i] +listB[i]) print (listC)

Page | 4

The header row and the first data row of the trace table are given.

listA [10, 20, 30]

listB [5, 10, 15, 5, 20]

i 1

listA[i] 20

listB[i] 10

listC []

[8 marks]

(b)Write a program to perform the following:

Input elements to two lists of equal size : listA and listB. The size should also be input by the user. If corresponding elements of both listA and listB are even then sum up both corresponding elements and store in a new list listC.

For example, given listA= [2, 4, 6, 5] and listB = [4, 5, 8, 10] then listC=[6, 14] [7 marks]

(c) Write a function input_List with three parameter, namely: a list, number of

rows and number of columns, to input integer values to a twodimensional list. [5 marks]

Question 6 From Nov 2010 Q7:


(a) Given that a reverse-acronym can be formed by taking the first letters of

the first three words in a phrase and making a word from them, in the reverse order. For example, SSC is the acronym for computer science support unit. Write a program that allows the user to type in a phrase and outputs the reverse-acronym for that phrase. Note that the output should be all uppercase, even if the words in the phrase are not capitalized.
[7 marks] Page | 5

(b) Write a function, addAsterix, that takes as parameter a string, adds an

asterix (*) after each two characters and returns the resulting string. Example: If the string testing is passed as parameter to addAsterix, the function must return the string te*st*in*g.
[5 marks]

(c) Write a program that continuously reads a series of words entered by the user, terminated by an empty string. The program must convert each word to its equivalent numerical value and append it to the file myFile.txt, which is initially empty, one number per line. The numerical value of a word is obtained by summing up the ordinal codes (ASCII values) of the letter in the word. Example: the numerical value of hi will be 209 (i.e. 104 + 105). The following is a sample input and output:
Sample input: hi how bye Sample output: myFile.t xt 209 334 320 [8 marks]

Question 7 Write a Python program that reads in a sequence of numbers terminated by zero. Your program must store all even numbers read into a file even.txt and all odd numbers entered in a file odd.txt (each on a separate line) and display which file has more lines as well as the sum of those numbers. [6 marks]

Page | 6