0% found this document useful (0 votes)
22 views4 pages

ProgrammingConcepts Exam Makeup Solution

The document provides instructions for a programming concepts exam. It contains 4 questions that involve writing Python functions and code to calculate totals, split lists, multiply digits in strings, and process data from a file. Students are instructed to write code for each question in a Python file and upload their work.

Uploaded by

Emad Abdelkader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

ProgrammingConcepts Exam Makeup Solution

The document provides instructions for a programming concepts exam. It contains 4 questions that involve writing Python functions and code to calculate totals, split lists, multiply digits in strings, and process data from a file. Students are instructed to write code for each question in a Python file and upload their work.

Uploaded by

Emad Abdelkader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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:

5 6 7 50
+ + + ⋯ +
50 49 48 5
total = 0
j = 50
for i in range(5,51):
total+=(i/j)
j-=1
print("Total=",total)

22 marks

Question 2:

Write a function unit_split that receives a list of numbers and returns two lists:
one includes only tenths (numbers from 0 to 99 ,including both numbers) and the
other one includes only hundreds (numbers from 100 to 999, including both
numbers).

• Test the unit_split function in a main program using the following list:
[5,200,60,342,192,0,15,999]
• Display the output (i.e., print the list of tenths and hundreds) in your main
program.
def unit_split(numbers):
tenths = []
hundreds = []
for i in numbers:
if 0<=i<=99:
tenths.append(i)
else:
hundreds.append(i)
return tenths, hundreds

2|Page
def main():
numbers = [5,200,60,342,192,0,15,999]
tenths,hundreds = unit_split(numbers)
print("Tenths:",tenths)
print("Hundreds:",hundreds)
main()

26 marks
Question 3:

Write a program with a function digitMultiplier that takes a string as an


argument, searches for single digits in the string (except zero), then multiplies the
digits and return the multiplication. 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 multiplication of all the single digit numbers in the
string. For example, if the user enters 0hello25there14, the method should return
40, which is the multiplication of 2, 5, 1, and 4.

Hint: You can use the built-in function isdigit


def digitMultiplier(string):
p = 1
for ch in string:
if ch.isdigit() and int(ch) != 0:
p*=int(ch)
return p

def main():
string = input("Enter a string:")
result = digitMultiplier(string)
print(result)
main()

26 marks

3|Page
Question 4:

The file “cars.txt” contains cars’ records where every car model is followed by it’s
horsepower and price as follows:
model(string)\nhorsepower(integer)\nprice(integer)

Write a python program that performs the following tasks:


• Write a function called process_list(file_name) that reads the car prices
into a list called prices.
• Print the average price of all the cars in the file.
• Print the average price for all toyota cars in the file.
• Print the average price for all cars with horsepower > 100.
def process_list(file_name):
inFile = open(file_name,"r")
model = inFile.readline().strip()
prices = []
priceToyota = []
price100 = []
while model != '':
horsepower = int(inFile.readline().strip())
price = int(inFile.readline().strip())
if horsepower > 100:
price100.append(price)
if model == 'toyota':
priceToyota.append(price)
prices.append(price)
model = inFile.readline().strip()

inFile.close()
print("Average price of all cars:",sum(prices)/len(prices))
print("Average price of all Toyota cars:",sum(priceToyota)/len(priceToyota))
print("Average price of all cars with horsepower >
100:",sum(price100)/len(price100))

process_list('cars.txt')

26 marks

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

4|Page

You might also like