讲解:program、python、PythonHaskell|

2020-01-12  本文已影响0人  日青天

Program 2I raed smoheewre taht if you take a sentcene and raenarrge the letters isdnie ecah wrod, kepeing thefrsit and last, that the rultses is ualsuly redaalbe. That wkors in prat bcasuee many wdors are srhot, sowodrs that are trehe leetrts or lses are uhngaencd, but also the hmuan biran seems to rigcnzeoe tnighstaht are csole whuotit lknooig for an eacxt match. That’s prpeahs why you can rncozigee semonoe whouasluly 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 thefirst and last, that the results is usually readable. That works in part because many words are short, sowords that are three letters or less are unchanged, but also the human brain seems to recognize thingsthat are close without looking for an exact match. That’s perhaps why you can recognize someone whousually 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 writethe 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 individualletters. 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 li代写program留学生作业、python程序设计作业调试、代写Python课程设计作业 帮做Haskell程序|调试Mst of characters. I suggest you write two functions toassist with that.The first will find the next letter in a list of characters, starting at a specific spot. For example, if you runyour function on the above list with 0 as the start, it should return 0. If you run it with 4 as the start, itshould 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, itshould return 4.With those two functions, you should be able to find contiguous groups of letters within the list, anddetermine 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 youidentify from a text sample, using slices.Then you are ready to rearrange. The shuffle function from random will work great. However, whileyou can scramble the letters in a slice from the larger list, that won’t scramble the letters in the largerlist itself. You will have extract a slice, shuffle it, then to copy the letters from the rearranged slice backinto 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-oneproblems.Anytime you are working with strings and lists, never assume that what you think is in one of yourvariables 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 insize, and show both before and after.转自:http://www.7daixie.com/2019043020184651.html

上一篇 下一篇

猜你喜欢

热点阅读