computer science coursework
Need help with this assignment?Get an original answer from a qualified tutor — from $10/page.
Get it written →Modify your program from the coding below to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:
- How to format each dictionary item as a text string in the input file.
- How to covert each input string into a dictionary item.
- How to format each item of your inverted dictionary as a text string in the output file.
Create an input file with your original three-or-more items and add at least three new items, for a total of at least six items.
Include the following in your Learning Journal submission:
- The input file for your original dictionary (with at least six items).
- The Python program to read from a file, invert the dictionary, and write to a different file.
- The output file for your inverted dictionary.
- A description of how you chose to encode the original dictionary and the inverted dictionary in text files.
- Use uploaded text Think Python: How to think like a computer scientist chapter 11-13 if you want to
This program create a dictionary that counts the number of time the letter appears in the word. the inverted dictionary shows the frequency and the respective letter repeated that much
[32]: # creating a dictionary function
def histogram(s):
d = dict()
for c in s:
if c not in d:
d[c] = 1
else:
d[c] += 1
return d
[33]: # dictionary from word
h = histogram (‘ FrequencyDictionary ‘)
# calling the dictionary
h [33]:
{‘F’: 1,
‘r’: 1,
‘e’: 1,
‘q’: 1,
‘u’: 1,
‘n’: 1,
‘c’: 1,
‘y’: 2,
‘D’: 1,
‘i’: 1,
‘t’: 1,
‘o’: 1,
‘a’: 1}
[34]: # function to reverse the dictionary
def invert(x):
inverse=dict()
for key in x:
value=x[key]
if value not in inverse:
inverse[value]=[key]
else:
inverse[value] .append(key)
return inverse
[35]: invert=invert(h)
[36]: # printing the original dictionary print(h)
{‘F’: 1, ‘r’: 1, ‘e’ : 1, ‘q’ : 1, ‘u’: 1, ‘n’ : 1, ‘c’: 1, ‘y’: 2, ‘D’: 1, ‘i’: 1, ‘t’: 1, ‘o’: 1, ‘a’: 1}
[37]: # printing the inverted Dictionary print(invert)
{ 1: [ ‘F’, ‘r’, ‘e’, ‘q’, ‘u’, ‘n’, ‘c’, ‘D’, ‘i’, ‘t’, ‘o’, ‘a’], 2: [ ‘y’ ] }
Reference
Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.
Get a plagiarism-free answer to this question
Send us your instructions and we’ll match you with the best writer in your subject.
- 100% human-written, zero AI
- Turnitin report included
- Confidential — we never share your data
- Free revisions & refunds