ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello i was making a scrambler from python and this is what i have
# Scramble word re-arrange characters
import random
scrambler= raw_input("put a word and i will unscramble:")
n=0
Z=[]
while n<len(scrambler):
st.append(scrambler[n])
n=n+1
return(Z)
X=0
L=[]
while X<len(scrambler):
Y=random.randint(0,len(scrambler)-1)
if L.count(Y)==0:
L.append(Y)
X=X+1
return L
X=0
B=''
while n<len(s):
B=B+ Z[L[X]]
n=n+1
return B
and when i try to run it says 'return' outside function
Repost that python code used CODE tags so we can see how it was indented.
In most languages the indentation is important only for readability (so we can follow the intent of your code). But in Python, indentation carries meaning. So without indentation, even the compiler can't understand your code.
As well as what has already been pointed out, you're appending to lists that don't exist (st.append(scrambler[n])), and other nasty things. Perhaps you should tell us what the goal of your code is; but you're also going to have difficulty "unscrambling" anything if you're using random numbers
X=0
L=[]
while X<len(scrambler):
Y=random.randint(0,len(scrambler)-1)
if L.count(Y)==0:
L.append(Y)
Your shuffle algorithm seems non-deterministic in the time that it takes and indeed may never complete a list of positions.
At first I thought you were randomly picking characters and not considering duplicates. Please use [code] tags asap.
You still seem to have the fundamental problem that Snark raises, a random sorting/shuffling isn't a deterministic way to unscramble any randomly shuffled original character sequence. But it should work on average 1/len(scrambler)! times (yep, factorial, as in permutations), more frequently if there's duplicate characters, so that's nice.
You can do a shuffle within a single array or list in this fashion:
Looping through the list from back to front, select a position at random from at-or-in-front-of that "current position" and swap that element with the one at the "current position."
(No, I'm not going to post a code example.)
This algorithm takes advantage of the fact that, as the list of "non-randomized elements" shrinks, the list of "randomized elements" grows by exactly the same amount. Therefore, there's no need to have two lists. The cursor marks the start of the randomized section and therefore the end of the non-randomized section.
True, but I don't see how a shuffle algorithm realistically unscrambles/unshuffles as per the OP. Plus Python strings are apparently like Java in that they're immutable, so you'd need to create a copy of the char array first to use an 'in-place' algorithm, so I just left it at dropping the word shuffle.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.