# PracProg2setdict

# 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

4.

def count_values(dictionary): """ (dict) -> int Return the number of unique values in dictionary. >>> count_values({'red': 1, 'green': 2, 'blue': 2}) 2 """ return len(set(dictionary.values()))

5.

def least_likely(particle_to_probability): """ (dict) -> str Return the particle from particle_to_probability with the lowest probablity. >>> least_likely({'neutron': 0.55, 'proton': 0.21, 'meson': 0.03, 'muon': 0.07}) 'meson' """ smallest = 1 name = '' for particle in particle_to_probability: probability = particle_to_probability[particle] if probability < smallest: smallest = probability name = particle return particle