Created
August 8, 2020 02:00
-
-
Save guestPK1986/c3717cbed6be2b81131b95cec51ad911 to your computer and use it in GitHub Desktop.
course_2_assessment_3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 1. The dictionary Junior shows a schedule for a junior year semester. | |
# The key is the course name and the value is the number of credits. | |
# Find the total number of credits taken this semester and assign it to the variable credits. | |
# Do not hardcode this – use dictionary accumulation! | |
Junior = {'SI 206':4, 'SI 310':4, 'BL 300':3, 'TO 313':3, 'BCOM 350':1, 'MO 300':3} | |
credits = 0 | |
for x in Junior: | |
credits = credits + Junior[x] | |
# 2. Create a dictionary, freq, that displays each character in string str1 as the key and its frequency as the value. | |
freq = {} | |
for x in str1: | |
if x not in freq: | |
freq[x] = 0 | |
freq[x] = freq[x] +1 | |
# 3. Provided is a string saved to the variable name s1. | |
# Create a dictionary named counts that contains each letter in s1 and the number of times it occurs. | |
s1 = "hello" | |
counts = {} | |
for x in s1: | |
if x not in counts: | |
counts[x] = 0 | |
counts[x] = counts[x] +1 | |
# 4. Create a dictionary, freq_words, that displays each word in string str1 as the key and its frequency as the value. | |
str1 = "I wish I wish with all my heart to fly with dragons in a land apart" | |
freq_words = {} | |
str2 = str1.split() | |
for x in str2: | |
if x not in freq_words: | |
freq_words[x] = 0 | |
freq_words[x] +=1 | |
print(freq_words) | |
# 5. Create a dictionary called wrd_d from the string sent, | |
# so that the key is a word and the value is how many times you have seen that word. | |
sent = "Singing in the rain and playing in the rain are two entirely different situations but both can be good" | |
wrd_d = {} | |
w2 = sent.split() | |
for x in w2: | |
if x not in wrd_d: | |
wrd_d[x] = 0 | |
wrd_d[x] +=1 | |
print(wrd_d) | |
# 6. Create the dictionary characters that shows each character from the string sally and its frequency. | |
# Then, find the most frequent letter based on the dictionary. Assign this letter to the variable best_char. | |
sally = "sally sells sea shells by the sea shore" | |
characters = {} | |
for char in sally: | |
if char not in characters: | |
characters[char]=0 | |
characters[char]+=1 | |
keys = characters.keys() | |
best_char = keys[0] | |
for key in keys: | |
if characters[key] > characters[best_char]: | |
best_char = key | |
print(best_char) | |
# 7. Do the same as above but now find the least frequent letter. | |
# Create the dictionary characters that shows each character from string sally and its frequency. | |
# Then, find the least frequent letter in the string and assign the letter to the variable worst_char. | |
sally = "sally sells sea shells by the sea shore and by the road" | |
characters = {} | |
for char in sally: | |
if char not in characters: | |
characters[char]=0 | |
characters[char]+=1 | |
keys = characters.keys() | |
worst_char = keys[0] | |
for key in keys: | |
if characters[key] < characters[worst_char]: | |
worst_char = key | |
print(worst_char) | |
# 9. Create a dictionary called low_d that keeps track of all the characters in the string p | |
# and notes how many times each character was seen. Make sure that there are no repeats of characters as keys, | |
# such that “T” and “t” are both seen as a “t” for example. | |
string1 = "There is a tide in the affairs of men, Which taken at the flood, leads on to fortune. Omitted, all the voyage of their life is bound in shallows and in miseries. On such a full sea are we now afloat. And we must take the current when it serves, or lose our ventures." | |
string2 = string1.lower() | |
letter_counts = {} | |
for x in string2: | |
if x not in letter_counts: | |
letter_counts[x] = 0 | |
letter_counts[x] +=1 | |
print(letter_counts) | |
#10. Create a dictionary called low_d that keeps track of all the characters in the string p | |
#and notes how many times each character was seen. Make sure that there are no repeats of characters as keys, | |
#such that “T” and “t” are both seen as a “t” for example. | |
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat." | |
string2 = p.lower() | |
low_d = {} | |
for x in string2: | |
if x not in low_d: | |
low_d[x] = 0 | |
low_d[x] +=1 | |
print(low_d) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment