Computer Science Practice Assignement (3) Solution
Computer Science Practice Assignement (3) Solution
Solution:
x = eval(input())
y = eval(input())
tmp = x
x = y
y = tmp
print("The new value of x is ", x, " and the new value of y is ", y)
• Swap the two numbers without using a temporary variable. Is it always possible to swap any two
values of any types?
Solution:
x = eval(input())
y = eval(input())
y = y + x
x = y - x
y = y - x
print("The new value of x is ", x, " and the new value of y is ", y)
Solution:
1
import random
dice1 = random.randint(1,99)
dice2 = random.randint(1,99)
sum =0
if(dice1 <10):
sum +=dice1
else:
sum += dice1%10 + dice1//10
if(dice2 <10):
sum +=dice2
else:
sum += dice2%10 + dice2//10
if sum%2 ==0:
print("You win!")
else:
print("You lose! try again")
16 17 12 -> 50
12 13 14 -> 30
6 4 4 -> 10
Solution:
a = eval(input())
b = eval(input())
c = eval(input())
aa = a % 10
bb = b % 10
cc = c % 10
2
if(cc >= 5):
c = (10 + c) - cc
else:
c = c - cc
sum = a + b + c
print(sum)
Exercise 3-4
To be discussed in Tutorial
Your task is to implement an algorithm that can calculate your maximum heart rate and your optimal
training pulse for fat-burning, endurance increase or cardiovascular system improvement. The Formula
for calculating the maximum heart rate (Pulse) depends on the age and the gender:
• Health zone: This amounts to 50-60% of the maximum heart rate. Within this pulse range
particularly the cardiovascular system will be invigorated. This range is particularly suitable for
beginners.
• Fat burning zone: This amounts to 60-70% of the maximum heart rate. Within this pulse range,
most calories from fat are burned. Furthermore the cardiovascular system will be trained.
• Aerobic zone: This amounts to 70-80% of the maximum heart rate. Within this pulse range,
carbohydrates and fats are burned for power production in the muscle cells. This range requires
the cardiovascular system as well as the lung and the metabolism.
• Anaerobic zone: This amounts to 80-90% of the maximum heart rate. Within this pulse range,
the body cannot cover the oxygen demand any longer. This range is for the development of power
and muscle mass.
• Red zone: This amounts to 90-100% of the maximum heart rate. This pulse range should be
handled with caution. It is dangerous for beginners and can be harmful for the heart.
a) Write an algorithmthat given the age and the gender should display the different zones. For example,
your algorithm should display the following for a 45 years old man:
Solution:
age = eval(input())
gender = eval(input())
if(gender == "male"):
maxHeartRate = 220 - age
else:
maxHeartRate = 226 - age
3
healthZoneTo = 60 * maxHeartRate / 100
print("Health Zone: Between ", healthZoneFrom, " and ", healthZoneTo)
b) Write an algorithm that given the age, the gender and the heart rate will display the corresponding
zone. For example, the algorithm should display for a 45 years old man and heart rate of 190, the
following message
Red Zone
Your algorithm should consist of only if then statements, i.e. you are not allowed to use any else
statements.
Solution:
age = eval(input())
gender = eval(input())
heartRate = eval(input())
if(gender == "male"):
maxHeartRate = 220 - age
if(gender == "female"):
maxHeartRate = 226 - age
4
Exercise 3-5 Discount
To be solved in Lab
A discount is made on a purchase as follows:
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.
Solution:
purchase = eval(input())
if(purchase <= 1000):
print(" There is no discount! ")
else:
discount = purchase * 0.05
purchase = purchase - discount
• Input a dog’s age in human years: 2 The dog’s age in dog years is 21
• Input a dog’s age in human years: 15 The dog’s age in dog years is 73
• Input a dog’s age in human years: -5 Age must be positive number.
Solution:
5
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.
Solution:
Solution:
BMI = weight/(height**2)
BMI = weight/(height**2)
6
print("You are underweight")
else:
if(BMI <= 25):
print("You are fit")
else:
print("You are overweight")
Solution:
•
import math
A,B,C = eval(input()), eval(input()), eval(input())
•
import math
A, B, C = eval(input()), eval(input()), eval(input())
triangle = 0
if(A < B + C):
if(B < C+ A):
if(C < A + B):
triangle = 1
if(triangle == 0):
print("This cannot be a triangle!")
else:
S = ((A + B+ C) / 2)
Area = (math.sqrt(S * (S - A) * ( S - B) * (S - C)))
print(" This is a triangle with the Area: ")
print(Area)
7
•
import math
A, B, C = eval(input()), eval(input()), eval(input())
• 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.
Solution:
• Mark = eval(input())
if(Mark < 0):
print("invalid mark, less than 0")
else:
if(Mark >105):
print("invalid mark, greater than 105")
else:
if(Mark <50):
print("grade is F")
else:
if(Mark <60):
print("grade is D")
else:
if(Mark <74):
print("grade is C")
else:
if(Mark <85):
print("grade is B")
else:
print("grade is A")
• Mark = eval(input())
if(Mark < 0):
print("invalid mark, less than 0")
elif(Mark >100):
print("invalid mark, greater than 105")
8
elif(Mark <50):
print("grade is F")
elif(Mark <60):
print("grade is D")
elif(Mark <74):
print("grade is C")
elif(Mark <85):
print("grade is B")
else:
print("grade is A")
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")
a) Write an equivalent algorithm that will print the same messages as the algorithm above without
using any nested if-statements.
Solution:
grade = eval(input())
if(grade <= 5):
print("this student is in elementary school")
b) Discuss the drawback of your algorithm? Hint: Compare the efficiency of both algorithms.
Solution:
The first algorithm is more efficient than the second one since in the second algorithms all conditions
should be checked. In the first algorithm the number of conditions that should be checked depend
on the input.