LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-28-2011, 11:11 AM   #1
newton
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Rep: Reputation: Disabled
scrambler for python


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

can anybody tell me what im doing wrong ??
 
Old 07-28-2011, 01:08 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Whilst I don't know python, my instinct from experience in other languages would be that you have a return statement which is unreachable.

eg:

Code:
if (this) then
  return 1
else
  return 2

return 3
^^ the return 3 can never be reached so would throw up a compiler error (in some languages).
 
Old 07-28-2011, 01:12 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by newton View Post
python and this is what i have
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.
 
Old 08-02-2011, 06:18 PM   #4
Julian Andrews
LQ Newbie
 
Registered: Jan 2011
Distribution: Ubuntu
Posts: 21

Rep: Reputation: 13
The 'return' statement in python is only allowed inside a function definition. If you just want to output a value to stdout use 'print' instead.

I'm finding it pretty hard to figure out what this code is supposed to do. What do you mean by 'scrambler'?
 
Old 08-03-2011, 04:09 AM   #5
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
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
 
Old 08-03-2011, 06:32 AM   #6
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Code:
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.

Last edited by Proud; 08-03-2011 at 06:35 AM.
 
Old 08-03-2011, 09:22 AM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
Old 08-03-2011, 09:38 AM   #8
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
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.
 
Old 08-03-2011, 11:18 AM   #9
Julian Andrews
LQ Newbie
 
Registered: Jan 2011
Distribution: Ubuntu
Posts: 21

Rep: Reputation: 13
If you're just trying to shuffle the elements in a string:

Code:
import random

s = raw_input("Give me a string: ")
l = list(s)
random.shuffle(l)
print ''.join(l)
should print a shuffled string.
 
Old 08-03-2011, 01:39 PM   #10
newton
LQ Newbie
 
Registered: Jul 2011
Posts: 10

Original Poster
Rep: Reputation: Disabled
Everyone thank you so much!! i think i have a new lead thank you again!!!
 
  


Reply



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 On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 11:05 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
python update - Unable to load GTK2 Python bindings: No module named gtk itpedersen Linux - Software 2 10-03-2008 03:44 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration