computer science coursework
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.
Collepals.com Plagiarism Free Papers
Are you looking for custom essay writing service or even dissertation writing services? Just request for our write my paper service, and we'll match you with the best essay writer in your subject! With an exceptional team of professional academic experts in a wide range of subjects, we can guarantee you an unrivaled quality of custom-written papers.
Get ZERO PLAGIARISM, HUMAN WRITTEN ESSAYS
Why Hire Collepals.com writers to do your paper?
Quality- We are experienced and have access to ample research materials.
We write plagiarism Free Content
Confidential- We never share or sell your personal information to third parties.
Support-Chat with us today! We are always waiting to answer all your questions.
