Question

In: Computer Science

Given some data in a text file, the task is to scramble the text and output...

Given some data in a text file, the task is to scramble the text and output in a separate text file. So, we need to write a Python program that reads a text file, scrambles the words in the file and writes the output to a new text file.

Rules to be followed:

  • Words less than or equal to 3 characters need not be scrambled.
  • Don’t scramble first and last char, so Scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg, i.e. letters except first and last can be scrambled in any order.
  • Punctuation at the end of the word to be maintained as is i.e. “Surprising, ” could become “Spsirnirug, ” but not “Spsirn, irug”

Solutions

Expert Solution

ANSWER:

I have provided the properly commented and indented code so you can easily copy the code as well as check for correct indentation.
I have provided the output image of the code so you can easily cross-check for the correct output of the code.
Have a nice and healthy day!!

CODE

# importing function random
import random

# defining function scrambleWord to scramble the word
def scrambleWord(word):
    # first checking if word length is less than or equal to 3,
    # returning the word as it is
    if len(word)<=3:
        return word
    
    # otherwise,
    # finding first and last index of word from where to start and end scrambling
    
    # checking for punctuations too,
    # defining punctuations string
    punctuations = """ !"#$%&'()*+, -./:;<=>?@[\]^_`{|}~ """
    
    # let first index be 1, if its a punctuation incrementing the index if its punctuation
    first_index = 1
    if word[0] in punctuations:
        first_index += 1
    # checking same for end_index
    end_index = len(word) - 2
    if word[end_index+1] in punctuations:
        end_index -= 1
    
    # if difference between first_index and end_index is less than 1, returning word as it is
    if (end_index-first_index)<1:
        return word
    
    # otherwise, scrambling it
    # In python string can not be modified element wise,
    # So, first converting string to list, using list function
    wordlst = list(word)
    # fetching sublist which is to be scrambled
    scramble_list = wordlst[first_index:end_index+1]
    
    # using random.shuffle function to shuffle list
    random.shuffle(scramble_list)
    
    # adding scramble_list back to main word
    wordlst[first_index:end_index+1] = scramble_list
    
    # converting list back to word string , using join method of string
    srambword = "".join(wordlst)
    
    return srambword



# reading a text file "text.txt"
file = open("text.txt",'r')

# opening file to write in
writefile = open('output.txt','w')

# looping text line by line
for line in file:
    # spliting words by spaces, using split method of string
    words = line.split()
    # looping to each word in words and calling function scrambleWord to 
    # scramble the word according to requirements
    # defining empty list scramblewords to store modified words
    scramblewords = []
    for word in words:
        # calling function scrambleWord
        sramword = scrambleWord(word)
        
        # appending to scramblewords list
        scramblewords.append(sramword)
        
    # converting the scramblewords list to string line, using join method of string
    scrambleline = " ".join(scramblewords)
    # writing line in writefile
    print(scrambleline,file=writefile)
    
# closing files
file.close()
writefile.close()
    
    

INPUT IMAGE(text.txt)

OUTPUT IMAGE(output.txt)


Related Solutions

Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
Using C++, write a code that this program always stores text file output into a text...
Using C++, write a code that this program always stores text file output into a text file named "clean.txt". -The program should read one character at a time from "someNumbers.txt", and do the following. -If it is a letter, print that letter to the screen, AND also store it in the text file. All letters should be converted to lowercase beforehand. -If it is a number, print that number to screen, but do NOT store it in the text file....
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
Download the file data.csv (comma separated text file) and read the data into R using the...
Download the file data.csv (comma separated text file) and read the data into R using the function read.csv(). Your data set consists of 100 measurements in Celsius of body temperatures from women and men. Use the function t.test() to answer the following questions. Do not assume that the variances are equal. Denote the mean body temperature of females and males by μFμF and μMμMrespectively. (a) Find the p-value for the test H0:μF=μMH0:μF=μM versus HA:μF≠μM.HA:μF≠μM. Answer (b) Are the body temperatures...
Whats the code to open a text file and every line in that text file that...
Whats the code to open a text file and every line in that text file that starts with # then it should delete that line In python using .strip
Develop a C++ program that looks for a given value in a text file full of...
Develop a C++ program that looks for a given value in a text file full of integer values Prompt the user for a value to search for in the file No input validation is required Open the accompanying text file named numbers.txt Search the contents of the text file You may not "hard-code" the quantity of values found in the file... Use a loop of some sort until the end of the text file is reached Maintain how many many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT