- Define a function
sum()
and a functionmultiply()
that sums and multiplies (respectively) all the numbers in a list of numbers. For example,sum([1, 2, 3, 4])
should return10
, andmultiply([1, 2, 3, 4])
should return24
.
For this exercise, we start by setting my_sum to 0 as inside the for loop the update occurs. my_sum is reassigned a new value which is my_sum + each element in the list.
- Define a function
reverse()
that computes the reversal of a string. For example,reverse("I am testing")
should return the string"gnitset ma I"
.
Thanks to [::-1], we can have the sentence written by the user reversed!
- Define a function
is_palindrome()
that recognises palindromes (i.e. words that look the same written backwards). For example,is_palindrome("radar")
should returnTrue
.
A useful trick for this function was to use the word.upper() Python string method. It allows you to ask the user either upper or lower case words. As stated above, the [::-1] is an easy way to have a whole string reversed. We insert a if statement inside the function asking to return True if the reverse of the word is equal to the word.
- Write a function
is_member()
that takes a value (i.e. a number, string, etc)x
and a list of valuesa
, and returnsTrue
ifx
is a member ofa
,False
otherwise. (Note that this is exactly what thein
operator does, but for the sake of the exercise you should pretend Python did not have this operator.)
I defined the function is_member with the two parameters x and alist. I used a for loop as it check for each of the elements in the list, one by one. If any of the element in the list [1,2,3,7,90] equals to x, the function returns true.
- Define a function
overlapping()
that takes two lists and returns True if they have at least one member in common, False otherwise. You may use youris_member()
function, or thein
operator, but for the sake of the exercise, you should (also) write it using two nested for-loops.
First, I defined the overlapping function which takes two arguments, x & y. Then I wrote a two nested for-loops. For each elements in x, and for each element in y. Finally comes the if statement which check if any of the elements in x are equal to any of the elements in y. If so, the function returns true.