You are on page 1of 4

Qatar University - College of Engineering - Dept.

of Computer Science & Engineering

Programming Concepts - CMPS 151 – Spring2022


Lab Exam
Name Grade

100

QUID Section

Instructions » Read carefully


1) The exam duration is 1 hour and 50 minutes. So, read the exam questions
carefully and plan your time accordingly.
2) Solve the questions using Visual Studio Code by creating a .py file and
writing your code in it.
3) Copy your .py programs (source code) to a new folder. And name your
folder as follows: (Your Name-id). Compress (zip) your folder and upload it
to the exam link on blackboard.
4) Save regularly to avoid unpleasant surprises, as a computer hang or
program shutdown L.
5) You will receive 0 points in case of plagiarism.

1|Page
Question 1:

Write a loop that calculates the total of the following series of numbers:
2 4 6 100
+ + + ⋯ +
1 3 5 99
total = 0
for i in range(1,101):
if i%2==0:
j = i-1
total+=(j/i)
print("Total=",total)

22 marks

Question 2:

Write a function number_split that receives a list of numbers and returns two
lists: one includes only float numbers (without zero) and the other one includes
only integer numbers.

Hint: A float is a number between 0 and 1.

• Test the number_split function in a main program using the following list:
[0.3,4,0,0.5,22,0.1].
• Display the output (i.e., print the list of float and integer numbers) in your
main program.
def number_split(numbers):
floats = []
integers = []
for i in numbers:
if 0 < i < 1:
floats.append(i)
else:
integers.append(i)
return floats, integers

def main():

2|Page
numbers = [4,0.6,0,23,0.2,0.1]

f,i = number_split(numbers)
print("Floats:",f)
print("Integers:",i)
main()

26 marks

Question 3:

Write a program with a function digitAdder that takes a string as an argument,


searches for single digits in the string, then adds the digits and return the total. In
a main function, ask the user to enter a series of characters and single-digit
numbers with nothing separating them. The program should display the sum of all
the single digit numbers in the string. For example, if the user enters
hello25there14, the method should return 12, which is the sum of 2, 5, 1, and 4.
Hint: You can use the built-in function isdigit
def digitAdder(string):
total = 0
for ch in string:
if ch.isdigit():
total+=int(ch)
return total
def main():
s = input("Enter a series of characters: ")
total = digitAdder(s)
print("Total=",total)
main()

26 marks

3|Page
Question 4:

The file TV.txt contains data about movies; every movie has title, year, and rate.
Each of this data is on separate line as follows:
title(string)\nyear(integer)\nrating(float)

Write a python program that performs the following tasks:


• Write a function called process_list(file_name) that reads the TV show
ratings into a list called ratings.
• Print the average rating of all the TV shows in the file.
• Print the average rating of all TV shows that aired in 2020.
• Print the name of the TV show with the highest rating from the file.

def process_list(file_name):
inFile = open(file_name,'r',encoding='utf-8')
title = inFile.readline().strip()
ratings = []
ratings2020 = []
maxRating = 0
maxTitle = ''
while title != '':
year = int(inFile.readline().strip())
rating = float(inFile.readline().strip())
if year == 2020:
ratings2020.append(rating)
ratings.append(rating)
if rating > maxRating:
maxRating = rating
maxTitle = title
title = inFile.readline().strip()
inFile.close()
print("Average rating of all TV shows:", sum(ratings)/len(ratings))
print("Average ratings of TV shows from 2020:",sum(ratings2020)/len(ratings2020))
print("Title of TV show with the highest rating:",maxTitle)
process_list("TV.txt")

26 marks

Don’t forget to upload your zip file on blackboard and submit your exam paper
4|Page

You might also like