Preparation

The random1 module from the Python Standard Library2 provides support for generating random numbers. The function random() from this module generates a random floating point number in the range $$[0, 1[$$. The function randint(a, b) can be used to generate a random integer from the range $$[a, b]$$. The following interactive Python session gives some examples on how to use these functions from the random module.

>>> import random
>>> help(random.random)
Help on built-in function random:

random(...)
    random() -> x in the interval [0, 1).
>>> random.random()
0.954131645221452
>>> random.random()
0.3548429482674793

>>> help(random.randint)
Help on method randint in module random:

randint(self, a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.
>>> random.randint(3, 10)
5
>>> random.randint(3, 10)
8

Description

According to a research at an English university, it doesn't matter in what order the letters in a word are. The only important thing is that the first and last letter is at the right place. The rest can be a total mess and you can still read it without problem. This is because we do not read every letter by itself but the word as a whole.

If this is correct, it means that the previous paragraph would still be perfectly readable if the words are distorted in the following way:

Aoccdrnig to a rscheearch at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are. The olny iprmoetnt tihng is taht the frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe.

Assignment

  1. Write a function distort_word that takes a word as its argument. The function must return a distorted version of the given word that meets the following criteria:

    • the first letter of the distorted word is the same as the first letter of the given word

    • the last letter of the distorted word is the same as the last letter of the given word

    • the middle letters of the distorted word (all letters except the first and the last letter) are a random anagram (a random permutation) of the middle letters of the given word

    To determine a random permutation $$p$$ of a string $$s$$, the following procedure can be followed:

    1. initialize the permutation $$p$$ as the empty string

    2. select a random letter from $$s$$

    3. append the selected letter to $$p$$

    4. remove the selected letter from $$s$$

    5. repeat the previous procedure from step (2) until $$s$$ contains no more letters

    This results for example in the following sequence of steps that convert the word biologic into the random permutation ibiogolc, where we have underlined the selected letter of $$s$$ in each step of the procedure:

    $$s$$ $$\rightarrow$$ $$p$$
    biologic    
    bologic   i
    ologic   ib
    ologc   ibi
    olgc   ibio
    olc   ibiog
    lc   ibiogo
    c   ibiogol
        ibiogolc
  2. Use the function distort_word to write a function distort_sentence that takes a sentence as its argument. The function distort_sentence must distort all words in the given sentence according to the above procedure and return the result. The words of the given sentence are defined as the longest possible sequence of letters. All characters that are not letters should remain unchanged and should retain their original position in the distorted sentence. As an example, if the string "Change, America needs change!" is passed to the function, the distorted string "Chnage, Ameicra nedes canhge!" could be returned. Note that this example contains two alternative distortions of the word change.

Example

>>> distort_word('monty')
'motny'
>>> distort_word('python')
'pothyn'

>>> distort_sentence('Nudge, nudge, wink, wink. Know what I mean?')
'Nugde, ngude, wnik, wnik. Konw what I maen?'
>>> distort_sentence('Nobody expects the Spanish inquisition!')
'Nbdooy epxtces the Snpasih iiiitqnsoun!'
>>> distort_sentence("He's not the Messiah - he's a very naughty boy.")
"He's not the Mseasih - he's a vrey nhaugty boy."