Question

In: Computer Science

Modify your program from Learning Journal Unit 7 to read dictionary items from a file and...

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.

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.

Here is my code for assignment 7 that needs to be modified;

alphabet = "abcdefghijklmnopqrstuvwxyz"
test_dups = ["zzz","dog","bookkeeper","subdermatoglyphic","subdermatoglyphics"]
test_miss = ["zzz","subdermatoglyphic","the quick brown fox jumps over the lazy dog"]
def histogram(s):
    d = dict()
    for c in s:
        if c not in d:
            d[c]=1
        else:
            d[c] += 1
    return d
#part 1
def has_duplicates():
    for string in test_dups:
        dictionary=histogram(string)
        duplicates=False
        for ch in dictionary.keys():
            if dictionary[ch] > 1:
                duplicates=True
                break
        if duplicates==True:
            print(string,"has duplicates")
        else:
            print(string,"has no duplicates")
#part 2
def missing_letters(string):
    answer=""
    for ch in alphabet:
        if ch not in string:
            answer=answer+ch
    return answer
has_duplicates()
for i in test_miss:
    answer=missing_letters(i)
    if len(answer)>=1:
        print(i,"is missing letters",answer)
    else:
        print(i,"uses all the letters")

Solutions

Expert Solution

Solution for the above Problem:

/copyable code

def invert_Dictionary(x):

inverse=dict()

for key1 in x.keys():

list_items = x.get(key1)

inverse[list_items]=key1

return inverse

def readfile(file_name):

x=dict()

with open(file_name,'r') as infile:

for line1 in infile.readlines():

line1=line1.strip().split(':')

x[line1[0]]=line1[1]

print('file Read',x)

return x

def writefile(invert_d,file_name):

with open(file_name,'w+') as wtfile:

for key1,value1 in invert_d.items():

wtfile.write(str(key1)+':'+str(value1)+'\n')

def main():

x = readfile('dic.txt')

invt_d = invert_Dictionary(x)

print('Dictionary inverted',invt_d)

writefile(invt_d,'inverted_dic.txt')

main()

Screenshot:

Output:


Related Solutions

Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
At this level your program must read in (from a file) at least four albums and...
At this level your program must read in (from a file) at least four albums and up to 15 tracks for each album. The information read from the file should include: Number of Albums Album title Artist Artwork file name (place your artwork in an /images folder under the main folder where you run the program) The number of tracks The title of each track The file location of each track At this level user interaction must be entirely through...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than...
In Java. Modify the attached GameDriver2 to read characters in from a text file rather than the user. You should use appropriate exception handling when reading from the file. The text file that you will read is provided. -------------------- Modify GaveDriver2.java -------------------- -----GameDriver2.java ----- import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * Class: GameDriver * * This class provides the main method for Homework 2 which reads in a * series of Wizards, Soldier and Civilian information from the user...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT