Kaggle — Learn Python Challenge: Day 5
Editor: Ishmael Njie
Day 5 is here!
Here are the links to the previous exercises:
Day 1 — Hello Python!(Variable assignment etc.)
Day 2 — Functions and Getting Help
Day 5 was all about loops. My notebook for these set of exercises can be found here!
- Exercise 1:
The first exercise was around debugging a function; the function is supposed to return True if there is a number in the list argument that is divisible by 7:
def has_lucky_number(nums):
"""Return whether the given list of numbers is lucky. A lucky list contains at least one number divisible by 7.
""" for num in nums:
if num % 7 == 0:
return True
else:
return False
If we call this function:
nums = [1,2,3,4,5,6,7]
has_lucky_number(nums)
The output here is False. However, we can clearly see that there is a number in the list that is divisible by 7.
The function will work if we remove the ‘else’ keyword and indent the last return correctly:
def has_lucky_number(nums):
for num in nums:
if num % 7 == 0:
return True
return False
The ‘return’ keyword causes a function to exit immediately. So previously, the function only ran for one iteration as it would have identified 1 to not be divisible by 7.
- Exercise 2:
Part a:
[1, 2, 3, 4] > 2
A TypeError was thrown with this particular line of code, as the > operator cannot compare instances of different types: lists and integers.
Part b:
Based on the code above, this part was about defining a function that returned True or False when comparing each element of the list to 2. True is the number is greater than 2 and False otherwise.
def elementwise_greater_than(L, thresh):
"""Return a list with the same length as L, where the value at index i is True if L[i] is greater than thresh, and False otherwise.
>>> elementwise_greater_than([1, 2, 3, 4], 2)
[False, False, True, True]
""" greater = []
for i in L:
greater.append(i > thresh)
return greater
pass
Here, we append a new list, called ‘greater’, with the boolean output of the comparisons on each element with 2.
- Exercise 3:
This exercise consisted of defining a function that returned True if the same meal was served two days in a row, and False otherwise.
def menu_is_boring(meals): for i in range(len(meals)-1):
if meals[i] == meals[i+1]:
return True
return False
- Exercise 4:
Exercise 4 was all about a slot machine. A function ‘play_slot_machine()’ is already defined, and outputs a number relating to winnings in dollars. It often returns 0, but with these slot machines, you could get lucky and get a number out.
As it costs 1$ to play the machine, we are to define a function that returns the average payout per play.
def estimate_average_slot_payout(n_runs):
"""Run the slot machine n_runs times and return the average net profit per run.
""" a =[]
for i in range(n_runs):
a.append(play_slot_machine()-1)
return sum(a)/len(a) #Average
- Exercise 5:
As it is 1$ to play the slot machine, how many spins can you play before running out of money?
If you have x$, you can definitely have x spins, but you may get a payout which will allow for more spins!
This task was centred around completing the function to estimate the probability of completing a given number of spins before running out of money.
def slots_survival_probability(start_balance, n_spins, n_simulations):
"""Return the approximate probability (as a number between 0 and 1) that we can complete the given number of spins of the slot machine before running out of money, assuming we start with the given balance. Estimate the probability by running the scenario the specified number of times.
>>> slots_survival_probability(10.00, 10, 1000)
1.0
>>> slots_survival_probability(1.00, 2, 1000)
.25
"""
success = 0
for i in range(n_simulations):
start = start_balance
spin = n_spins
while start>=1 and spin:
start = start - 1 + play_slot_machine()
spin -=1
if spin ==0:
success+= 1
return success / n_simulations
For each simulation, we have a start balance and a number of spins. While we have x$ and an n number of spins left, update our balance each time we play the slot machine. Every time we meet the number of spins (spins = 0), we have succeeded. The returned value is an average given a number of simulations.
Follow the notebook for these exercises here and stay tuned for Day 6!