Lab Worksheet 9 Solutions

Problem 1: Write a class NameSet that collects a unique set of names. Within the class, names should be stored in a list. NameSet should accept names in any format (upper case, lower case, etc.), but keep them capitalized.

Hint - when solving this question, these string functions may be useful: upper(), lower(), and capitalize().

In [1]:
class NameSet:
    
    def __init__(self):
        self.unique_list = []    # creates an empty list to keep unique names

    def add_name(self, name):   # if a name is unique, adds it to the list
        if name.capitalize() in self.unique_list: # check if a name is already in the list
            pass 
        else: # if the name is not in the list, then add it
            self.unique_list.append(name.capitalize()) 

names = NameSet()

names.add_name("Isabelle")
names.add_name("isabelle")
names.add_name("Tom")
names.add_name("ToM")
names.add_name("Timmy")
names.add_name("TiMMy")
names.add_name("Coco")
names.add_name("COCO")

print(names.unique_list)
['Isabelle', 'Tom', 'Timmy', 'Coco']

Problem 2: Write a class CountNuc that stores the counts of A's, C's, G's, and T's in a DNA sequence. Your class should be able to accept both upper and lower case DNA sequences. Nucleotide counts in CountNuc should be stored in a dictionary. Once your class has been written, confirm your class operates correctly on a test string "AGct". After that, determine the counts of A's, C's, G's, and T's in dna_string string given below, and print the counts.

In [2]:
class CountNuc:

    def __init__(self):
        self.nuc_dict = {}   # creates a dictionary to keep counts of nucleotides
    
    def add_nuc(self, nuc):   # increments the count for a nucleotide by 1
        nuc = nuc.upper()    # convert a nucleotide to upper case
        
        if nuc in self.nuc_dict: # check if nucleotide is in the dictionary
            self.nuc_dict[nuc] += 1 # increment the count of a nucleotide by 1
        else: # if nucleotide is not in the dictionary
            self.nuc_dict[nuc] = 1 # set the count for a nucleotide to 1

# test the class `CountNuc` with small test string
test_dna_string = "AGct"

nuc_count1 = CountNuc() 

for N in test_dna_string:
    nuc_count1.add_nuc(N)

print(nuc_count1.nuc_dict)

# determine the counts of A's, C's, G's and T's of this string:
dna_string = "ATCGAGCTataCCGATACAGGcTGGTATAAAAgatTC"

nuc_count2 = CountNuc()

for N in dna_string:
    nuc_count2.add_nuc(N)
    
print(nuc_count2.nuc_dict)
{'A': 1, 'G': 1, 'C': 1, 'T': 1}
{'A': 13, 'T': 9, 'C': 7, 'G': 8}