Title | P.A Unit 7 - Programming Assignment Unit 7 |
---|---|
Course | Programming Fundamentals |
Institution | University of the People |
Pages | 2 |
File Size | 33 KB |
File Type | |
Total Downloads | 56 |
Total Views | 160 |
Programming Assignment Unit 7...
def histogram(s): d = dict() for c in s:
# this function iterates through an iterable
if c not in d: #if the element is not in the dictionary it adds to the empty dictionary d as a key and assignes a value of 1 d[c] = 1 else:
# if it does exits as a key then it increments its correponding value by 1
d[c] += 1 return d # after going through all the elements is returns a completed dictionary. Which most of less has the elements # of the iterable been passed as argument as keys and frequency of their apearance as their corresponding values
#PART:1 # here is the duplicates() function that take a list as an argument. def has_duplicates(s): h=histogram(s) easy to read)
# use histogram() function above. (variable h is used to keep code short and
for key,value in h.items(): if value>1: return True # This condition checks if return value of the key of the dictinary returned by histogram function is more than # 1 and return True otherwise it returns false as the element occurs only once. return False
for s in test_dups:
# iterates through each element of test_dump
if has_duplicates(s): #if this conditional is met we execute the print statement" has duplicates" otherwise the else block is run. print(s,"has duplicates")
else: print(s, "has no duplicates")
#PART:2
def missing_letters(s): missings=[]
# an empty list called missings
h=histogram(s)
for e in alphabet: if e not in h: missings.append(e) return " ".join(missings) easier to read.
#the empty string adds space between the appended elements making it
#We join each element to the empty list m
# test_miss is a global variable for s in test_miss: missings=missing_letters(s) if len(missings):
# this simply counts the number of elements present in list m.
print(s,"is missing letters",missings) else: print(s,"uses all the letters")...