Exercises

If you ever get really stuck on an exercise, skip it and ask for help!

Exercise 1.1: Variables

a) What is a variable? Why do you think they are called variables?

b) Assign integers to two variables and print the sum of the variables.

c) Assign strings to two variables and print the result of adding the variables together (+).

d) Let a, b and c be declared like in the code below. Use a fourth variable d to swap the values of a and b, and then b and c. Print the variables to verify that, at the end, a = 2, b = 3, and c = 1. a = 1 b = 2 c = 3

e) What will the code below output? Determine the answer before running the code. Verify your answer by running the code.

a = 3
b = a
a = 5
print(b)

f) What is the difference between 3, 3.0, '3', '''3''' and "3"?

g) What variable type should be used to store:

  • Whether Wednesday’s weather will water wallflowers

  • The number Wednesdays on which it has rained before

  • The probability of it raining on Wednesday

  • A description of Wednesday’s weather

h) What happens when you try to print the following:

  • 1/0

  • variableName(40)

  • "Ola" + 5

  • "Ola", 5

Exercise 1.2: Probability

a) Rolling a traditional fair die (faces: 1, 2, 3, 4, 5, 6), calculate the probability to roll: (i) 6; (ii) a prime number (i.e. 2, 3, or 5); (iii) a number greater or equal to 5.

b) A fair coin (heads, tails) is tossed three times. What are the possible outcomes?

c) Do you think there is a difference between tossing three coins and tossing the same coin three times? Motivate your answer.

d) How many times do we have to toss a fair coin for the probability of getting at least one “tail” to be 0.875 (= 7/8)? (Hint: think of the probability of getting at least one “tail” as the probability of not getting another outcome whose probability is easier to calculate.)

e) You have a deck of cards. Calculate the probability of:

  • getting spades when drawing two cards

  • getting a pair of kings drawing two honours (cards from 10 to A)

  • getting a royal flush of diamonds (T, J, Q, K, A of diamonds) drawing 5 cards

f) Alice tossed a fair coin and Bob tossed another fair coin. They tell you that at least one of the coins landed heads. What is the probability that both landed heads?

g) Alice tossed two coins. You witnessed the first toss and know that the first coin landed heads. What is the probability that the second one landed heads?

h) Describe the probability space (the possible outcomes) for the following situations:

  • rolling a die with four faces

  • receiving a grade for a test taken at UiO

  • Oscars result for Quentin Tarantino’s next nominated movie

i) For all the scenarios described in the previous exercise, is it sensible to assume that every outcome has the same probability?

j) You roll a standard die and a die with two faces. Calculate the probability of:

  • obtaining a sum of at least four given that the outcome of the two-faced die is two

  • either the outcome of the six-faced die being twice the outcome of the two-faced die or the outcome of the six-faced die being prime number given that the outcome of the two-faced die is two

  • either the sum of the outcomes being an even number or the outcome of the six-faced die being twice that of the two-faced die given that the outcome of the six-faced die is four

k) Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, “Do you want to pick door No. 2?” Is it to your advantage to switch your choice? (text from Wikipedia “Monty Hall problem”) Note: the host must open a door that you did not pick, and he must reveal a goat and never the car.

Exercise 1.3: Decisions

a) Assign an integer to a variable a. Write a program that prints “Greater than 10!” if the variable is greater than 10; otherwise it prints “NOT greater than 10!”.

b) Assign integers to two variables, smallest and largest. Write a program that swaps their values if smallest is grater than largest; otherwise it leaves the variables unchanged.

c) The program below decides whether someone receives a loan depending on their gender and requested loan amount. Does it discriminate depending on gender?

d) Write a program that grants any loan under 1 million to anyone between the ages of 18 and 80.

e) Write a program that grants a loan randomly, with different probabilites depending on some factor like gender, age or loan amount. (using randint())

f) 1) Store the first name of a customer in the variable customerFirstName. 2) Store the birthday of the customer (as a string of the form dd/mm/yyyy) in the variable customerBirthday. 3) Write a program that prints, “Happy birthday (first name of the customer)!” if the birthday is today; it prints “Sorry, (first name of the customer), we just missed your birthday.” if the birthday was yesterday; otherwise it prints “Good morning, (first name of the customer)!”.

Exercise 1.4: Random number generation

a) Write a program that prints a random integer between 0 and 50 (0 and 50 included).

b) Write a program that prints a random even integer between 0 and 100 (0 and 100 included).

c) Write a program that prints out “Heads” 49% of the time, “Tails” 49% of the time, and “Standing” 2% of the time.

d) Write a program that generates 5 random coin tosses (50% heads, 50% tails), and prints out the total number of heads.

e) Write a program that throws an error 50% of the time.

f) Three equally qualified applicants applied for a job: Annie, Bonnie, and Connie. Write a program that selects randomly the applicant to hire and prints the selected applicant’s name.

g) Three students volunteered to give presentations: Annie, Bonnie, and Connie. We need to decide the order of their presentations. Write a program that selects the order randomly and then prints the names of the students in the order of their presentations.