The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to remove sublists from a given list of lists, which are outside a given range.
Next: Write a Python program to find the maximum and minimum values in a given heterogeneous list.
What is the difficulty level of this exercise?
Easy Medium HardTest your Programming skills with w3resource's quiz.
Follow us on Facebook and Twitter for latest update.
Python: Tips of the Day
Enum Class:
Here is another classy approach from Python, no pun intended. Python's enum class will let you create enum object members with constant and unique values which then can be listed, compared or used for identification.
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”.
- Following punctuation marks are to be supported – Comma Question mark, Full stop, Semicolon, Exclamation.
- Do this for a file and maintain sequences of lines.
On executing the program, it should prompt the user to enter input file name and generate an output file with scrambled text. The output file should 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 words is very itrensientg. Bscauee even if tehy are srelabcmd, it dosn'et ipcmat our raidneg. Bacusee we dn'ot raed lteetr by letetr, we raed the word as a wolhe.Below is the implementation :
Python3
import random
punct= (".",Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 0,Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 2,Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 4,Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 6Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 7
Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 8= import0
import1= import3
import5= import7(import9Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 7
random2random3random4random5random6
random7random8 random9 0 1 2
4random8 6 0 8 9
punct1punct2 punct3punct4punct5punct6punct7
punct9=0
=1=2
=1=4
=1=6
=1=8
=1(0
punct9punct2 (4
(5(6= (8(9"."0"."1"."2"."3
(5(6= "."7punct3"."9
(5,1import0,3import0,5
(5,7"."1"."2,5
(5,7"."1(9,5
(5Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 08
=1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 10
=1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 12
=1=6
=1=8
=1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 18
punct9Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 21"."0
(5(6= (8(9"."0"."1(9"."3
(5(6= "."7punct3"."9
(5,1import0,3import0,5
(5,7"."1(9,5
(5import1= import1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 53 Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 54Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 53 Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 56
punct1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 59
Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 60Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 61
4Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 21"."0
punct1import1= import1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 53 6Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 53 Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 56
4Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 76
random2random3Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 80"."1Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 82"."3Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 53 Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 85Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 86Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 87Smcinrablg wodrs very Bauscee eevn tehy dnoes't icpamt Bcuesae d'not read lteter raed wrod whole. 88