Question

Learn how to use the advanced data structures (such as: list, tuple, string, dictionary, and set)...

Learn how to use the advanced data structures (such as: list, tuple, string, dictionary, and set)

·         Learn and practice how to use functions.

·         Learn and practice how to use file I/Os.

·         Appreciate the importance of data validations.

·         Provide appropriate documentation and good programming style.

Python Programming

Description of the problem:

Write a “censor” program that first reads a file with “bad words” such as “sex”, “drug”, “rape”, “kill”, and so on, places them in a set, and then reads an arbitrary text file. The program should write the text to a new file, replacing each letter of any bad word in the text with an asterisk.

Requirements:

·         Filename for the bad words is called:  badWord.txt

·         Filename for the 2nd arbitrary text file is called:  anyFile.txt

·         Filename for the 3rd output file is called:  good.txt


please also provide algorithim for this program and with output showing on the screen
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python 3.6 Code

Note: the indentation of this code is being lost while pasting it here. Refer to the provided screenshot for proper indentation.

#open the file badWord.txt in read mode
f=open("badWord.txt","r")
#create array of lines in badWord.txt
#assuming every line has a single word
temp=f.readlines()
a=[]
#the words will be having a "\n" at their end
#that "\n" needs to be striped so that we can compare the
#exact words
#Array 'a' will contain all words separately
for i in temp:
a.append(i.strip("\n"))
#close the file badWord.txt
f.close()

#ask user for the input file name
print("Enter file name: ")
file_name=input()

#open the arbitrary file
f=open(file_name,"r")
#read all the data as a string
data=f.read()
#create an array of all words by splitting the string
#from " "(spaces)
d=data.split(" ")
d1=[]
#the words are still not as they should be
#words may have a full-stop(.),"\n" or "\t" at ends
#strip the words of any undesired character
for i in d:
temp=i.strip(".")
temp1=temp.strip("\n")
d1.append(temp1.strip("\t"))
#d1 contains pure words which can be compared
#closing the arbitrary file
f.close()
print("Data read from entered file: ")
print(data)
#check for every word in d1 if it is in a
#that is if it is a bad word then
#replace the bad word with "*"
for i in d1:
if i in a:
data=data.replace(i,"*")
#the string data(which was read from the arbitrary file)
#now has bad words replaced with "*"
#it can be written to the new file
print("\nData with replaced bad words: ")
print(data)
#create a new file
f=open("good.txt","w")
f.write(data)
f.close()
print("Good file created!")

Screenshot

badWord.txt

anyFile.txt

good.txt

OUTPUT

Algorithm

>Start

>open file badWord.txt

>assign temp to f.readlines()

>create an empty array 'a'

>for every element in temp array

strip that element with "\n"

add striped element to new array a

>close the file badWord.txt

>read file name

>open the file with entered name

>read data to a string and assign it to 'data variable'

>split data string with " " and create an array of splitted words and name it as d

>create empty array d1

>for every element element in d

strip it of "," and store in temp

strip temp of "\n" and store in temp

strip temp1 of "\t" and add in array d1

>close the file with entered file name

>display the data read from entered file

>loop through all elements of d1

if the element is in the set of bad words then replace that word in data string with "*"

>display the modified data string

>create a new file 'good.txt'

>write the data string to it

>close the file

>Stop

Add a comment
Know the answer?
Add Answer to:
Learn how to use the advanced data structures (such as: list, tuple, string, dictionary, and set)...
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
  • ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple...

    ***How do I insert the Halloween text into this and write the program**** Topics: List, tuple In this lab, you will write a scrambled word game.  The game starts by loading a file containing scrambled word-answer pair separated.  Sample of the file content is shown below.  Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it.  The number of guesses is unlimited.  When the user guesses the correct answer, it asks the user if he/she wants another scrambled...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • C programming. please include comments In this lab, you will learn to read data from and...

    C programming. please include comments In this lab, you will learn to read data from and write data to a file - another use for pointers. SAMPLE PROGRAM There are two sample programs this week - WriteFile.c and ReadFile.c. The first, WriteFile.c, allows the user to enter customer names (first, middle, and last), and writes each name to an output text file, customerNames.txt. formatted as: firstName middlelnitial lastName" After you've run it once, what happens to the names you added...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify...

    Objective: Learn how to -- read string from a file -- write multiple files Problem: Modify the Person class in Lab #4. It has four private data members firstNam (char[20]), day, month, year (all int); and two public member functions: setPerson(char *, int, int, int) and printInfo(). The function setPerson sets the first name and birthday in the order of day, month and year. The function printInfo prints first name and birthday. But it should print the date in the...

  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • please use c++ please write down the input file information please add comments for some codes...

    please use c++ please write down the input file information please add comments for some codes please do not use #include <bits/stdc++.h> we did not learn it yet thank you CSIT 575-Take Home Lab #11: Arrays To learn to code, compile and run a program processing characters. Assignment Plan and code a top-down modular program utilizing ARRAYS to solve the following problem, using at least 3 functions (called from the main() section or from another function) to solve the problem....

  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • does anyone know how to do this? C++ Your task for this assignment is to use...

    does anyone know how to do this? C++ Your task for this assignment is to use C++ language to implement an insertion sort algorithm. 1. Implement an insertion sort with an array to sort 10 arbitrary numbers using the C++ programming language. The program initializes an array with 10 random 3-digit positive integers. The program then sorts the numbers in the array using the insertion sort algorithm. 2. The program displays the original values in the array before the sort...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

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
ADVERTISEMENT