0% found this document useful (0 votes)
3 views

Assignment 3

This document outlines a practice assignment for an Introduction to Computer Science course at the German University in Cairo for the Winter Semester 2023-2024. It includes various exercises related to Python programming, covering topics such as conditional statements, algorithms for calculating values based on user input, and debugging existing code. The exercises range from simple tasks like tracing variable values to more complex problems involving calculations and decision-making based on user inputs.

Uploaded by

yusuf tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 3

This document outlines a practice assignment for an Introduction to Computer Science course at the German University in Cairo for the Winter Semester 2023-2024. It includes various exercises related to Python programming, covering topics such as conditional statements, algorithms for calculating values based on user input, and debugging existing code. The exercises range from simple tasks like tracing variable values to more complex problems involving calculations and decision-making based on user inputs.

Uploaded by

yusuf tarek
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

German University in Cairo

Media Engineering and Technology Faculty


Prof. Dr. Slim Abdennadher
Assoc. Prof. Milad Ghantous
Dr. Nourhan Ehab

Introduction to Computer Science, Winter Semester 2023-2024


Practice Assignment 3
Discussion: 29.10.2023 - 02.11.2023

To be discussed in Tutorials:
Exercise 3-1 Tracing
a) Assign a value to x so that the character "A" is printed out:

x = _______________________
if x%2==0 and x%3==1:
print("A")

b) Assign values to x and y so that the character "D" is printed out. Hint: The values could be floating-
point numbers.

x = ____________________
y = ____________________
if not ((0<=x<=3) and (0<=y<=3)):
print("A")
elif y<=1 or y>=2:
print("B")
elif x<=1 or x>=2:
print("C")
else:
print("D")

Exercise 3-2 Turkey cooking


The following Python program displays the cooking time (in minutes) for a turkey of a given weight (in
pounds) that may or may not be stuffed, according to the times in the following table:

Weight of turkey Cooking time when not stuffed


under 14 pounds 195 minutes
14 to 20 poundsinclusive 240 minutes
over 20 pounds 270 minutes

It adds 30 minutes to the cooking time if the turkey has been stuffed.

weight = eval(input())
stuffed = input()
if weight < 14:
result = 195
if weight <= 20:
result = 240
else:
result = 270
if stuffed:
result = result + 30
print(result)

1
The program should work for any input. You are asked to check whether the program is correct or not.
If it is not correct, you should state why it is incorrect and re-write the program with the incorrect
statement(s) fixed.

Exercise 3-3
divisible
Write a Python program that given a number (product) from the user and checks if the number is divisible
by 7 and prints the multiples of that number. If the user enters a negative number, print "Try again."
Use conditional operators only.

Example 1:
Enter a number: 15
15 is not divisible by 7.

Example 2:
Enter a number: 35
This is the product of 7*5.

Example 3:
Enter a number: -2
Try again.

Exercise 3-4 Suitcases’ weight


Consider an airport boarding counter. Each passenger carries two suitcases with no extra charge if the
weight is 23 Kg, or less, per each. The passenger will have to pay an extra charge of 50 LE per extra Kg
for extra weight, up to a maximum weight of 32Kg per suitcase. If a suitcase weight exceeds 32Kg, the
suitcase is rejected. Write a Python program that takes as input weights of two suitcases and prints out
the weight charge. For Example:

• If the first suitcase has a weight of 27Kg and the second Suitcase has a weight of
25Kg,then the program should display:
Suitcase 1 accepted
Suitcase 2 accepted
Total charge = 300 LE

• If the first suitcase has a weight of 45Kg and the second Suitcase has a weight of
20Kg, then the program should display:
Suitcase 1 rejected
Suitcase 2 accepted
Total charge = 0 LE

• If the first suitcase has a weight of 25Kg and the second Suitcase has a weight of
40Kg, then the program should display:
Suitcase 1 accepted
Suitcase 2 rejected
Total charge = 100 LE

• If the first suitcase has a weight of 25Kg and the second Suitcase has a weight of
18Kg, then the program should display:
Suitcase 1 accepted
Suitcase 2 accepted
Total charge = 100 LE

Exercise 3-5 Mysterious Task

2
• What does the following program display for any boolean values x, y and z? Choose one answer
from the below choices and Justify.

x = eval(input())
y = eval(input())
z = eval(input())
if (x and y): #if (x == True and y == True)
print((not x) or z)
else:
print((x and y) or z)

a) The value of x
b) The value of y
c) The value of z
d) The value of x and y
e) Always true
f) True if either x and y are both True or z is True, and False otherwise.

• Consider a program which changes the value of a boolean variable that tracks whether a light is on
or off. Three people, creatively named Sarah, Ali, and Mina, who are claiming to be expert light
switchers have written different implementations of this function.

Sarah’s Version:

lightOn = eval(input())
if (lightOn):
lightOn = False
else:
lightOn = True

Ali’s Version:

lightOn = eval(input())
if (lightOn):
lightOn = False
if (not lightOn):
lightOn = True

Minas’s Version:

lightOn = eval(input())
lightOn = not lightOn

Are these implementations equivalent, i.e. performing the same task? Are all three people expert
light switchers? Why? Justify your answer.

To be solved in Labs:

Exercise 3-6 Discount


A discount is made on a purchase as follows:

• if purchase ≤ 1000 L.E., there is no discount


• if purchase > 1000 L.E., the discount is 5%

3
Given the cost of the purchase, write an algorithm to calculate and print the money paid taking into
consideration the 10% sales taxes. The taxes are calculated on the amount after the discount.

Exercise 3-7 Dog age


For the first two years, a dog year is equal to 10.5 human years. After that, each dog year is equal to 4
human years. Write a Python program to calculate a dog’s age in human years, given a dog’s age in dog
years. Expected output should look like the following:

• Input a dog’s age in dog years: 2 The dog’s age in human years is 21
• Input a dog’s age in dog years: 15 The dog’s age in human years is 73
• Input a dog’s age in dog years: -5 Age must be positive number.

Exercise 3-8 Months


Write a Python program that given a month of the year and displays the number of the days Solve
using conditional operator only.

Exercise 3-9 Triangle


Write an algorithm that determines whether A, B, and C can form the sides of a triangle. Note A, B
and C can form the sides of a triangle if each side is less than the sum of the two other sides, i.e.:
A < B + C, B < A + C and C < A + B .
If A, B, and C forms a triangle, calculate its area using the formula:
p
Area = S(S − A)(S − B)(S − C) , where S = (A + B + C)/2

Exercise 3-10 Gas Station


National Last Chance gas station sits on Cairo Alexandria route. There is no other gas station for 200
Miles. Write an algorithm to help drivers decide if they need gas or not. The user will enter:

• The capacity of the gas tank, in Gallons.


• The indication of the gas gauge in percent (full=100, three quarters=75 and so on).
• The miles per gallon of the car.

The following is a sample run of the algorithm:

Tank Capacity: 12
Gas Gauge Reading in percent: 50
Miles per Gallon: 30

Get Gas!

The algorithm should print out Get gas or Safe to proceed depending on if the car can cross the 200
miles with the gas remaining in the tank.
Extra Exercises:

Exercise 3-11 BMI


Write an algorithm that determines whether you are underweight, fit, or overweight given your weight
and height based on your BMI calculation.

4
The BMI is calculated using the weight divided by height squared, where weight is in kg and height is in
meters.

• If BMI ≤ 18.5, you are underweight


• If BMI > 18.5 and BMI ≤ 25, you are fit

• If BMI > 25, you are overweight

Exercise 3-12 Maximum


Write a Python program to calculate the maximum of three numbers. Solve using conditional oper-
ator only.

Exercise 3-13 Letter Grades


Students marks in a class are graded on the following policy:

• A: 85-100
• B: 74-85
• C: 60-74
• D: 50-60

• F: <50

Keeping in mind that a student cannot score more than 105 marks, nor less than 0 marks.
Write an algorithm that reads each student’s marks, print either a grade or an error message.

Exercise 3-14 Student School


The following algorithm prints out whether a current student is in elementary (1st - 5th), middle (6th -
8th), or high school (9th - 12th).

grade = eval(input())
if(grade <= 5):
print("this student is in elementary school")
elif(grade <= 8):
print("this student is in middle school")
elif(grade <= 12):
print("this student is in high school")

The algorithm above uses nested if-statements.

a) Write an equivalent algorithm that will print the same messages as the algorithm above without
using any nested if-statements.
b) Discuss the drawback of your algorithm? Hint: Compare the efficiency of both algorithms.

Exercise 3-15 Balance


Consider the following algorithm:

5
balance = eval(input())
if((balance >= 2000) or (balance <= 3000)):
print("Your balance is between 2000$ and 3000$")
print("Your interest rate will be 3.5%")
else:
print("Your balance is larger than 3000$")
print("Your interest rate will be 4.5%")

a) Do you think this algorithm does what the programmer intended? Justify your answer.
b) If the algorithm does not do what the programmer intends, improve this algorithm.

You might also like