Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Exercise 21.1

Modify your code from Exercise 20.2 so that the function average() returns the average of a list of numbers rather than printing it.

arriving_penguins = [10, 156, 73, 376, 786, 432, 1035, 901, 1102, 2567, 1571, 916, 1560, 632, 943, 246, 654, 1456, 504, 632]
def average(x): '''Average of a list of numbers''' return sum(x)/len(x) arriving_penguins = [10, 156, 73, 376, 786, 432, 1035, 901, 1102, 2567, 1571, 916, 1560, 632, 943, 246, 654, 1456, 504, 632] ave = average( arriving_penguins ) print(ave)

Exercise 21.2

  1. Write a function that takes the following list of individual lemur species as an argument, creates a dictionary of the number of sightings of each lemur species and returns that dictionary.

  2. Print out each species name and its number of sightings in descending order of sightings.

    • Hint: You wrote the code for counting lemur sightings in Exercise 17.1. Copy and paste that code then modify it.

lemurs = ['White-headed lemur', 'Gray-headed lemur', 'Common brown lemur', 'Collared brown lemur', 'Red lemur', 'Collared brown lemur', 'Red-fronted lemur', 'Red-bellied lemur', 'Collared brown lemur', 'Crowned lemur', 'Red lemur', 'Crowned lemur', 'Blue-eyed black lemur', 'Common brown lemur', 'Mongoose lemur', 'Common brown lemur', "Sanford's brown lemur", 'White-headed lemur', 'Black lemur', 'Mongoose lemur', 'Red-bellied lemur', 'Red-fronted lemur', 'Blue-eyed black lemur', 'Collared brown lemur', 'Blue-eyed black lemur', 'White-headed lemur', 'Red lemur', 'Red-bellied lemur', 'Gray-headed lemur', 'Red-bellied lemur', 'Red lemur', 'Common brown lemur', 'Mongoose lemur', 'Red lemur', 'Common brown lemur', "Sanford's brown lemur"]
def count_lemurs(lemurs): '''Count the number of sightings of each lemur species''' sightings = {} for lemur in lemurs: if lemur not in sightings: sightings[lemur] = 1 else: sightings[lemur] += 1 return sightings lemur_count = count_lemurs(lemurs) for species, count in sorted(lemur_count.items(), reverse=True, key=lambda kv: kv[1]): print( f'{species}\t{count}')

Exercise 21.3

In Exercise 18.3 you calculated the molecular mass of the peptide "FWGCYPT".

Write a program that passes a peptide sequence to a function called peptide_mass and return its molecular mass.

masses = { 'A' : 71.03711, 'C' : 103.00919, 'D' : 115.02694, 'E' : 129.04259, 'F' : 147.06841, 'G' : 57.02146, 'H' : 137.05891, 'I' : 113.08406, 'K' : 128.09496, 'L' : 113.08406, 'M' : 131.04049, 'N' : 114.04293, 'P' : 97.05276, 'Q' : 128.05858, 'R' : 156.10111, 'S' : 87.03203, 'T' : 101.04768, 'V' : 99.06841, 'W' : 186.07931, 'Y' : 163.06333}
def peptide_mass(peptide): '''calculate the molecular mass of peptide''' mass = 0 for aa in peptide: mass += masses[aa] return mass for peptide in ['CPHRALIAIT', 'NGQSVCGMSG', 'WPFYWRICNH', 'DLQVIDQMNW', 'CEWIMYVTDE']: print(f'Mass of {peptide} is {peptide_mass(peptide):.5f} Daltons')

Exercise 21.4

Write a function that splits someone's full name into a forename and surname and returns both of these separately.

  • Hint: In Exercise 7.3 you split a string of two sentences into two separate sentences. Use the same technique here but split on a space rather than a full stop.

full_names = ['Harry Potter', 'Hermione Granger', 'Ronald Weasley']
def split_name(full_name): '''Split a full name into forename and surname''' idx = full_name.find(' ') forename = full_name[:idx] surname = full_name[idx+1:] return forename, surname for name in full_names: forename, surname = split_name(name) print(f'{surname.upper()} {forename}')