Define a function named combined_length that accepts two arguments, string_one and string_two, and returns their combined length.

computer science


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:

  1. If both interview_one_score and interview_two_score are greater than four, return "hire"
  2. If either interview_one_score or interview_two_score (but not both) are greater than four, return "interview again"
  3. Otherwise, return "nope"assert should_get_hired(5,5) == "hire"

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.


assert random_element([1,2,3,4,5]) == 5

Related Questions in computer science category

The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.