联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Python编程Python编程

日期:2019-04-29 09:26

Program 2

I raed smoheewre taht if you take a sentcene and raenarrge the letters isdnie ecah wrod, kepeing the

frsit and last, that the rultses is ualsuly redaalbe. That wkors in prat bcasuee many wdors are srhot, so

wodrs that are trehe leetrts or lses are uhngaencd, but also the hmuan biran seems to rigcnzeoe tnighs

taht are csole whuotit lknooig for an eacxt match. That’s prpeahs why you can rncozigee semonoe who

uasluly wears glssaes wehn taht psreon is whtiout tehm.Huh.

Huh?

That started out like this:

I read somewhere that if you take a sentence and rearrange the letters inside each word, keeping the

first and last, that the results is usually readable. That works in part because many words are short, so

words that are three letters or less are unchanged, but also the human brain seems to recognize things

that are close without looking for an exact match. That’s perhaps why you can recognize someone who

usually wears glasses when that person is without them.

This program is not about the recognizing part; it’s about the rearranging letters part. You are to write

the Python to produce something like the first paragraph above.

There are a few ideas that make this reasonable.

While you can’t rearrange letters in a string, you can make the string into a list containing the individual

letters. The list function does that.

Lists are mutable, so you can work with the list and rearrange things.

Then you can make the list of letters back into a string with "".join( … )

Try that first – make a string into a list of letters, then turn it back into a string.

list("This string is now a list.")

['T', 'h', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', ' ', 'i', 's', ' ',

'n', 'o', 'w', ' ', 'a', ' ', 'l', 'i', 's', 't', '.']

You need to be able to identify words within a list of characters. I suggest you write two functions to

assist with that.

The first will find the next letter in a list of characters, starting at a specific spot. For example, if you run

your function on the above list with 0 as the start, it should return 0. If you run it with 4 as the start, it

should return 5, since the 4 position holds a blank. That function will help you find the start of words.

The second is similar, but finds the next NON-letter. If you run it on the above list with 0 as the start, it

should return 4.

With those two functions, you should be able to find contiguous groups of letters within the list, and

determine if it is long enough to be changed.

I suggest that you get those working before you do anything further. As a test, print the words you

identify from a text sample, using slices.

Then you are ready to rearrange. The shuffle function from random will work great. However, while

you can scramble the letters in a slice from the larger list, that won’t scramble the letters in the larger

list itself. You will have extract a slice, shuffle it, then to copy the letters from the rearranged slice back

into the appropriate places in the larger list.

You only have to shuffle the middle in words of 4 or more characters. Short words don’t change.

Start with a small text sample, print everything of conceivable interest while you are developing this,

and work on one thing at a time. Don’t try to do everything at once. You will likely have off-by-one

problems.

Anytime you are working with strings and lists, never assume that what you think is in one of your

variables is what is really there. Look at it and confirm.

To turn in: show how your program works with a text sample. Use something at least a quarter-page in

size, and show both before and after.


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp