''' Author: Sohie Lee Purpose: Lab 09 Dictionaries and a bit of sorting Date: Spring 22 ''' from kona import kona # Task 1A def printNames(data): """ Print all the athlete names in the format lastname, firstname """ for athlete in data: print(athlete['lastname'] + ',' + athlete['firstname']) # Task 1B def getNameTuples(data): ''' Returns all athlete names in a list of tuples (lastname, firstname) ''' names = [] for athlete in data: names.append((athlete['lastname'], athlete['firstname'])) return sorted(names) def firstname(tup): ''' Helper function to sort name tuples by first name. tup is in form (lastname, firstname). This function returns the firstname of the provided tuple. ''' return tup[1] # Task 1C def getNameTuplesSortedByFirstname(data): ''' Returns all athlete names, sorted by firstname, in a list of tuples (lastname, firstname). Uses the helper function firstname so that the tuples are sorted by firstname. ''' names = [] for athlete in data: names.append((athlete['lastname'], athlete['firstname'])) return sorted(names, key=firstname) # Task 2 def getNamesCountry(data, country): """ Return a list of athlete tuples (lastname, firstname) of a specified country, sorted by lastname """ names = [] for athlete in data: if athlete['country'] == country: names.append((athlete['lastname'], athlete['firstname'])) return sorted(names) # Task 3 def getAllCountries(data): """ Returns a list of unique countries from the provided data, sorted alphabetically """ # initialize list to store all countries countries = [] for athlete in data: thisCountry = athlete['country'] # only add countries that are not already in the countries list if thisCountry not in countries: countries.append(thisCountry) return sorted(countries) # Task 4 def totalAthletesByCountry(data): ''' Returns a dictionary where the keys are unique countries, and the corresponding values are a mini-dictionary of average times for each event (swim, bike, run). A dictionary key:value pair might look liks this: 'FIN': {'swim': 0.88, 'bike': 5.05, 'run': 3.7} ''' results = {} for athlete in data: country = athlete['country'] # Country already counted, increment if country in results: results[country] += 1 # Country not counted, initialize else: results[country] = 1 return results # Task 5 def buildDictWithAverageEventTimes(data): athletesByCountry = totalAthletesByCountry(data) countries = getAllCountries(data) results = {} # Initialize 0 totals for swim, bike, run for country in countries: results[country] = {'swim':0, 'bike':0, 'run':0} # Add each athlete's total to swim, bike, run for athlete in data: country = athlete['country'] # Note: some athletes who did not finish, so their times # are recorded as "DNF" = Did not finish or '---' if type(athlete['swim']) == str or type(athlete['bike']) == str or type(athlete['run']) == str: # skip this athlete pass else: results[country]['swim'] += athlete['swim'] results[country]['bike'] += athlete['bike'] results[country]['run'] += athlete['run'] # Divide totals by athlete count for each country for country in results: average = results[country]['swim'] / athletesByCountry[country] results[country]['swim'] = round(average, 2) average = results[country]['bike'] / athletesByCountry[country] results[country]['bike'] = round(average, 2) average = results[country]['run'] / athletesByCountry[country] results[country]['run'] = round(average, 2) return results