<jupyter_start><jupyter_text>Functions 6 - exercises with sets [Download exercises zip](../_static/generated/functions.zip)[Browse files online](https://github.com/DavidLeoni/softpython-en/tree/master/functions)  Exercise - syllabsWrite 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:```python>>> found = set()>>> syllabs("banana", found)>>> found{'an', 'ba'}```Example 2:```python>>> found = set()>>> syllabs("bonobo", found)>>> found{'bo', 'on'}```<jupyter_code>#jupman-purge-output
# write here
def syllabs(word, t):
    for i in range(len(word)//2):
        t.add(word[i:i+2])
    
found = set()
syllabs("banana", found)
print(found)

found = set()
syllabs("bonobo", found)
print(found)<jupyter_output>{'na', 'ba', 'an'}
{'no', 'bo', 'on'}<jupyter_text>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:```python>>> 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'] ]```<jupyter_code># write here

def distinguish(blist):
    s = set()
    ret = []
    
    for sublist in blist: 
        #  In sets we can't place lists because they are mutable, 
        #  but we can insert tuples        
        tup = tuple(sublist)

        #  Checking whether an element belongs to a set it's very fast:
        #  it is independent from the set dimension!    
        
        if tup not in s:  
            ret.append(sublist)
            # Adding an element to a set is very fast:
            # it is independent from the set dimension!            
            s.add(tup)
    
    return ret    
        
big_list = [ ['d','d'],['a','b'],['d','d'],['c','a'],['c','a'],['d','d'],['a','b'] ]
#print('distincts:', distinguish(big_list))
#print('big_list:', big_list)<jupyter_output><empty_output><jupyter_text>Exercise - intersectron![intersectron](img/abc-intersection.png) 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](https://stackoverflow.com/a/2541814) (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)<jupyter_code>def inter_for(sets):    
    
    #jupman-raise    
    if len(sets) == 0:
        return set()
    
    first = True
    
    for el in sets:
        if first:
            ret = set(el)
            first = False
        else:
            ret.intersection_update(el)
    return ret
    #/jupman-raise

# 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
def inter_fast(sets):
    
    #jupman-raise    
    if len(sets) == 0:
        return set()
    
    return set.intersection(*sets)
    #/jupman-raise

# 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<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.23379490266907485, 'fraction_numerical': 0.0028095524784266506, 'mean_word_length': 4.207941483803553, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 39, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '495342', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1727, 'n_words': 496, 'path': 'functions/fun6-sets-sol.ipynb', 'repo_name': 'simoneb1x/softpython-en', 'size': '4983'}