--Originally published at Python fundamentals
Instructions
Return to the 46 exercises page and solve the exercises 6 up to 10.
Procedure
Exercise 6:
Define a function sum()
and a function multiply()
that sums and multiplies (respectively) all the numbers in a list of numbers. For example, sum([1, 2, 3, 4])
should return 10
, and multiply([1, 2, 3, 4])
should return 24
We will create 2 funcitons, one for addition and other for multiplication, in the first one we will create a loop that will go through ach one of the numbers on the list and add into a new variable know as total.
In the second one, we will do the same but instead of addition we will use substraction.
Exercise 7:
Define a function reverse()
that computes the reversal of a string. For example, reverse("I am testing")
should return the string "gnitset ma I"
.
We will create a function that uses a for loop to go through each one of the characters of a string and add it to a new string callled “word” in an inverse order.
Exercise 8:
Define a function is_palindrome()
that recognizes palindromes (i.e. words that look the same written backwards). For example, is_palindrome("radar")
should return True
.
We will use our current reverse function to find the reversal of a string, if the reversal its the same as the original string, then palindorme its true, we will do this second part in another function called palindrome.
Exercise 9:
Write a function is_member()
that takes a value (i.e. a number, string, etc) x
and a list of values a
, and returns True
if x
is a member of a
, False
otherwise. (Note that this is exactly what the in
operator does, but for the sake of the exercise you should pretend Python did not have this
Continue reading "#quiz06" →