Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 29th, 2008, 05:06 AM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default Need Help in Scrambling! Thank you

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!
Reply
  #2  
Old September 29th, 2008, 09:56 AM
bvdet's Avatar
Expert
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,219
Default

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.
Reply
  #3  
Old September 29th, 2008, 05:12 PM
Newbie
 
Join Date: Sep 2008
Posts: 2
Default

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

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 :)
Reply
  #5  
Old October 3rd, 2008, 12:38 PM
Newbie
 
Join Date: Sep 2008
Posts: 6
Default

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles