Kaggle Challenge 03 - Functions and Getting Help

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

Kaggle Challenge 03 - Functions and Getting Help

0. help()

I saw the abs function in the previous tutorial, but what if you've forgotten what it does?

Example : help(round)

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

1. Question01(round)

Problem

def round_to_two_places(num):
    """Return the given number rounded to two decimal places. 
    
    >>> round_to_two_places(3.14159)
    3.14
    """
    # Replace this body with your own code.
    # ("pass" is a keyword that does literally nothing. We used it as a placeholder
    # because after we begin a code block, Python requires at least one line of code)
    pass
    round(3.14)

# Check your answer
q1.check()

help(round)

Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

Solution

return round(num, 2)

2. Question02

Problem

The help for round says that ndigits (the second argument) may be negative. What do you think will happen when it is? Try some examples in the following cell.

Solution

Solution: As you've seen, ndigits=-1 rounds to the nearest 10, ndigits=-2 rounds to the nearest 100 and so on. Where might this be useful? Suppose we're dealing with large numbers:

The area of Finland is 338,424 km²
The area of Greenland is 2,166,086 km²

We probably don't care whether it's really 338,424, or 338,425, or 338,177. All those digits of accuracy are just distracting. We can chop them off by calling round() with ndigits=-3:

The area of Finland is 338,000 km²
The area of Greenland is 2,166,000 km²

(We'll talk about how we would get the commas later when we talk about string formatting :))

3. Question03

Problem

In the previous exercise, the candy-sharing friends Alice, Bob and Carol tried to split candies evenly. For the sake of their friendship, any candies left over would be smashed. For example, if they collectively bring home 91 candies, they'll take 30 each and smash 1.
Below is a simple function that will calculate the number of candies to smash for any number of total candies.
Modify it so that it optionally takes a second argument representing the number of friends the candies are being split between. If no second argument is provided, it should assume 3 friends, as before.
Update the docstring to reflect this new behaviour.

def to_smash(total_candies):
    """Return the number of leftover candies that must be smashed after distributing
    the given number of candies evenly between 3 friends.
    
    >>> to_smash(91)
    1
    """
    return total_candies % 3

# Check your answer
q3.check()

Solution

def to_smash(total_candies, n_friends=3):
    return total_candies % n_friends
profile
DataEngineer Lee.

5개의 댓글

comment-user-thumbnail
2022년 8월 10일

This is a fantastic post, and I appreciate the details. Without a doubt, education is a sensitive subject. Despite this, one of the hottest topics of our time remains. I enjoy reading your posts and look forward to reading more. uno online

답글 달기
comment-user-thumbnail
2023년 7월 27일

Great information! I will visit your site more often so I am updated. The ideas I gather from here cannot be paid with money. Thanks very much! Bitlife

답글 달기
comment-user-thumbnail
2024년 3월 23일

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit Strands Unlimited https://strandsgame.net/

답글 달기
comment-user-thumbnail
2024년 3월 27일
답글 달기
comment-user-thumbnail
2024년 3월 30일

Thank you for submitting this article. This is information I have been looking for. I’ve been hoping to find clear and concise content like yours. Your unique points helped me think about this information differently. Not My Neighbor Online https://notmyneighborgame.com/

답글 달기