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

Q4. Assume you have been given a scrambled text file with some hidden text data similar...
Q4. Assume you have been given a scrambled text file with some hidden text data similar to the one in your assessment. What will be the best method that you will use to unscramble the file and why would you choose this method? Justify your answer. [5 marks] You have collected a digital evidence from a crime scene and calculated its hash value using WinHex editor with MD5 algorithm. You have stored the evidence in a forensics lab. After a...
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...
in java please!!! suppose there is a text file named TextFile.txt with some data in the...
in java please!!! suppose there is a text file named TextFile.txt with some data in the root directory (e.g. C:\ or \) of your computer. In JAVA, write code that (i) opens the file in read mode; (ii) displays the file content on the monitor screen/console; (iii) uses exception handling mechanism with try, catch, and finally blocks; and (iv) safely closes the file. Writing the main() method is optional.
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...
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. (Must use the code below without changing algorithms) 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...
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....
Selection sort on fstream data C++: Sort the given student information data inside a text file...
Selection sort on fstream data C++: Sort the given student information data inside a text file The format of the student data inside the text file is: Student Number, Surname, First Name, Middle Name, Birthday, Course, Year, School Year (The list can be updated over time so there is no limit for the size of the data) But I need to automatically sort the given the data in ascending alphabetical order based on their surnames when displaying the list. I...
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...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a...
C++ Data Structures: Use Huffman coding to encode text in given file (Pride_and_Prejudice.txt). TO_DO: Define a struct for Huffman tree node. This struct contains links to left/right child nodes, a character, and its frequency.Define a function for file reading operation. This function should take in a filename (string type) as parameter and return a proper data structure object that contains characters and their frequencies that will be used to generate Huffman tree nodes.The construction of Huffman tree requires taking two...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT