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:
    
    unique = []    # 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: # check if a name is in the list
            pass # do nothing
        else: # if the name is not in the list 
            self.unique.append(name.capitalize()) # add the name to the list 

names=NameSet()
names.add_name("Tonya")
names.add_name("Maria")
names.add_name("Joseph")
names.add_name("maria")
names.add_name("WILLIAM")
names.add_name("william")
print(names.unique)
['Tonya', 'Maria', 'Joseph', 'William']



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 [1]:
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 class CountNuc
test_string = "AGct"

nuc_count1=CountNuc() 
for N in test_string:
    nuc_count1.add_nuc(N)
print(nuc_count1.nuc_dict)

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}
In [ ]: