Question
Answered step-by-step

Part 1 Modify your program from Learning Journal Unit 7 to read...

Part 1


Modify your program from Learning Journal Unit 7 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.

Part 2

Copy your program from Part 1 and modify it to do the following:

  • Read the output file from Part 1 and create a dictionary from it (the inverted dictionary from Part 1).
  • Invert that dictionary.
  • Write the re-inverted dictionary to an output file.

It will be interesting to see if your original dictionary is reversible. If you invert it twice, do you get the original dictionary back?

Include the following in your Learning Journal submission:

  • The input file for your original dictionary (with at least six items).
  • The Python program for Part 1.
  • The output file for your inverted dictionary, which is also the input file for Part 2.
  • The Python program for Part 2.
  • The output file for your twice-inverted dictionary.
  • A description of any differences between your program for Part 1 and your program for Part 2.
  • A description of any differences between the original input file and the final twice-inverted output file.


Reference:

Learning Journal Unit 7

Create a Python dictionary where the value is a list. The key can be whatever type you want.

Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it. Invent the dictionary yourself. Do not copy the design or items from some other source.

Next consider the invert_dict function from Section 11.5 of your textbook.

# From Section 11.5 of:

# Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.

def invert_dict(d):

inverse = dict()

for key in d:

  val = d[key]

if val not in inverse:

inverse[val] = [key]

else:

inverse[val].append(key)

return inverse

Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary.

Run your modified invert_dict function on your dictionary. Print the original dictionary and the inverted one.

Include your Python program and the output in your Learning Journal submission.

Describe what is useful about your dictionary. Then describe whether the inverted dictionary is useful or meaningful, and why.


I'm able to pull out the text file but I'm having trouble doing the assingment:

import os

cwd = os.getcwd()

fin = open('words.txt')

for line in fin:

   word = line.strip()

   print(word)

Answer & Explanation
Verified Solved by verified expert
<p>icitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis.</p> Fusce dui lectus, congue vel laoreet ac, dictum vitae odio. Donec aliquet

Unlock full access to Course Hero

Explore over 16 million step-by-step answers from our library

Subscribe to view answer

lc

sques a molestie consequat, ultrices ac magna.cingonec aliquet. Loremcinglsum dolor sit amet, consectetur adipiscing elit. Nam laciniiscing elit. Nam lacinia pulvinar tortor nec facac, dictum vitamesinusgueamesinamexgue vel laoreet ac, dictum vitae ocing elit.citur laoreet. Nam risua. Fusce dui


acinia

a. Fusce dui lac,,amet, consexuscuscictrices ac magna. Fuipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar torat, ultrices ac marisus,ac,ec facicitur laoreet. Nam risus ante, dapibus a molestie consequat, ultricnec facilisis. P0ultricesfsus antuscegrisus,aciuscrem irisus ante, dapibus a molestie consequarem isumrem i,fficitur laoreet. Namongue verem i,icitur laoreet. Nam risu, consectet

Step-by-step explanation

output.JPG

ec aliquet. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam lacinia pulvinar tortor nec facilisis. Pellentesque dapibus efficitur laoreet. Nam risus ante, dapibus a molestie consequat, ultrices ac magna. Fusce dui lectus, congue vel laoreet ac, dictum vita
1 Attachment
output.JPG
JPG
Student reviews
63% (30 ratings)