-
-
Save cibofdevs/d34c9d200ac240074b4c1bdb2369027c to your computer and use it in GitHub Desktop.
# 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 v in Junior.values(): | |
credits += v | |
print(credits) | |
# 2. Create a dictionary, freq, that displays each character in string str1 as the key and its frequency as the value. | |
from collections import Counter | |
str1 = "peter piper picked a peck of pickled peppers" | |
freq = Counter(str1) | |
for i in str1: | |
print(i, freq[i]) | |
# 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" | |
def char_frequency(s1): | |
dict = {} | |
for n in s1: | |
keys = dict.keys() | |
if n in keys: | |
dict[n] += 1 | |
else: | |
dict[n] = 1 | |
return dict | |
counts = char_frequency(s1) | |
print(counts) | |
# 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" | |
def word_count(str): | |
counts = dict() | |
words = str.split() | |
for word in words: | |
if word in counts: | |
counts[word] += 1 | |
else: | |
counts[word] = 1 | |
return counts | |
freq_words = word_count(str1) | |
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" | |
def word_count(str): | |
counts = dict() | |
words = str.split() | |
for word in words: | |
if word in counts: | |
counts[word] += 1 | |
else: | |
counts[word] = 1 | |
return counts | |
wrd_d = word_count(sent) | |
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 i in sally: | |
characters[i]=characters.get(i,0)+1 | |
sorted(characters.items(), key=lambda x: x[1]) | |
best_char = sorted(characters.items(), key=lambda x: x[1])[-1][0] | |
# 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 i in sally: | |
characters[i]=characters.get(i,0)+1 | |
sorted(characters.items(), key=lambda x: x[1]) | |
worst_char = sorted(characters.items(), key=lambda x: x[1])[-13][0] | |
# 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. | |
import collections | |
p = p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat." | |
low_d = collections.defaultdict(int) | |
for c in p: | |
low_d[c] += 1 | |
for c in sorted(low_d, key=low_d.get, reverse=True): | |
if low_d[c] > 1: | |
print('%s %d' % (c, low_d[c])) |
right answer of question 9
#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.
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
k = p.lower()
print(k)
low_d = {}
for c in k:
if c not in low_d:
low_d[c] = 0
low_d[c] = low_d[c] + 1
here is for #8
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. Instead, 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 letters in string1.lower():
letter_counts[letters] = letter_counts.get(letters, 0) + 1
print(letter_counts)
#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.
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
low_d = dict()
for letter in p.lower():
low_d[letter] = low_d.get(letter, 0) + 1
print(low_d)
letter_counts={}
for i in string1:
x=i.lower()
letter_counts[x]=letter_counts.get(x,0)+1
print(letter_counts)
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 i in string1.lower():
if i not in letter_counts:
letter_counts[i]=0
letter_counts[i]=letter_counts[i]+1
print(letter_counts)
#Here the right answer of 9
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
low_d={}
for c in p.lower():
if c not in low_d:
low_d[c]=0
low_d[c]=low_d[c]+1
Here is #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.
low_d={}
for i in p.lower():
if i not in low_d:
low_d[i]=0
low_d[i]=low_d[i]+1
For #8
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 i in string1.lower():
if i not in letter_counts:
letter_counts[i]=0
letter_counts[i]=letter_counts[i]+1
here is #4
str1 = "I wish I wish with all my heart to fly with dragons in a land apart"
counts=str1.split()
freq_words={}
for count in counts:
if count not in freq_words:
freq_words[count]=0
freq_words[count] += 1
print(freq_words)
#8
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 = {}
string2 = string1.lower()
for i in string2:
if i not in letter_counts:
letter_counts[i] = 1
else:
letter_counts[i] += 1
print(letter_counts)
#9
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
low_d = {}
p2 = p.lower()
for i in p2:
if i not in low_d:
low_d[i] = 1
else:
low_d[i] += 1
print(low_d)
FOR SIMPLIFIED Q2 AND Q 3
str1 = "peter piper picked a peck of pickled peppers"
freq = {}
for c in str1:
if c not in freq:
freq[c] = 0
freq[c] += 1
s1 = "hello"
counts = {}
for c in s1:
if c not in counts:
counts[c] = 0
counts[c] += 1
#q5 easy explained
str1 = "I wish I wish with all my heart to fly with dragons in a land apart"
okay = str1.split()
freq_words = {}
for i in okay:
if i not in freq_words:
freq_words[i] = 0
freq_words[i] = freq_words[i] + 1
#Q.6
sally = "sally sells sea shells by the sea shore and by the road"
characters={}
for i in sally:
if i not in characters:
characters[i]=0
characters[i]=characters[i]+1
print(characters)
k=list(characters.keys())
worst_char=k[0]
for j in k:
if characters[j] < characters[worst_char]:
worst_char=j
print(worst_char)
*Q.8 *
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 i in string1.lower():
if i not in letter_counts:
letter_counts[i]=0
letter_counts[i]=letter_counts[i]+1
print(letter_counts)
Q.9
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
low_d={}
for i in p.lower():
if i not in low_d:
low_d[i]=0
low_d[i]=low_d[i]+1
print(low_d)
#Q.9
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
p = p.lower()
low_d = {}
for j in p:
for i in j:
if i not in low_d:
low_d[i] = 1
else:
low_d[i] += 1
print(low_d)
#Assessment: Dictionary Accumulation - course_2_assessment_3 - My repository in my profile - Simplified Answers
- 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!
Answer:
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]
print(credits)
- Create a dictionary, freq, that displays each character in string str1 as the key and its frequency as the value.
Answer:
str1 = "peter piper picked a peck of pickled peppers"
freq = {}
for char in str1:
if char not in freq:
freq[char] = 0
freq[char] += 1
print(freq)
- 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.
Answer:
s1 = "hello"
counts = {}
for letter in s1:
if letter not in counts:
counts[letter] = 0
counts[letter] += 1
print(counts)
- Create a dictionary, freq_words, that contains each word in string str1 as the key and its frequency as the value.
Answer:
str1 = "I wish I wish with all my heart to fly with dragons in a land apart"
words = str1.split()
freq_words = {}
for word in words:
if word not in freq_words:
freq_words[word] = 0
freq_words[word] += 1
print(freq_words)
- 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.
Answer:
sent = "Singing in the rain and playing in the rain are two entirely different situations but both can be good"
words = sent.split()
wrd_d = {}
for word in words:
if word not in wrd_d:
wrd_d[word] = 0
wrd_d[word] += 1
print(wrd_d)
- 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.
Answer:
sally = "sally sells sea shells by the sea shore"
characters = {}
for chars in sally:
if chars not in characters:
characters[chars] = 0
characters[chars] = characters[chars] + 1
keys = list(characters.keys())
best_char = keys[0]
for key in keys:
if characters[key] > characters[best_char]:
best_char = key
print(characters)
print(best_char)
- 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.
Answer:
sally = "sally sells sea shells by the sea shore and by the road"
characters = {}
for chars in sally:
if chars not in characters:
characters[chars] = 0
characters[chars] = characters[chars] + 1
keys = list(characters.keys())
worst_char = keys[0]
for key in keys:
if characters[key] < characters[worst_char]:
worst_char = key
print(characters)
print(worst_char)
- 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.
Answer:
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():
if char not in letter_counts:
letter_counts[char] = 0
letter_counts[char] = letter_counts[char] + 1
print(letter_counts)
- 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.
Answer:
p = "Summer is a great time to go outside. You have to be careful of the sun though because of the heat."
low_d = {}
for char in p.lower():
if char not in low_d:
low_d[char] = 0
low_d[char] = low_d[char] +1
print(low_d)
question 7.
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:
characters[char] = characters.get(char, 0) + 1
worst_char = None
for current_char in characters:
if worst_char is None or characters[worst_char] > characters[current_char]:
worst_char = current_char
print(worst_char)
If you want to use sorted and lambda functions instead of for loop (which I think is more pythonic) you can do it like following:
sally = "sally sells sea shells by the sea shore and by the road"
characters = {}
for i in sally:
characters[i]=characters.get(i,0)+1
worst_char = sorted(characters, key=lambda k: characters[k], reverse=True)[-1]
print(worst_char)
- 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:
characters[char] = characters.get(char, 0) + 1
best_char = None
for char in characters:
if best_char is None or characters[best_char] < characters[char]:
best_char = char
print(best_char)
If you want to use sorted and lambda functions instead of for loop you can do it like following:
sally = "sally sells sea shells by the sea shore"
characters = {}
for char in sally:
characters[char] = characters.get(char, 0) + 1
best_char = sorted(characters, key=lambda k: characters[k])[-1]
print(best_char)
Can someone help with this coding please, not sure how to start? Thanks
Part 1:
Your job for this lab is to read in a paragraph from user input and create a dictionary using it. The keys for the dictionary will be the words. The values will be the positions of the words. If a word has a period, comma, exclamation mark, or question mark, you must split the string into the word and the character. You can assume that punctuation will only appear at the end of a word followed by a space or the end of the message.
Hint - You may want to make different lists for each type of change you want to make to the message.
Part 2:
You must then print out the list of words with the positions of each. You MUST use the print function provided.
For the input message:
Your job for this lab is to read in a paragraph from user input and create a dictionary utilizing it. The keys for the dictionary will be the words.
Part 3:
Using the dictionary, you must recreate the paragraph and print it out. Hint: Creating a reverse look up function and reusing it for each position number will make this far easier. Leave extra spacing between any punctuation (. , ! ?) and the preceding word.
The input message:
Your job for this lab is to read in a paragraph from user input and create a dictionary using it. The keys for the dictionary will be the words.
str1 = "I wish I wish with all my heart to fly with dragons in a land apart"
freq_words = {}
for i in str1.split():
if i not in freq_words:
freq_words[i]=0
freq_words[i]+=1
print(freq_words)
please help
Create a dictionary, freq, that displays each character in string str1 as the key and its frequency as the value.
str1 = "peter piper picked a peck of pickled peppers"
help #8
help #9
here is for #8
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."
string1.lower()
letter_counts = {}
for c in string1.lower():
if c not in letter_counts:
letter_counts[c] = 0
letter_counts[c] = letter_counts[c] + 1
else:
letter_counts[c] = letter_counts[c] + 1
print(letter_counts)