Kaggle — Learn Python Challenge: Day 4
Editor: Ishmael Njie
Onto Day 4!
Hope you have been keeping up with the daily posts!
Here are the links to the previous exercises:
Day 1 — Hello Python!(Variable assignment etc.)
Day 4 was on lists. In Python, lists represent sequences of values. They can be defined with comma-separated values between square brackets:
numbers = [1,2,3,4]
Let’s get into the exercises; follow with the notebook for this set of exercises:
- Exercise 1:
The first task was to create a function that returned the second element of a given list.
def select_second(L):
if len(L) < 2: #if list has less than 2 elements then return none.
return None return L[1] #Otherwise, return the second element
Here, the index 1 refers to the 2nd element as Python uses zero-based indexing; where the first element is index 0.
- Exercise 2:
Here we had to define a function that took a list of many lists as an argument and then returned the 2nd element from the last list, in the list of lists… hope that made sense (refer to the notebook for a better understanding).
def losing_team_captain(teams):
"""Given a list of teams, where each team is a list of names, return the 2nd player (captain)
from the last listed team
"""
return teams[-1][1]
- Exercise 3:
This exercise introduced a new item in Mario Kart! A purple shell, which swaps the racer in first place and the racer in last place (a pretty evil item!)
So we had to define a function that set the first place racer to last place and vice versa.
def purple_shell(racers):
"""
>>> r = ["Mario", "Bowser", "Luigi"]
>>> purple_shell(r)
>>> r
["Luigi", "Bowser", "Mario"]
""" c = racers[0]
racers[0] = racers[-1]
racers[-1] = c
We saw a technique like this in Day 1 of the exercises! Using a temporary variable to store one of the variables.
- Exercise 4:
This exercise was testing our understanding of lists and slicing. We were to guess the lengths of the following lists:
a = [1, 2, 3]
b = [1, [2, 3]]
c = []
d = [1, 2, 3][1:]
a = 3
b = 2 as [2,3] counts as one element in the entire list.
c = 0 items (empty list)
d = 2 as the expression returns [2,3]
- Exercise 5:
This tasks involves using a list to record party attendees. The task is to define a function that details whether an attendee is fashionably late (arriving after at least half of the guests but not last).
def fashionably_late(arrivals, name):
"""Given an ordered list of arrivals to the party and a name, return whether the guest with that name was fashionably late.
""" ind = arrivals.index(name) # gets the index
half = len(arrivals)/2
return ind >= half and ind != len(arrivals) -1
- Exercise 6:
This was a tough ‘riddle’ to complete. A very interesting problem; to count the number of negative numbers in a given list.
def count_negatives(nums):
"""Return the number of negative numbers in the given list.
>>> count_negatives([5, -1, -2, 0, 3])
2
"""
To solve this problem, it would be great to sort the list. If we first append the list with the number ‘0’, we can search for the 0 with the .index method. If the list is in order after we append the list with a 0, we get the position of the 0, and all the items below that place will be negative so we can infer the number of negative numbers.
def count_negatives(nums):
nums.append(0)
nums.sort()
return nums.index(0)
If we run the function:
count_negatives([5, -1, -2, 0, 3])
This will return the number 2, which is the position of the 0 after it is sorted, as well as the number of negative numbers in the list. Check the notebook for a full explanation!
Keep up with the challenges and stay tuned for Day 5!