Connecting Tech Pros Worldwide Help | Site Map

Need Help in Scrambling! Thank you

Newbie
 
Join Date: Sep 2008
Posts: 2
#1: September 29th, 2008, 05:06 AM
I would like to make a code in which I scramble the words in the middle of a sentence so the first and last letter remain the same.

example: The gentleman gave his seat to the lady
= The geltneamn gvae his saet to the lday

please inform me how you do it, so I can understand how to do one.

thank you!
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,554
#2: September 29th, 2008, 09:56 AM

re: Need Help in Scrambling! Thank you


random.shuffle and string slicing is ideal for this task.
Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3. def scramble(w):
  4.     mid = list(w[1:-1])
  5.     random.shuffle(mid)
  6.     return w[0]+''.join(mid)+w[-1]
Now all you need is another function to break apart the sentence by words, process each word, and rejoin. Example:
Expand|Select|Wrap|Line Numbers
  1. s = 'Mr. Harvey was notorious for his tendency to engage in endless circumlocution when a simple, brief explanation would suffice.'
  2. print process(s)
  3.  
  4. >>> Mr. Heavry was nrutooois for his tedcnney to eggnae in esldens citcuuirlmocon wehn a smlpie, beirf eltnioaaxpn wluod suffice.
Newbie
 
Join Date: Sep 2008
Posts: 2
#3: September 29th, 2008, 05:12 PM

re: Need Help in Scrambling! Thank you


Thank You very much!
Newbie
 
Join Date: Sep 2008
Posts: 10
#4: October 3rd, 2008, 07:50 AM

re: Need Help in Scrambling! Thank you


yes, it works fine...
but be sure to strip comma, semicolon char with word...
like in string
str1= "authors responsible for the design, creation, and management "
str1.split() will give
['authors', 'responsible', 'for', 'the', 'design,', 'creation,', 'and', 'management']

so strip comma in string "creation," and then pass to function :)
Newbie
 
Join Date: Sep 2008
Posts: 10
#5: October 3rd, 2008, 12:38 PM

re: Need Help in Scrambling! Thank you


repeated entry .. not able to delete it....
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using 3 buttons in single page ashish1779 answers 2 January 29th, 2008 01:53 PM
is there any instruction or operator to convert a Byte from 10110001 to 10001101? NISARG answers 13 March 16th, 2006 05:25 PM
Where are we? Where is the peak? Are there any way and direction to this top? What are they? Xiangliang Meng answers 3 November 14th, 2005 10:35 AM
How can I let DB2 to use more CPU richardshen@canada.com answers 10 November 12th, 2005 07:31 AM