:

# Remember to execute this cell with Control+Enter
import jupman;


# Functions 6 - exercises with sets¶

Browse files online

### Exercise - syllabs¶

Write a function syllabs which given a string word made by only bisyllabs and a set found, finds all the distinct bisyllabs and puts them into the set found.

• NOTE: the function syllabs return NOTHING !

Example 1:

>>> found = set()
>>> syllabs("banana", found)
>>> found
{'an', 'ba'}


Example 2:

>>> found = set()
>>> syllabs("bonobo", found)
>>> found
{'bo', 'on'}

Show solution
:


# write here



### Exercise - distinguish¶

✪✪ Write a function distinguish which given a list big_list containing sublists of two characters each, RETURN a NEW LIST containing all the distinct sublists (ignoring the duplicated sublists)

• the returned list must have the elements in the same order in which they were found in big_list

• to know fast whether a sublist was already found, use a set

• DO NOT search in lists (so no count, index, in in lists - they’re slow!)

• DO NOT remove from lists (so no remove from lists - it’s slow!)

• HINT: lists are mutable, can we place them in a set? If it’s not possible, what can we do?

Example:

>>> big_list = [ ['d','d'],['a','b'],['d','d'],['c','a'],['c','a'],['d','d'],['a','b'] ]
>>> distinguish( big_list)
[['d', 'd'], ['a', 'b'], ['c', 'a']]
#NOTE: variable big_list MUST NOT be modified:
>>> big_list
[ ['d','d'],['a','b'],['d','d'],['c','a'],['c','a'],['d','d'],['a','b'] ]

Show solution
:

# write here



### Exercise - intersectron¶ Given a list sets containing an arbitrary number of sets, RETURN a NEW set which contains the elements common to all sets.

To solve the exercise, you can intersecate a set at a time with a for cycle (slow) or with the technique described here (short and fast).

• try to solve it in both ways

• BEWARE of the empty list!

• your code must work with any number of sets (the image is just an example)

Show solution
:

def inter_for(sets):

raise Exception('TODO IMPLEMENT ME !')

# TEST START - DO NOT TOUCH !
assert inter_for([]) == set()
assert inter_for([set(),set()]) == set()
assert inter_for([set(),set(),set()]) == set()
assert inter_for([{'a'},{'a'},{'a'}]) == {'a'}
assert inter_for([{'a','b'},{'b'},{'b'}]) == {'b'}
assert inter_for([{'a'},{'a','b'},{'a'}]) == {'a'}
assert inter_for([{'c'},{'c'},{'c','b'}]) == {'c'}
assert inter_for([{'a','b'},{'a','b'},{'a','b'}]) == {'a','b'}
assert inter_for([{'a','b','c'},{'a','b','c','d'},{'b','c','d'}, {'b','c'}]) == {'b','c'}
# check we didn't modify the input sets
s = {'a','b'}
assert inter_for([s,{'b','c'}]) == {'b'}
assert s == {'a','b'}
# TEST END

Show solution
:

def inter_fast(sets):

raise Exception('TODO IMPLEMENT ME !')

# TEST START - DO NOT TOUCH !
assert inter_fast([]) == set()
assert inter_fast([set(),set()]) == set()
assert inter_fast([set(),set(),set()]) == set()
assert inter_fast([{'a'},{'a'},{'a'}]) == {'a'}
assert inter_fast([{'a','b'},{'b'},{'b'}]) == {'b'}
assert inter_fast([{'a'},{'a','b'},{'a'}]) == {'a'}
assert inter_fast([{'c'},{'c'},{'c','b'}]) == {'c'}
assert inter_fast([{'a','b'},{'a','b'},{'a','b'}]) == {'a','b'}
assert inter_fast([{'a','b','c'},{'a','b','c','d'},{'b','c','d'}, {'b','c'}]) == {'b','c'}
# check we didn't modify the input sets
s = {'a','b'}
assert inter_fast([s,{'b','c'}]) == {'b'}
assert s == {'a','b'}
# TEST END

[ ]: