0% found this document useful (0 votes)
14 views

Assignment2 Part2

Python 2

Uploaded by

amanvverma109
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Assignment2 Part2

Python 2

Uploaded by

amanvverma109
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/12/24, 8:53 PM assignment2_part2

Assignment2_part2 (Score: 9.0 / 9.0)


1. Test cell (Score: 1.0 / 1.0)
2. Test cell (Score: 1.0 / 1.0)
3. Test cell (Score: 1.0 / 1.0)
4. Test cell (Score: 1.0 / 1.0)
5. Test cell (Score: 1.0 / 1.0)
6. Test cell (Score: 1.0 / 1.0)
7. Test cell (Score: 1.0 / 1.0)
8. Test cell (Score: 1.0 / 1.0)
9. Test cell (Score: 1.0 / 1.0)

Assignment 2 (part two)¶


Dictionary Accumulation

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!

In [1]:

Student's answer (Top)

Junior = {'SI 206':4, 'SI 310':4, 'BL 300':3, 'TO 313':3, 'BCOM 350':1, 'MO 300':3}
credits = None

credits = sum(Junior.values())

# Print the total credits


print(credits)

18

In [2]:
Grade cell: cell-c8fa510d6d4f279a Score: 1.0 / 1.0 (Top)

Create a dictionary, freq , that displays each character in string str1 as the key and its frequency
as the value.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 1/9
11/12/24, 8:53 PM assignment2_part2

In [3]:

Student's answer (Top)

str1 = 'peter piper picked a peck of pickled peppers'


freq = {}

# Iterate through each character in the string


for char in str1:
# If the character is already in the dictionary, increment its count
if char in freq:
freq[char] += 1
# If the character is not in the dictionary, add it with a count of 1
else:
freq[char] = 1

# Print the dictionary to see the frequency of each character


print(freq)

{'p': 9, 'e': 8, 't': 1, 'r': 3, ' ': 7, 'i': 3, 'c': 3, 'k': 3, 'd': 2, 'a': 1, 'o':

In [4]:

Grade cell: cell-eb6028eb9928ebb3 Score: 1.0 / 1.0 (Top)

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.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 2/9
11/12/24, 8:53 PM assignment2_part2

In [5]:

Student's answer (Top)

s1 = 'hello'
counts = {}

# Iterate through each character in the string


for char in s1:
# If the character is already in the dictionary, increment its count
if char in counts:
counts[char] += 1
# If the character is not in the dictionary, add it with a count of 1
else:
counts[char] = 1

# Print the counts dictionary


print(counts)

{'h': 1, 'e': 1, 'l': 2, 'o': 1}

In [6]:

Grade cell: cell-38a7e977dc21a1f9 Score: 1.0 / 1.0 (Top)

Create a dictionary, freq_words , that contains each word in string str1 as the key and its
frequency as the value.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 3/9
11/12/24, 8:53 PM assignment2_part2

In [7]:

Student's answer (Top)

str1 = 'I wish I wish with all my heart to fly with dragons in a land apart'
#str1 = 'I wish I wish with all my heart to fly with dragons in a land apart'

# Initialize an empty dictionary to store word frequencies


freq_words = {}

# Split the string into words


words = str1.split()

# Iterate through each word in the list


for word in words:
# If the word is already in the dictionary, increment its count
if word in freq_words:
freq_words[word] += 1
# If the word is not in the dictionary, add it with a count of 1
else:
freq_words[word] = 1

# Print the freq_words dictionary


print(freq_words)

{'I': 2, 'wish': 2, 'with': 2, 'all': 1, 'my': 1, 'heart': 1, 'to': 1, 'fly': 1, 'dra

In [8]:

Grade cell: cell-fffebcf0817fb5a3 Score: 1.0 / 1.0 (Top)

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.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 4/9
11/12/24, 8:53 PM assignment2_part2

In [9]:

Student's answer (Top)

sent = 'Singing in the rain and playing in the rain are two entirely different situ
wrd_d = {}

# Split the sentence into words


words = sent.split()

# Iterate through each word in the list


for word in words:
# If the word is already in the dictionary, increment its count
if word in wrd_d:
wrd_d[word] += 1
# If the word is not in the dictionary, add it with a count of 1
else:
wrd_d[word] = 1

# Print the wrd_d dictionary


print(wrd_d)

{'Singing': 1, 'in': 2, 'the': 2, 'rain': 2, 'and': 1, 'playing': 1, 'are': 1, 'two':

In [10]:

Grade cell: cell-b77327c91f457b27 Score: 1.0 / 1.0 (Top)

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 .

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 5/9
11/12/24, 8:53 PM assignment2_part2

In [11]:

Student's answer (Top)

sally = 'sally sells sea shells by the sea shore'


characters = {}

# Iterate through each character in the string


for char in sally:
# If the character is already in the dictionary, increment its count
if char in characters:
characters[char] += 1
# If the character is not in the dictionary, add it with a count of 1
else:
characters[char] = 1

# Find the character with the highest frequency


best_char = max(characters, key=characters.get)

# Print the dictionary and the most frequent character


print(characters)
print("The most frequent character is:", best_char)

{'s': 8, 'a': 3, 'l': 6, 'y': 2, ' ': 7, 'e': 6, 'h': 3, 'b': 1, 't': 1, 'o': 1, 'r':
The most frequent character is: s

In [12]:

Grade cell: cell-29e7463079387e6f Score: 1.0 / 1.0 (Top)

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 .

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 6/9
11/12/24, 8:53 PM assignment2_part2

In [13]:

Student's answer (Top)

sally = 'sally sells sea shells by the sea shore and by the road'
characters = {}

# Iterate through each character in the string


for char in sally:
# If the character is already in the dictionary, increment its count
if char in characters:
characters[char] += 1
# If the character is not in the dictionary, add it with a count of 1
else:
characters[char] = 1

# Find the character with the lowest frequency (ignore spaces or other characters if
worst_char = min(characters, key=characters.get)

# Print the dictionary and the least frequent character


print(characters)
print("The least frequent character is:", worst_char)

{'s': 8, 'a': 5, 'l': 6, 'y': 3, ' ': 11, 'e': 7, 'h': 4, 'b': 2, 't': 2, 'o': 2, 'r'
The least frequent character is: n

In [14]:

Grade cell: cell-81bcdaa409766969 Score: 1.0 / 1.0 (Top)

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.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 7/9
11/12/24, 8:53 PM assignment2_part2

In [15]:

Student's answer (Top)

string1 = "There is a tide in the affairs of men, Which taken at the flood, leads o
letter_counts = {}

# Convert the string to lowercase to ensure case-insensitivity


string1 = string1.lower()

# Iterate through each character in the string


for char in string1:
# Only count letters, ignore other characters (e.g., spaces, punctuation)
if char.isalpha():
if char in letter_counts:
letter_counts[char] += 1
else:
letter_counts[char] = 1

# Print the letter_counts dictionary


print(letter_counts)

{'t': 19, 'h': 11, 'e': 29, 'r': 12, 'i': 14, 's': 15, 'a': 17, 'd': 7, 'n': 15, 'f':

In [16]:
Grade cell: cell-d01025e4e9d1d5d4 Score: 1.0 / 1.0 (Top)

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.

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 8/9
11/12/24, 8:53 PM assignment2_part2

In [17]:

Student's answer (Top)

p = "Summer is a great time to go outside. You have to be careful of the sun though
low_d = {}

# Convert the string to lowercase to ensure case insensitivity


p = p.lower()

# Iterate through each character in the string


for char in p:
# Only count alphabetic characters (ignoring spaces and punctuation)
if char.isalpha():
if char in low_d:
low_d[char] += 1
else:
low_d[char] = 1

# Print the low_d dictionary


print(low_d)

{'s': 5, 'u': 7, 'm': 3, 'e': 12, 'r': 3, 'i': 3, 'a': 6, 'g': 3, 't': 9, 'o': 8, 'd'

In [18]:
Grade cell: cell-642520e26103eb1b Score: 1.0 / 1.0 (Top)

This assignment was graded by mooc_python3:193e72412036, v1.15.070824

https://www.coursera.org/api/rest/v1/executorruns/richfeedback?id=AY7MUZ4HEe-AEBJMcgTSCQ&feedbackType=HTML 9/9

You might also like