-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetermineDuplicate-MissingLetters.py
143 lines (67 loc) · 2.68 KB
/
DetermineDuplicate-MissingLetters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#Start with the following Python code.
alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
#Part 1
#Write a function called has_duplicates()
#that takes a string parameter and returns True
#if the string has any repeated characters
def has_duplicates(s):
#Call the function histogram and store the resultant
#dictionary in a variable letter_count.
letter_count = histogram(s)
#Run the loop to access each letter present in 'letter_count'.
for letter in letter_count:
#If the count of the current letter is
#greater than 1 then, return True.
if letter_count[letter] > 1:
return True
#Otherwise, return False.
return False
#Run the loop to access each string in test_dups.
for curr_string in test_dups:
#Call the function has_duplicates() and print the duplicate information
#for the current string.
if(has_duplicates(curr_string)):
print(curr_string + " has duplicates")
else:
print(curr_string + " has no duplicates")
#Part 2
#Define the function missing letters().
def missing_letters(s):
#Use the global variable alphabet.
global alphabet
#Define a variable to store the missing letters.
new_string = ""
#Get the histogram of the string 's'.
letter_count = histogram(s)
#Run the loop to access each alphabet one by one.
for curr_letter in alphabet:
#If the current letter is not present in
#the histogram then, add it to the string
#containing the missing letters.
if curr_letter not in letter_count:
new_string += curr_letter
#Return the string containing the missing letters.
return new_string
#Run the loop to access each string in test_miss.
for curr_string in test_miss:
#Call the function() missing_letters to get
#the letters that are missing from the current string.
miss_letters = missing_letters(curr_string)
#If there are no missing letters in the current string then, print the
#required information.
if miss_letters == "":
print(curr_string + " uses all the letters")
#Otherwise, output the missing letters
#for the current string.
else:
print(curr_string +" is missing "+ miss_letters)