Topic3 - Exercises
Topic3 - Exercises
Exercises
Topic 3: Introduction to Programming
Introduction
1. Electric current. Ohm’s law states that the current through a conductor between two points is
directly proportional to the potential difference across the two points. The mathematical equation
that describes this relationship is as follows:
V=I*R
where V is the potential difference, I is the electric current and R is the resistance.
Based on this equation, write an expression in Python that, given the values of I and R, calculates
the value of V. Additionally, write the other two expressions to calculate I and R from the other
variables. To check if your code is correct, assign values to two of the three variables and
calculate the value of the third one.
2. Free-delivery pizza.
a. Your restaurant wants to offer free-delivery pizza anywhere to any order over 100€, or
to any order lower than 100€, greater than 50€ and within 100 kilometers, or to any
customer over 65 years old. Write an expression that allows you to check whether
these restrictions are met or not.
b. Add input/output sentences to read the price of the order, the distance, and the
customer’s age, and print if the offer corresponds or not.
1
Computer Science Fundamentals
4. Currency converter.
a. Write the expressions to convert euros to dollars, pounds, and bitcoins. Find the
corresponding conversion factors on the Internet.
b. Add input/output sentences to read a value in euros and print its equivalent value in
dollars, pounds, and bitcoins. Example:
Type a value in euros: 5.2
Currency converter: 5.20 euros are 5.98 dollars, 4.63 pounds, and 0.00936
bitcoins.
Control structures
1. Tire pressure. The front tires of a car should both have the same pressure. Also, the rear tires of
a car should both have the same pressure (but not necessarily the same pressure as the front
tires).
Write a program that accepts the pressure of the four tires and prints Inflation is OK only if the
inflation of the four tires is correct.
2. Love 6. The number 6 is a truly great number. Given two integer values, a and b, print GREAT
NUMBER if either one is 6, or if their sum or difference is 6. Ask the user to enter the numbers
and assume (s)he enters positive integers.
3. Black Jack. Ask the user for two integer values greater than 0 and print the nearest one to 21
without going over. Print 0 if both of them go over. You may assume the user will always type
positive integers or 0.
4. Green ticket. You have a green lottery ticket, with integer numbers a, b, and c on it. If the
numbers are all different from each other, the result is 0. If all the numbers are the same, the
result is 20. If two of the numbers are the same, the result is 10. Ask the user to enter the
numbers (assume integers) and print the resulting value.
5. Number guess. Write a program that picks a random integer from 1 to 100. It then prompts the
user to guess the value, with hints of Too high or Too low from the program. The program
continues to run until the user guesses the integer. To get a random integer in the range from 1 to
100, use the following piece of code:
import random
2
Computer Science Fundamentals
n = random.randint(1, 100)
6. Buzz and fizz. Write a program that prints all the numbers from 1 to 100. But for multiples of 3
print Fizz instead of the number and for multiples of 5 print Buzz. For numbers that are multiples
of both 3 and 5, print FizzBuzz.
7. Write a program that prompts the user for two positive integers, a and b, and then performs the
addition of the integers in the range [a, b]. What changes would have to be made in the program
to calculate the multiplication instead of the addition?
8. Write a program that prints the integers from 1 to 100 with n integers per line. Ask the user to
enter the value of n. Assume that n is greater than 0 and 100 is divisible by n.
while n%2==0:
x = x+1
n = n/2
10. Try to guess the output you get when this program is run:
rows = 4
columns = 6
while rows!=0:
i=1
while i <= columns:
print("*", end="")
i = i+1
print(" ")
rows = rows-1
3
Computer Science Fundamentals
# PROGRAM 1 # PROGRAM 2
import random import random
Functions
1. Write a function read_natural that prompts the user to enter a natural number (i.e., positive
integer) and returns it. The function must guarantee that the number returned is natural (assume
integer).
Next, write a program that uses the function to read a natural number and print it on the screen.
2. Write a function multiple that receives two numbers (assume natural numbers) and returns a
Boolean that indicates if the first one is a multiple of the second one.
Next, write a program that uses the function to determine if two numbers typed by the user are
multiples (or not) and print a message accordingly.
3. Write a function odd_multiple5 that receives an integer number (assume positive integer) and
returns a Boolean value indicating if the number is odd and multiple of 5 at the same time.
Next, write a program that reads from the keyboard number by number (the reading of numbers
ends with a negative number), and then it uses the function to count how many of the numbers
4
Computer Science Fundamentals
entered are odd and multiple of 5. Finally, the program prints a message to indicate how many
numbers meet these conditions.
4. Write a function slope that receives a character and a number, and draws a decreasing slope
with the given character, using the number as height.
Next, write a program that uses the function to draw the slope with a character and a number
entered using the keyboard. Example for character * and number 4:
*
**
***
****
5. Write a function that receives a number and returns its Fibonacci (assume integer >=0).
Next, write a program that uses the function to print the Fibonacci number of an integer entered
by the keyboard.
Fibonacci sequence:
F0 = 0
F1 = 1
Fn = Fn-1 + Fn-2
Example for number 8:
fib(8) = 21
2. Modify the program of Exercise 1 in such a way that it replaces all the multiples of 5 with the
string 'five' and then prints the modified list.
3. Write a function th_list that receives a list and a threshold (integer number) as parameters and
returns a new list containing only the elements of the first list that are greater than the threshold.
Next, write a program that uses the function to, given a list and a threshold, print the new list.
5
Computer Science Fundamentals
4. Consider a list [string, number, string, number, string, number, …] in which each string
represents the name of children born in the current year and each number represents the
number of children with that name.
Next, write a program that, given a list such as the one mentioned above, prints the most popular
name.
5. Write a function starts_with that receives a complete name (written as "Name Surname") and a
letter as parameters, and it returns True or False depending on whether the Surname starts with
the given letter or not.
Next, write a program that, given a list of students with their names/surnames and a letter, uses
the function to print how many of them have a surname that starts with that letter.
6. Write a function transpose that receives a matrix (list of lists) as a parameter and returns its
transpose.
Next, write a program that, given a matrix, uses the function to print its transpose.
Files
Consider that we have a file in CSV format that includes a list of country names, their population,
and the number of mobile phones (real data with this information can be downloaded from
https://www.cia.gov/library/publications/the-world-factbook/). The content of the file is as follows:
COUNTRY;POPULATION;MOBILE PHONES
Argentina;41769726;51891000
Brazil;203429773;173959000
Canada;34030589;23081000
Dominican Republic;9956648;8630000
Ecuador;15007343;13635000
Faroe Islands;49267;57000
Germany;81471834;105000000
Hong Kong;7122508;12207000
Iran;77891220;52555000
Jamaica;2868380;2971000
6
Computer Science Fundamentals
Korea South;48754657;47944000
Latvia;2204708;2243000
Macau;573003;1109000
Nigeria;155215573;73099000
Oman;3027959;3971000
Pakistan;187342721;103000000
Qatar;848016;2472000
Romania;21904551;25377000
Spain;46754784;50991000
Thailand;66720153;83057000
United States;313232044;286000000
Venezuela;27635743;28124000
Zambia;13881336;4407000
Keeping the CSV format in mind, write the following functions in Python:
1. number_inhabitants(filename) receives a string (the filename) and returns a number with the
total population (sum of all countries).
a. get_inhabitants(line) receives a string (a line in the file as described before) and
returns the population field (as a number).
2. number_mobiles(filename) receives a string (the filename) and returns a number with the total
number of mobiles (sum of all countries).
a. get_mobiles(line) receives a string (a line in the file as described before) and returns
the number of mobiles field (as a number).
3. average_mobiles(filename) receives a string (the filename) and returns a number with the
average of mobiles per inhabitant (the number of mobiles divided by the number of inhabitants).
7
Computer Science Fundamentals
Finally, write a program that, given an input filename and an output filename, uses the previous
functions to obtain the names of the countries with more mobile phones per inhabitant than the
average according to the data in the input filename and store these names on the output
filename.