small medium large xlarge

# Designing and Using Functions

1.

a. `2`
a. `7`
a. `4`

2. a. `max(3, 4)`, then `abs(-5)`, then `min(4, 5)`.
b. `max(2, 8)`, then `min(4, 6, 8)`, then `abs(4)`.
c. `max(5.572, 3.258)`, then `abs(-2)`, then `round(5.572, 2)`.

3.

```def triple(num):
""" (number) -> number

Return num tripled.

>>> triple(3)
9
"""

return num * 3
```

4.

```def absolute_difference(number1, number2):
""" (number, number) -> number

Return the absolute value of the difference between number1
and number2.

>>> absolute_difference(3, 7)
4
"""
return abs(number1 - number2)
```
5.
```def km_to_miles(km):
""" (number) -> float

Return the distance km in miles.

>>> km_to_miles(5)
3.125
"""

return km / 1.6
```

6.

```def average_grade(grade1, grade2, grade3):
""" (number, number, number) -> number

Return the average of the grade1, grade2, and grade3, where
each grade ranges from 0 to 100, inclusive.

>>> average_grade(80, 95, 90)
88.33333333333333
"""
return (grade1 + grade2 + grade3) / 3
```

7.

```def top_three_avg(grade1, grade2, grade3, grade4):
""" (number, number, number, number) -> number

Return the average of the top three of grades grade1, grade2,
grade3, and grade4.

>>> top_three_avg(50, 60, 70, 80)
70
"""

# Here is one solution that does not use average_grade from Q6.
total = sum(grade1, grade2, grade3, grade4)
top_three = total - min(grade1, grade2, grade3, grade4)
return top_three / 3

# Here is a different solution that does use the function from Q6.
return max(average_grade(grade1, grade2, grade3),
average_grade(grade1, grade2, grade4),
average_grade(grade1, grade3, grade4),
average_grade(grade2, grade3, grade4))

```

8.

```def weeks_elapsed(day1, day2):
""" (int, int) -> int
day1 and day2 are days in the same year. Return the number of full weeks
that have elapsed between the two days.

>>> weeks_elapsed(3, 20)
2
>>> weeks_elapsed(20, 3)
2
>>> weeks_elapsed(8, 5)
0
>>> weeks_elapsed(40, 61)
3
"""
return int(abs(day1 - day2) / 7)
```

9.

```Description                Example
Parameter                  num
Argument                   3
Function name              square
Function call              square(3)
```

10.

```def square(num):
""" (number) -> number

Return the square of num.

>>> square(3)
9
"""
return num ** 2
```

...or:

```def square(num):
""" (number) -> number

Return the square of num.

>>> square(3)
9
"""
return num * num
```
Page History
• V16: Jennifer Campbell [over 3 years ago]
• V15: Jennifer Campbell [over 3 years ago]
• V14: Jennifer Campbell [over 3 years ago]
• V13: Jennifer Campbell [over 3 years ago]
• V12: Jennifer Campbell [over 3 years ago]
• V11: Jennifer Campbell [over 3 years ago]
• V10: Jennifer Campbell [over 3 years ago]
• V9: Jennifer Campbell [over 3 years ago]
• V8: Jennifer Campbell [over 3 years ago]
• V7: Jennifer Campbell [over 3 years ago]
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://pragprog.com/wikis/wiki/PracProg2functions/version/15", "fetch_time": 1484696029000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2017-04/segments/1484560280128.70/warc/CC-MAIN-20170116095120-00456-ip-10-171-10-70.ec2.internal.warc.gz", "warc_record_offset": 847975211, "warc_record_length": 11456, "token_count": 903, "char_count": 2862, "score": 3.90625, "int_score": 4, "crawl": "CC-MAIN-2017-04", "snapshot_type": "longest", "language": "en", "language_score": 0.7061863541603088, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00014-of-00064.parquet"}