0% found this document useful (0 votes)
34 views

Programming Assignment Unit 7

The document contains Python code that defines functions to analyze strings for duplicate characters and missing letters from the alphabet. It defines a histogram function to count the frequency of each character in a string. It then uses this histogram to define functions to check if a string has any duplicate characters and to find which letters are missing from the alphabet. These functions are tested on sample strings and their outputs are displayed.

Uploaded by

tokyo carrie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Programming Assignment Unit 7

The document contains Python code that defines functions to analyze strings for duplicate characters and missing letters from the alphabet. It defines a histogram function to count the frequency of each character in a string. It then uses this histogram to define functions to check if a string has any duplicate characters and to find which letters are missing from the alphabet. These functions are tested on sample strings and their outputs are displayed.

Uploaded by

tokyo carrie
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Part 1

alphabet = "abcdefghijklmnopqrstuvwxyz"

test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]

test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

#The below code turns the lists into strings that can go through the histogram program

test_dups = ''.join(test_dups)

test_miss = ''.join(test_miss)

def histogram(s):

d = dict()

for c in s:

if c not in d:

d[c] = 1

else:

d[c] += 1

return d

#takes the histogram from above and returns true if any of the values are duplicated

def has_duplicates(s):

histo=histogram(s)

for k,v in histo.items():

if v > 1:

return True

else:

return False

#prints has duplicates if there are any duplicates and prints does not have duplicates if there are no
#duplicates

def test_duplicates(s):

if has_duplicates(s)==True:

print(s+' has duplicates')


else:

print(s+' does not have duplicates')

OUTPUT

>>> test_duplicates(alphabet)

abcdefghijklmnopqrstuvwxyz does not have duplicates

>>> test_duplicates(test_dups)

zzzdogbookkeepersubdermatoglyphicsubdermatoglyphics has duplicates

>>> test_duplicates(test_miss)

zzzsubdermatoglyphicthe quick brown fox jumps over the lazy dog has duplicates

Part 2

alphabet = "abcdefghijklmnopqrstuvwxyz"

test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]

test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]

#The below code turns the lists into strings that can go through the histogram program

test_dups = ''.join(test_dups)

test_miss = ''.join(test_miss)

def histogram(s):

d = dict()

for c in s:

if c not in d:

d[c] = 1

else:

d[c] += 1

return d
def missing_letters(s):

histo=histogram(s)

absent_let=[]

for l in alphabet:

if l not in histo:

absent_let.append(l)

return ''.join(absent_let)

def has_letters(s):

if len(missing_letters(s))<1:

print(s+' has all the letters of the alphabet')

elif len(missing_letters(s))>=1:

print(s+' is missing the following letters: '+ missing_letters(s))

OUTPUT

>>> has_letters(test_dups)

zzzdogbookkeepersubdermatoglyphicsubdermatoglyphics is missing the following letters: fjnqvwx

>>> has_letters(test_miss)

zzzsubdermatoglyphicthe quick brown fox jumps over the lazy dog has all the letters of the alphabet

>>> has_letters(alphabet)

abcdefghijklmnopqrstuvwxyz has all the letters of the alphabet

References

Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree
Press.

Ondiekelijah. (n.d.). Ondiekelijah/python-dictionaries. GitHub. Retrieved March 15, 2023, from


https://github.com/ondiekelijah/Python-Dictionaries

Tyler David(1966, September 1). Checking for duplicate letters within lists by using a histogram function. Stack
Overflow. Retrieved March 15, 2023, from
GeeksforGeeks. (2023, February 27). Python - accessing items in lists within dictionary. GeeksforGeeks.
Retrieved March 15, 2023, from https://www.geeksforgeeks.org/python-accessing-items-in-lists-within-
dictionary/

You might also like