6.1. Exercises

Function Design Questions

def give_me_facts(myinfo):
    max_val = max(myinfo)
    min_val = min(myinfo)
    range_val = max_val - min_val
    return max_val, min_val, range_val

Improve it!


def count_the_sevens(mylist): 
    number_of_occurances  = mylist.count(7)
    return number_of_occurances

Function Design

Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.

When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.

Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.

Given the function below, improve it so that it follows good function design principles.

Tasks:

  • Given the function above, improve it using to the new function new_wage(). We have provided you with the function stub and the docstring to guide you.
  • Make sure it follows good function design practices by not looping over a function, avoiding hard-coding and not returning multiple values.
  • Test your new function on a person with a salary of $84000 and an expected raise of 12%.
  • Save this in an object named person1_new_wage.
Hint 1
  • Are you removing the hardcoded values in the function?
  • Are removing the return of 2 variables?
  • Are you removing the loop from inside the function?
  • Are you multiplying the salary by (1 + (0.01 * percent_raise)?
Fully worked solution: