Skip to content

Instantly share code, notes, and snippets.

  • Select an option

  • Save LizaPiya/06e7df0f4a1f7759a54ee9d18d730cf9 to your computer and use it in GitHub Desktop.

Select an option

Save LizaPiya/06e7df0f4a1f7759a54ee9d18d730cf9 to your computer and use it in GitHub Desktop.
lizapiya_course_2_assessment_3
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]
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
## 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
## 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
## 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
## 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
## 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
##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
## 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
@hadrocodium
Copy link

hadrocodium commented Oct 15, 2021

Number 2 - using a dictionary get method

freq = {}

for char in str1:
	freq[char] = freq.get(char, 0) + 1

@hadrocodium
Copy link

hadrocodium commented Oct 15, 2021

Number 3 - using a dictionary get method

counts = {}

for char in s1:
	counts[char] = counts.get(char, 0) + 1

@hadrocodium
Copy link

hadrocodium commented Oct 15, 2021

  1. 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"

words = str1.split()

freq_words = {}

for char in words:
	freq_words[char] = freq_words.get(char, 0) + 1

@hadrocodium
Copy link

hadrocodium commented Oct 16, 2021

  1. Find the least frequent letter.
sally = "sally sells sea shells by the sea shore and by the road"


characters = {}

for char in sally:
	characters[char] = characters.get(char, 0) + 1

worst_char = None

for k in characters:
	if worst_char is None or characters[worst_char] > characters[k]:
		worst_char = k

print(worst_char)

@hadrocodium
Copy link

hadrocodium commented Oct 16, 2021

8 - don't use str as a variable name because is a function name. Your variable str has overwritten the built-in str function. If you try to convert integer to string by str(1) it will not be able to convert because you assigned a string to it.

Because strings are immutable, you have either to assign strings.lower() to the variable like you did, but you need the variable name that does not overwrite the built-in function or you can add strings.lower() in the for loop as is in the following code.

"""
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."

letter_counts = {}

for char in string1.lower():
	letter_counts[char] = letter_counts.get(char, 0) + 1

@NamanJain62
Copy link

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 code is not correct

@NamanJain62
Copy link

Number 2 - using a dictionary get method

freq = {}

for char in str1:
	freq[char] = freq.get(char, 0) + 1

this is not Working bro

@hadrocodium
Copy link

To NamanJain62

Both codes work.

For the first code containing sally string, you need to properly indent code to make it work.

For the second code ("Number 2 - using a dictionary get method"), you need a string str1. I provided another version of the code given by lizapiya. It is essentially the same thing.

I would recommend you to go to runestone academy and look up these exercises in the book Foundations of Python Programming or fopp for short. It is a great way to learn the basics of Python.

@NikitossssiG
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment