Python Lab: Exercises
Note: For any looping requirements, you can use for loops, comprehensions or maps where ever relevant. There is no hard rule (UNTIL SPECIFIED EXPLICITLY) to only use comprehensions over maps or loops. You can write whatever you feel will work best and keep the code clean and readable.
- Write a function named compare_nums. The function should accept two integers as parameters. If the first number is greater than the second, return “First is greater”. If the second is greater than the first, then return “Second is greater”. Otherwise simply return “Numbers are equal”.
- Write a function named count_letter. The function should accept two strings as parameters. The first parameter should be a word and the second parameter should be a single letter. The function should return the number of times the letter appears in the word. If the letter was not found in the word, then it should simply return 0. Also the function should be case insensitive, so it does not matter if the word or the letter provided are uppercase or lowercase.
- Write a function named count_multiple_letters. The function take in just one parameter, a string and then returns a dictionary with the keys being the letters and the values being the count of the letter in the string (number of times the letter appeared in that string).
- Write a function named modify_list. This function should take 4 parameters, (list, command, location, value).
- If the command is “remove” and the location is “end”, the function should remove the last item in the list and return the removed item
- If the command is “remove” and the location is “start”, the function should remove the first item in the list and return the removed item
- If the command is “add” and the location is “start”, the function should add the value to the beginning of the list and return the list
- If the command is “add” and the location is “end”, the function should add the value to the end of the list and return the list
- Write a function named capitalize. The function should accept a string and then return the same string with the first letter capitalized.
- Write a function named common. The function should accept two lists as parameters and should return a new list containing only the common values from both the lists that were passed as arguments.
- Write a function named contains_python. This should should accept any number of arguments (unlimited). It should return True if any of the arguments passed contain the string “python” (all lowercase). Otherwise it should return False.
- Write a lambda that accepts a single number as the parameter and raises it’s power by 10. Save the lambda to a variable named power_10.
- Write a function named decrement_list that accepts two arguments, a list of numbers and a single number. The function should return a copy of the list where each value in the list is subtracted by the number passed as the second argument. (NOTE: YOU HAVE TO USE THE MAP FUNCTION TO DO THIS AND NOT ANYTHING ELSE. NO COMPREHENSIONS OR LOOPS.)
- Write a function named positive_list that accepts a list of numbers and returns a copy of the list with all negative numbers removed. For ex. if I pass [2,-2,34,5,-45,9,-1], I should get [2,34,5,9] back. (NOTE: YOU HAVE TO THE FILTER FUNCTION AND NOT A LIST COMPREHENSION)
- Write a function named low_high which accepts an iterable. It should return a tuple containing the minimum and maximum values from that iterable. For ex. if I pass a list [7,89,43,-2] I should get back a tuple (-2, 43). If pass a tuple (923,21,23,754,1001) I should get back a tuple (21,1001).
Decorator
# Write a function called \`show\_args\` which accepts a function and returns another function.
# Before invoking the function passed to it, show\_args should be responsible for printing
# two things: a tuple for the positional arguments, and a dictionary of the keyword arguments.
# Write a function called \`double\_return\` which accepts a function and returns another function.
# double\_return should decorate a function by returning two copies of the inner function's return
# value inside of a list. Below is an example.
# @double\_return
# def add(x, y):
# return x + y
# def add(2, 2) # \[4, 4\]
# Write a function called \`ensure\_fewer\_than\_three\_args\` which accepts a function and returns
# another function. The function passed to it should only be evoked if there are fewer than
# three positional arguments passed to it. Otherwise, the inner function should return
# "Too many arguments"
# Write a function called \`only\_ints\` which accepts a function and returns another function.
# The function passed to it should only be invoked if all of the arguments passed to it are
# integers. Otherwise the inner function should return, "Please only invoke integers".
# @only\_ints
# def add(x, y):
# return x + y
# add(1, "hello") # "Please only invoke with integers"
# add(1, 2) # 3
# Write a function called \`ensure\_authorized\` which accepts a function and returns another function.
# The function passed to it should only be invoked if there exists a keyword argument with a name
# of "role" and a value of "admin". Otherwise, the inner function should return "Unauthorized".
# Write a function called \`delay\` which accepts a time and returns an inner function that accepts
# a function. When used as a decorator, delay will wait to execute the function being decorated
# by the amount of time passed to it. Before starting the timer, delay will also print a message
# informing the user that there will be a delay before the decorated function gets run.