Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Read the following paragraph as quickly as you can, and see if you encounter

ID: 662837 • Letter: 1

Question

1.
Read the following paragraph as quickly as you can, and see if you encounter any
diculties.
""""""Aoccdrnig to 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 a porbelm. Tihs is bcuseae we
do not raed ervey lteter by itslef but the wrod as a wlohe."""""
This has been presented as an example of a principle of human reading compre-
hension. If you keep the rst letter and the last letter of a word in their correct
positions, then scramble the letters in between, the word is still quite readable in
the context of an accompanying paragraph. However, it seems that this is a bit of
a myth and not truly based on solid research.1 In short, for longer words the task
is much more dicult. Nonetheless, we are going to imitate the process on some
English text.
The task will be to read in a paragraph from a le, scramble the internal letters of
each word, and then write the result to a le. Handling punctuation is tricky. You
are required to deal with punctuation that comes at the end of a word (period,
question mark, exclamation, etc.)|that is, punctuation is left untouched and does
not count as the nal unscrambled letter. Optionally, one can deal with the more
dicult task of handling all punctuation, such as apostrophes for possessives or
1http://www.balancedreading.com/cambridge.html
1
hyphenated words. Truly randomizing the order of letters is a task for later in the
text, but we can do some reasonable approximations now. Attacking this problem
in a divide-and-conquer way should begin by writing code to scramble the letters
in a word.
Create a di erent solution by de ning scrambling functions for each of the following
approaches: (Each approach counts as a di erent problem)
(a) Rotate letters by 13 (known as ROT13). That is, `a' becomes `n', `b'
becomes `o', . . . , `n' becomes `a', . . . . The chr and its inverse, ord, functions
will be useful.
(b) For each letter choose a random number and rotate that letter by the
random amount. Import random and use the random.randint(a,b) function
where `a' and `b' de ne the range of random numbers returned.
(c) Improve on the above techniques by scrambling the letters by a
method of your choice.

Explanation / Answer

letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x' ,'y', 'z']
def rotate_word(word,n):
        l = ""
        for letter in word:
                rot = ord(letter) + n
                if rot > ord('z'):
                        rot = rot % ord('a') - 27
                      
                elif rot < ord('a'):
                        rot = rot % ord('a') - 97
                      
                else:
                        rot = rot % ord('a')


                #print rot
                l = l + letters[(+rot)%26]
                rot = 0
        return l
#print letters[ord('m') % 97]
print rotate_word("ceaser",13)