Kaggle Challenge 05 - Functions and Getting Help

JongseokLee·2021년 8월 8일
0
post-thumbnail

0. Tutorial

Indexing

planets = ['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']

planets[0], planets[-1]
'Mercury', 'Neptune'

Slicing

planets[0:3], planets[:3]['Mercury', 'Venus', 'Earth']

Changing lists

planets[3] = 'Malacandra'planets
['Mercury',
'Venus',
'Earth',
'Malacandra',
'Jupiter',
'Saturn',
'Uranus',
'Neptune']

len

len(planets) # How many planets are there?
8

sorted

#The planets sorted in alphabetical order
sorted(planets)
['Earth', 'Jupiter', 'Mars', 'Mercury', 'Neptune', 'Saturn', 'Uranus', 'Venus']

list.append modifies a list by adding an item to the end:

planets.pop()

planets.index('Earth')

Tuples
Tuples are almost exactly the same as lists. They differ in just two ways.

1: The syntax for creating them uses parentheses instead of square brackets
2: They cannot be modified (they are immutable).


Question01

Question
Complete the function below according to its docstring.

def select_second(L):
    """Return the second element of the given list. If the list has no second
    element, return None.
    """
    pass

Solution

Solution:

def select_second(L):
    if len(L) < 2:
        return None
    return L[1]

Question02

Question
You are analyzing sports teams. Members of each team are stored in a list. The Coach is the first name in the list, the captain is the second name in the list, and other players are listed after that. These lists are stored in another list, which starts with the best team and proceeds through the list to the worst team last. Complete the function below to select the captain of the worst team.

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
    """
    pass

Solution

def losing_team_captain(teams):
    return teams[-1][1]


Question03

Question
The next iteration of Mario Kart will feature an extra-infuriating new item, the Purple Shell. When used, it warps the last place racer into first place and the first place racer into last place. Complete the function below to implement the Purple Shell's effect.

def purple_shell(racers):
    """Given a list of racers, set the first place racer (at the front of the list) to last
    place and vice versa.
    
    >>> r = ["Mario", "Bowser", "Luigi"]
    >>> purple_shell(r)
    >>> r
    ["Luigi", "Bowser", "Mario"]
    """
    pass

Solution

def purple_shell(racers):
    # One slick way to do the swap is x[0], x[-1] = x[-1], x[0].
    temp = racers[0]
    racers[0] = racers[-1]
    racers[-1] = temp

Question04

Question
What are the lengths of the following lists? Fill in the variable lengths with your predictions. (Try to make a prediction for each list without just calling len() on it.)

a = [1, 2, 3]
b = [1, [2, 3]]
c = []
d = [1, 2, 3][1:]

# Put your predictions in the list below. Lengths should contain 4 numbers, the
# first being the length of a, the second being the length of b and so on.
lengths = []

Solution

a: There are three items in this list. Nothing tricky yet.
b: The list [2, 3] counts as a single item. It has one item before it. So we have 2 items in the list
c: The empty list has 0 items
d: The expression is the same as the list [2, 3], which has length 2.

Question05 🌶️

Question
We're using lists to record people who attended our party and what order they arrived in. For example, the following list represents a party with 7 guests, in which Adela showed up first and Ford was the last to arrive:
party_attendees = ['Adela', 'Fleda', 'Owen', 'May', 'Mona', 'Gilbert', 'Ford']
A guest is considered 'fashionably late' if they arrived after at least half of the party's guests. However, they must not be the very last guest (that's taking it too far). In the above example, Mona and Gilbert are the only guests who were fashionably late.
Complete the function below which takes a list of party attendees as well as a person, and tells us whether that person is fashionably late.

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.
    """
    pass

Solution

def fashionably_late(arrivals, name):
    order = arrivals.index(name)
    return order >= len(arrivals) / 2 and order != len(arrivals) - 1

Vocabularies

interlude[n] 막간
associated variable 기록 변수

profile
DataEngineer Lee.

0개의 댓글