1)
Exercise: Define a function
named combined_length that accepts two
arguments, string_one and string_two, and returns their combined
length. For example, when given the arguments "hi" and "there", your function should
return 7
assert combined_length("hi", "there") == 7
assert combined_length("foo", "bar") == 6
---------------------------------------------------------------------------
TypeError Traceback
(most recent call last)
<ipython-input-17-05cf60eb871f> in <module>
----> 1 assert combined_length("hi", "there") == 7
2 assert combined_length("foo", "bar") == 6
TypeError: 'str' object is not callable
2)
Exercise: Write a function called calculate_average that accepts one
argument called my_list and
returns the average value (arithmetic mean) of the items in the list. For
example, when the value [3,9,12] is
passed as the argument, your function should return 8. Hint: you will need to use the sum and len functions.
assert calculate_average([3,9,12]) == 8
assert calculate_average([5,4,8,9]) == 6.5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-34-20507da77250> in <module>
----> 1 assert calculate_average([3,9,12]) == 8
2 assert calculate_average([5,4,8,9]) == 6.5
TypeError: 'float' object is not callable
3)
Exercise: Write a function
called first_two that accepts one argument called my_list and returns a
new list consisting of the first two elements of my_list. Use slice notation.
What happens when you call your function with a zero-length or one-length list?
assert
first_two([]) == []
assert
first_two([1]) == [1]
assert
first_two([1,2,3]) == [1,2]
---------------------------------------------------------------------------
TypeError Traceback
(most recent call last)
<ipython-input-52-b4835bc628df> in <module>
----> 1 assert first_two([]) == []
2 assert first_two([1]) == [1]
3 assert first_two([1,2,3]) == [1,2]
TypeError: 'list' object is not callable
4)
Exercise: Write a function called length_of_longest_word that accepts one list variable called word_list as an argument and returns the length of the longest word in
that list. Hint: initialize a variable above your loop with a statement like
this: max_length = 0.
Then, in the body of your loop, use the max function to compare this max_length value with the length of the current word, like this: max(max_length,
length_of_current_word), and then update max_length appropriately with the new value. If you get lost, try
adding some print statements
inside of your loop!
assert length_of_longest_word(["these", "are", "diminuitive", "words"]) == 11
assert length_of_longest_word(["short", "tiny", "haha", "antidisestablishmentarianism"]) == 28
5.
Exercise: Write a
function called sum_leq that accepts as input a number variable
called num and returns the sum of all positive
integers less than or equal to num. For example, sum_leq(4) should return 1 + 2 + 3 +
4 = 10. Hint: you
will need to use a variable to keep track of the total as you add more numbers.
To check your implementation, call your function with the argument 100 - you should get a result of 5050.
6.
Exercise: Write a function called should_get_hired that accepts two arguments: interview_one_score and interview_two_score and returns the following values:
assert should_get_hired(3,5) == "interview again"
assert should_get_hired(5,3) == "interview again"
assert should_get_hired(2,2) == "nope"
7.
Exercise: Write a function called get_loyalty_program that receives a single argument called customer_spending_usd. If the customer spent more than $50,000, your function should
return "platinum". If the customer spent more than
$20,000, your function should return "gold". Otherwise, your function should return "no
status"
assert get_loyalty_program(70000) == "platinum"
assert get_loyalty_program(45000) == "gold"
assert get_loyalty_program(0) == "no status"
8. Exercise: Write a function
called calculate_sphere_volume that
accepts one argument called radius and uses the pi value from the math module and the function math.pow above to implement the formula 43??343πr3:
assert calculate_sphere_volume(1) == 4.1887902047863905
assert calculate_sphere_volume(2) == 33.510321638291124
9. Exercise: Write a function called random_element that accepts a list argument my_list and
returns a random element from the list. Hint: you can do this with the randint function,
or you can be adventurous and search the random module documentation for a function that can perform the same task with
fewer lines of code.
random.seed(1337)
assert random_element([1,2,3,4,5]) == 5
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
27 | 28 | 29 | 30 | 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |