473,405 Members | 2,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need Help in Scrambling! Thank you

2
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!
Sep 29 '08 #1
4 3569
bvdet
2,851 Expert Mod 2GB
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.
Sep 29 '08 #2
pazzi
2
Thank You very much!
Sep 29 '08 #3
aberry
10
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 :)
Oct 3 '08 #4
aberry
10
repeated entry .. not able to delete it....
Oct 3 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Sofia | last post by:
My name is Sofia and I have for many years been running a personals site, together with my partner, on a non-profit basis. The site is currently not running due to us emigrating, but during its...
6
by: Robert Maas, see http://tinyurl.com/uh3t | last post by:
System login message says PHP is available, so I tried this: http://www.rawbw.com/~rem/HelloPlus/h.php It doesn't work at all. Browser just shows the source. What am I doing wrong?
0
by: Gregory Nans | last post by:
hello, i need some help to 'tree-ify' a string... for example i have strings such as : s = """A(here 's , B(A ) silly test) C(to show D(what kind) of stuff i need))""" and i need to...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.