In: Computer Science
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:
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)
