Functions 3 - exercises with strings
Download exercises zip
First functions
length
✪ a. Write a function length1(s) in which, given a string, RETURN the length of the string. Use len function. For example, with "ciao" string your function should return 4 while with "hi" it should return 2
>>> x = length1("ciao")
>>> x
4
✪ b. Write a function length2 that like before calculates the string length, this time without using len (instead, use a for cycle)
>>> y = length2("mondo")
>>> y
5
[2]:
# write here
contains
✪ Write the function contains(word, character), which RETURN True is the string contains the given character, otherwise RETURN False
Use
inoperator
>>> x = contains('ciao', 'a')
>>> x
True
>>> y = contains('ciao', 'z')
>>> y
False
[3]:
# write here
invertlet
✪ Write the function invertlet(first, second) which takes in input two strings of length greater than 3, and RETURN a new string in which the words are concataned and separated by a space, the last two characters in the words are inverted. For example, if you pass in input 'twist' and 'space', the function should RETURN 'twise spact'
If the two strings are not of adequate length, the program PRINTS error!
NOTE 1: PRINTing is different from RETURNing !!! Whatever gets printed is shown to the user but Python cannot reuse it for calculations.
NOTE 2: if a function does not explicitly return anything, Python implicitly returns None.
NOTE 3: Resorting to prints on error conditions is actually bad practice: this is an invitation to think about what happens when you print something and do not return anything. You can read a discussion about it in Errors handling and testing page
>>> x = invertlet("twist", "space")
>>> x
'twise spact'
>>> x = invertlet("fear", "me")
'error!'
>>> x
None
>>> x = invertlet("so", "bad")
'error!'
>>> x
None
[4]:
# write here
nspace
✪ Write a function nspace that given a string s in input, RETURN a new string in which the n-character is a space.
if the number is too big, raise the exception
ValueError- in the exception message state clearly what the problem was and the input.
NOTE: This time instead of printing the error we raise the exception, which will prevent the program from continuing further. This is a much better way to react to erroneous conditions.
>>> x = nspace('allegory', 5)
>>> x
'alleg ry'
>>> x = nspace('toy', 9)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
2610223641.py in <module>
---> 12 nspace("toy", 9)
ValueError: index 9 is larger than word toy
>>> x = nspace('rack', 4)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
2610223641.py in <module>
---> 12 nspace("rack", 4)
ValueError: index 4 is larger than word rack
[5]:
# write here
startend
✪ Write a function which takes a string s and RETURN the first and last two characters
if length is less than 4, raises
ValueError- in the exception message state clearly what the problem was and the input
>>> startend('robust pack')
rock
>>> startend('sig')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
230230193.py in <module>
----> 8 startend('sig')
ValueError: I need at least 4 characters, got instead: sig
[6]:
# write here
swap
Write a function that given a string, swaps the first and last character and RETURN the result.
if the string is empty, raise
ValueError- in the exception message state clearly the cause of the problem
>>> swap('dream')
mread
>>> swap('c')
c
>>> swap('')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
2089609385.py in <module>
---> 11 swap('')
ValueError: Empty string!
[7]:
# write here
Verify comprehension
ATTENTION
The following exercises contain tests with asserts. To understand how to solve them, read first Error handling and testing