Question

Program Language: PYTHON Consider the following file structure: each line of the file contains a word....

Program Language: PYTHON

Consider the following file structure: each line of the file contains a word. The words are in sorted order. For example, a file might look like this

apple
apple
apple
apple
banana
bargain
brick
brick
sample
sample
simple
text
text
text

Write a program that asks the user for a filename with this structure. The program's job is to write the sequence of words to another file, without any duplicates. Name the output file  result.txt. Each word is on its own line. Using the input file example above, result.txt would contain the following lines:
apple
banana
bargain
brick
sample
simple
text

Do not store the words in any type of data structure, such as a list (we learn those later) or a set. Your program has to determine if it writes a word to the output file or not as is reads in the data.

HINT: Consider someone hands you a deck of cards; the deck is face down. You are told that the cards are sorted. You don't know if it is a full deck though - some cards may be missing. You can only turn over the cards one at a time; you may hold at most two cards at a time. Your job is to create a pile where there are no duplicates and discard the rest. How would you do it? Once you've done this by hand, consider what information you needed and the logic you used, then write the code to replicate that algorithm.

Here are 2 files to test your program. You can also make your own text file for testing. Your solution should work with any file that matches the described format.

  • data.txtLinks to an external site.
  • otherData.txtLinks to an external site.

If the program cannot find your file, it will crash and raise an error to the user (those red messages). If you want to experiment, use a try/except block to display an error message to the user and then ask for a new filename (see section 6.4). This is not a requirement for the lab.

The only file you have to sumbit is the .py file. I don't need the text files.

data.txt

apple
apple
apple
apple
banana
brick
brick
sample
sample
text
text
text

otherData.txt

a
b
c
c
c
c
c
c
d
d
d
d
d
d
d
d
f
f
f
f
f
f
f
f
f
f
f
g
0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''
Python version : 2.7
Python program to read words from a file containing one word per line and
output the words to another file such that duplicate words from input file
are not copied to output file
'''

def main():
   # input of input filename  
   inFilename = raw_input('Enter input filename : ')
   # open input and output file
   inFile = open(inFilename)
   outFile = open("result.txt","w")
   # read a word from file
   word = inFile.readline()
   previousWord = "" # variable to store the last word read
   # loop continues till the end of file
   while word:
       # if previous read word is not the same as current read word
       if previousWord != word.strip():
           # update previousWord, strip is used to remove whitespaces from word
           previousWord = word.strip()
           outFile.write(word) # output the word to output file
       word = inFile.readline() # read next word from input file
  
   # check that the last word read was not the same as previousWord
   if previousWord != word:
       # if not same output the word to output file
       outFile.write(word.strip())
   # close both the files  
   outFile.close()
   inFile.close()

#call the main function  
main()  
#end of program

Code Screenshot:

Output:

Input file 1:

Output file 1:

Input file 2:

Output file 2:

Add a comment
Know the answer?
Add Answer to:
Program Language: PYTHON Consider the following file structure: each line of the file contains a word....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads a series of words (one word per line) from a file...

    Write a program that reads a series of words (one word per line) from a file named data.txt. Each word in the file should have each of its characters shifted by 1 character value in the ASCII table (incremented) and then that new word with its characters shifted should be printed to a new file named result.txt. Each word from data.txt should be reprinted with its new encoding in result.txt. Your program should not have any knowledge of how many...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • Python Programming Write a program that counts how often a word occurs in a text file....

    Python Programming Write a program that counts how often a word occurs in a text file. Input: Ask the user for the name of an ASCII text file. Output: Display the numbers of top frequently appeared words and their frequencies. Pseudocode: 1) Read the file 2) Split the file into words 3) Count each word 4) Sort the words by frequencies, starting with the most frequent ones 5) Internally you should use some sort of data structure to keep track...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Capitalization JAVA In this program, you will read a file line-by-line. For each line of data...

    Capitalization JAVA In this program, you will read a file line-by-line. For each line of data (a string), you will process the words (or tokens) of that line one at a time. Your program will capitalize each word and print them to the screen separated by a single space. You will then print a single linefeed (i.e., newline character) after processing each line – thus your program will maintain the same line breaks as the input file. Your program should...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT