Change language

Python | Scramble words from a text file

|

Rules to follow:

  • Words containing no more than 3 characters do not need to be encrypted.
  • Do not encrypt the first and last characters, so scrambling can become Srbmnacilg or Srbmnailcg or Snmbracilg , that is, letters other than the first and last can encrypt in any order.
  • The punctuation at the end of a word to be kept as is, that is, “Amazing” may become “Spsirnirug” but not “Spsirn, irug”.
  • The following punctuation marks must be supported — Semicolon, Semicolon, Semicolon, Exclamation point.
  • Do this for the file and save the sequence of lines.

When the program starts, it should prompt the user for a name input file and generate ciphertext output file. The output file must be named by appending the word "Scrambled" to the input file name.

Example :

  Input:  MyFile. txt -" Scrambling words is very interesting. Because even if they are scrambled, it doesn’t impact our reading. Because we don’t read letter by letter, we read the word as a whole.  Output:  MyFileScrambled.txt -" Srbmnacilg wrods is vrey itrensientg. Bscauee even if tehy are srelabcmd, it dosn’et ipcmat our raidneg. Bacusee we dn’ot raed lteetr by letetr, we raed the wrod as a wolhe. 

Below is the implementation:

import random

 

punct = ( "." , " ; " , "! " , "?" , "," )

count = 0

new_word = ""

 

inputfile = input ( " Enter input file name: " )

 

with open (inputfile, ’r’ ) as fin:

for line in fin.readlines ():  # Read line by line in a text file

 

for word in line.split ():  # Read word by word on each line

  

  if len (word)"  3 # If the word length is" 3

 

& # 39; & # 39; & # 39; If word ends with punctuation

Remove first letter, last letter and punctuation

Shuffle words

Add deleted letters (first letter)

  Add deleted letters (last letter)

Add deleted letters (punctuation marks) & # 39; & # 39; & # 39;

 

if word.endswith (punct):

word1 = word [ 1 : - 2 ]

word1 = random.sample (word1, len (word1))

word1.insert ( 0 , word [ 0 ])

word1.append (word [ - 2 ])

word1.append (word [ - 1 ])

 

& # 39; & # 39; & # 39; If there is no punctuation mark

Remove first letter and last letter

Shuffle word

Add deleted letters (first letter)

Add deleted letters (last letter)

Add a word and “” to the previous words “”

 

else :

  word1 = word [ 1 : - 1 ]

word1 = random.sample (word1, len (word1))

word1.insert ( 0 , word [ 0 ])

word1.append (word [ - 1 ])

new_word = new_word + ’’ .join (word1) + ""

 

  & # 39; & # 39; & # 39; If the word length is "3

just add the word and & quot; & quot; to the previous words & quot;

 

else :

new_word = new_word + word + ""

 

  # & quot; Add to "filename" Scrambled.txt & quot;

 

with open ((inputfile [: - 4 ] + "Scrambled.txt" ), ’a +’ ) as fout:

fout.write (new_word + "" )

 

new_word = ""

Exit:

 Smcinrablg wodrs very Bauscee eevn tehy dnoes’t icpamt Bcuesae d’not read lteter raed wrod whole. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically