×
Write the following functions
difference
difference(2,2) # 0 difference(0,2) # -2
product
product(2,2) # 4 product(0,2) # 0
print_day
print_day(4) # "Wednesday" print_day(41) # None
last_element
last_element([1,2,3,4]) # 4 last_element([]) # None
number_compare
number_compare(1,1) # "Numbers are equal" number_compare(1,2) # "Second is greater" number_compare(2,1) # "First is greater"
single_letter_count
single_letter_count('amazing','A') # 2
multiple_letter_count
multiple_letter_count("hello") # {h:1, e: 1, l: 2, o:1} multiple_letter_count("person") # {p:1, e: 1, r: 1, s:1, o:1, n:1}
list_manipulation
list_manipulation([1,2,3], "remove", "end") # 3 list_manipulation([1,2,3], "remove", "beginning") # 1 list_manipulation([1,2,3], "add", "beginning", 20) # [20,1,2,3] list_manipulation([1,2,3], "add", "end", 30) # [1,2,3,30]
is_palindrome
True
or False
depending on whether it is a palindrome. As a bonus, allow your function to ignore whitespace and capitalization so that is_palindrome('a man a plan a canal Panama')
returns True
.is_palindrome('testing') # False is_palindrome('tacocat') # True is_palindrome('hannah') # True is_palindrome('robert') # False
frequency
This function accepts a list and a search_term
(this will always be a primitive value) and returns the number of times the search_term
appears in the list.
frequency([1,2,3,4,4,4], 4) # 3 frequency([True, False, True, True], False) # 1
flip_case
This function accepts a string and a letter and reverses the case of all occurances of the letter in the string.
flip_case("Hardy har har", "h") # "hardy Har Har"
multiply_even_numbers
This function accepts a list of numbers and returns the product of all even numbers in the list.
multiply_even_numbers([2,3,4,5,6]) # 48
mode
This function accepts a list of numbers and returns the most frequent number in the list of numbers. You can assume that the mode will be unique.
mode([2,4,1,2,3,3,4,4,5,4,4,6,4,6,7,4]) # 4
capitalize
This function accepts a string and returns the same string with the first letter capitalized.
capitalize("tim") # "Tim" capitalize("matt") # "Matt"
compact
This function accepts a list and returns a list of values that are truthy values.
compact([0,1,2,"",[], False, {}, None, "All done"]) # [1,2, "All done"]
partition
This function accepts a list and a callback function (which you can assume returns True
or False
). The function should iterate over each element in the list and invoke the callback function at each iteration. If the result of the callback function is True
, the element should go into one list if it's False
, the element should go into another list. When it's finished, partition
should return both lists inside of one larger list.
def is_even(num): return num % 2 == 0 partition([1,2,3,4], is_even) # [[2,4],[1,3]]
intersection
This function should accept a two dimensional list and return a list with the values that are the same in each list.
intersection([1,2,3], [2,3,4]) # [2,3]
once
This function accepts a function and returns a new function that can only be invoked once. If the function is invoked more than once, it should return None
. Hint you will need to define a new function inside of your once function and return that function. You can add properties to your inner function to see if it has run already.
def add(a,b): return a + b one_addition = once(add) one_addition(2,2) # 4 one_addition(2,2) # undefined one_addition(12,200) # undefined
Research what decorators
are and refactor your once
code to use a decorator so that you can run
@run_once def add(a,b): return a + b add(2,2) # 4 add(2,20) # None add(12,20) # None
For solutions to these exercises, click here.
Complete the following Codewars problems:
When you're ready, move on to Debugging Python