Functions 5 - exercises with tuples

Download exercises zip

Browse files online

Exercise - joined

✪✪ Write a function which given two tuples of characters ta and tb having each different characters (may also be empty), return a tuple made like this:

  • if the tuple ta terminates with the same character tb begins with, RETURN the concatenation of ta and tb WITHOUT the join character duplicated.

  • otherwise RETURN an empty tuple

Example:

>>> joined(('a','b','c'), ('c','d','e','e','f'))
('a', 'b', 'c', 'd', 'e','e','f')
>>> joined(('a','b'), ('b','c','d'))
('a', 'b', 'c', 'd')
Show solution
[2]:


def joined(ta,tb): raise Exception('TODO IMPLEMENT ME !') assert joined(('a','b','c'), ('c','d','e','e','f')) == ('a', 'b', 'c', 'd', 'e','e','f') assert joined(('a','b'), ('b','c','d')) == ('a', 'b', 'c', 'd') assert joined((),('e','f','g')) == () assert joined(('a',),('e','f','g')) == () assert joined(('a','b','c'),()) == () assert joined(('a','b','c'),('d','e')) == ()

nasty

✪✪✪ Given two tuples ta and b, ta made of characters and tb of positive integer numbers , write a function nasty which RETURNS a tuple having two character strings: the first character is taken from ta, the second is a number taken from the corresponding position in tb. The strings are repeated for a number of times equal to that number.

>>> nasty(('u','r','g'), (4,2,3))
('u4', 'u4', 'u4', 'u4', 'r2', 'r2', 'g3', 'g3', 'g3')

>>> nasty(('g','a','s','p'), (2,4,1,3))
('g2', 'g2', 'a4', 'a4', 'a4', 'a4', 's1', 'p3', 'p3', 'p3')
Show solution
[3]:
# write here


Continue

Go on with exercises about functions and sets

[ ]: