Reading and Writing Files

1.

def find_dups(L):
    """ (list) -> set

    Return the number of duplicates numbers from L.

    >>> find_dups([1, 1, 2, 3, 4, 2])
    {1, 2}
    >>> find_dups([1, 2, 3, 4])
    set()
    """ 

    elem_set = set()
    dups_set = set()

    for entry in L:
        len_initial = len(elem_set)
        elem_set.add(entry)
        len_after = len(elem_set)
        if len_initial == len_after:
            dups_set.add(entry)

    return(dups_set)

2.

def mating_pairs(males, females):
    """ (set, set) -> set of tuple

    Return a set of tuples where each tuple contains a male from males and a 
    female from females.

    >>> mating_pairs({'Anne', 'Beatrice', 'Cari'}, {'Ali', 'Bob', 'Chen'})
    {('Cari', 'Chen'), ('Beatrice', 'Bob'), ('Anne', 'Ali')}
    """ 

    pairs = set()
    num_gerbils = len(males)

    for i in range(num_gerbils):

        male = males.pop()
        female = females.pop()
        pairs.add((male, female),)

    return pairs

3.

def get_authors(filenames):
    """ (list of str) -> set of str

    Return a list of the authors in PDB files names appear in filenames.
    """ 

    authors = set()

    for filename in filenames:
        pdb_file = open(filename)

        for line in pdb_file:
            if line.lower().startswith('author'):
                author = line[6:].strip()
                authors.add(author)

    return authors