-
-
Save LizaPiya/06e7df0f4a1f7759a54ee9d18d730cf9 to your computer and use it in GitHub Desktop.
lizapiya_course_2_assessment_3
This file contains hidden or 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
| 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 credit in Junior: | |
| credits=credits+Junior[credit] |
This file contains hidden or 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
| 2. Create a dictionary, freq, that displays each character in string str1 as the key and its frequency as the value. | |
| freq={} | |
| for achar in str1: | |
| if achar not in freq: | |
| freq[achar]=0 | |
| freq[achar]=freq[achar]+1 |
This file contains hidden or 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
| ## 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 achar in s1: | |
| if achar not in counts: | |
| counts[achar]=0 | |
| counts[achar]=counts[achar]+1 |
This file contains hidden or 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
| ## Create a dictionary, freq_words, that contains 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={} | |
| splitted=str1.split() | |
| for aword in splitted: | |
| if aword not in freq_words: | |
| freq_words[aword]=0 | |
| freq_words[aword]=freq_words[aword]+1 |
This file contains hidden or 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
| ## 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" | |
| splitted=sent.split() | |
| print (splitted) | |
| wrd_d={} | |
| for aword in splitted: | |
| if aword not in wrd_d: | |
| wrd_d[aword]=0 | |
| wrd_d[aword]=wrd_d[aword]+1 |
This file contains hidden or 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
| ## 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 achar in sally: | |
| if achar not in characters: | |
| characters[achar]=0 | |
| characters[achar]=characters[achar]+1 | |
| ks=list(characters.keys()) | |
| print (ks) | |
| best_char=ks[0] | |
| for mostfreq in characters: | |
| if characters[mostfreq] > characters[achar]: | |
| best_char=mostfreq | |
This file contains hidden or 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
| ## 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 achar in sally: | |
| if achar not in characters: | |
| characters[achar]=0 | |
| characters[achar]=characters[achar]+1 | |
| ks=list(characters.keys()) | |
| print (ks) | |
| worst_char=ks[0] | |
| for keys in characters: | |
| if characters[keys] < characters[achar]: | |
| worst_char=keys |
This file contains hidden or 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
| ##Create a dictionary named letter_counts that contains each letter and the number of times it occurs in string1. Challenge: Letters should not be counted separately as upper-case and lower-case. Intead, all of them should be counted as lower-case. | |
| 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." | |
| str=string1.lower() | |
| print (str) | |
| letter_counts={} | |
| for aletter in str: | |
| if aletter not in letter_counts: | |
| letter_counts[aletter]=0 | |
| letter_counts[aletter]=letter_counts[aletter]+1 |
This file contains hidden or 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
| ## 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." | |
| low_d={} | |
| pa=p.lower() | |
| print (pa) | |
| for aletter in pa: | |
| if aletter not in low_d: | |
| low_d[aletter]=0 | |
| low_d[aletter]=low_d[aletter]+1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sally = "sally sells sea shells by the sea shore"
characters = {}
for i in list(sally):
characters[i] = characters.get(i, 0) + 1
best_char = max(characters, key=characters.get)