473,394 Members | 1,810 Online

# Random Letters

Simple question here. Im trying to figureout how to write a program in such a way that when a word is entered, the output is each individual letter of that word in a random order. so if pizza is entered, the output would be
P
Z
A
I
Z

I have a general idea of how to output the letters, but not without them repeating. Id like to figure this out myself, but would appreciate being pointed in the right direction. heres the code thus far:
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. word = "index"
4. print "The word is: ", word, "\n"
5.
6. high = len(word)
7. low = -len(word)
8. x=len(word)
9.
10.
11. for i in range(x):
12.     position = random.randrange(low, high)
13.     print "word[", position, "]\t", word[position]
14.
15. raw_input("\n\nPress the enter key to exit.")
16.
thanks
Sep 19 '07 #1
10 5098
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
1. >>> word = "hello"
2. >>> x = len(word)
3. >>> beenUsed = [] # and empty list of used indeces
4. >>> while len(beenUsed) < x:
5. ...     position = random.randint(0, x - 1)
6. ...     if position not in beenUsed:
7. ...         beenUsed.append(position)
8. ...         print "word[", position, "]\t", word[position]
9. ...
10. word[ 3 ]    l
11. word[ 2 ]    l
12. word[ 0 ]    h
13. word[ 4 ]    o
14. word[ 1 ]    e
15. >>>
Sep 19 '07 #2
Simple question here. Im trying to figureout how to write a program in such a way that when a word is entered, the output is each individual letter of that word in a random order. so if pizza is entered, the output would be
P
Z
A
I
Z

I have a general idea of how to output the letters, but not without them repeating. Id like to figure this out myself, but would appreciate being pointed in the right direction. heres the code thus far:
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. word = "index"
4. print "The word is: ", word, "\n"
5.
6. high = len(word)
7. low = -len(word)
8. x=len(word)
9.
10.
11. for i in range(x):
12.     position = random.randrange(low, high)
13.     print "word[", position, "]\t", word[position]
14.
15. raw_input("\n\nPress the enter key to exit.")
16.
thanks
Try using 0 as your low bound. When you have a negative low bound and your program randomly picks a negative index, it will look backwards through the word which you don't need to do.
Also, every time you find a random letter, remove it from the word so you don't pick it again. Remember to decrease you high bound by one every time you remove a letter since the word will be shorter
You could even go without bounds at all and use a statement something like:
Expand|Select|Wrap|Line Numbers
1. position = random.randrange(len(word))
2.
That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.
Sep 19 '07 #3
bartonc
6,596 Expert 4TB
Try using 0 as your low bound. When you have a negative low bound and your program randomly picks a negative index, it will look backwards through the word which you don't need to do.
Also, every time you find a random letter, remove it from the word so you don't pick it again. Remember to decrease you high bound by one every time you remove a letter since the word will be shorter
You could even go without bounds at all and use a statement something like:
Expand|Select|Wrap|Line Numbers
1. position = random.randrange(len(word))
2.
That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.
Except that random.randrange() returns an iterator and this one needs an int.
Sep 19 '07 #4
Except that random.randrange() returns an iterator and this one needs an int.
Maybe it's an accident, but this works:
Expand|Select|Wrap|Line Numbers
1. >>> import random
2. >>> word = "index"
3. >>> while len(word):
4. ...        position = random.randrange(len(word))
5. ...        print word[position]
6. ...        word = word.replace(word[position], "", 1)
7. ...
8. e
9. d
10. i
11. n
12. x
13. >>>
I use 2.5 if that matters
Sep 19 '07 #5
bartonc
6,596 Expert 4TB
Maybe it's an accident, but this works:
Expand|Select|Wrap|Line Numbers
1. >>> import random
2. >>> word = "index"
3. >>> while len(word):
4. ...        position = random.randrange(len(word))
5. ...        print word[position]
6. ...        word = word.replace(word[position], "", 1)
7. ...
8. e
9. d
10. i
11. n
12. x
13. >>>
I use 2.5 if that matters
Well, I'll be... You taught me something, there. Thank you. (2.5 doesn't matter)
Sep 19 '07 #6
Heres where it gets complicated. This is a "brain teaser" for a class i am taking. The trick here is that i cannot use a while loop and i must use the negative range as well as positive.
Sep 19 '07 #7
Heres where it gets complicated. This is a "brain teaser" for a class i am taking. The trick here is that i cannot use a while loop and i must use the negative range as well as positive.
It sounds like recursion is a good way to go here. It's very handy when teachers say you can't use a specific kind of loop.
Expand|Select|Wrap|Line Numbers
1. def recurse(word):
2.     if word == "":# base case
3.         return
4.     else:
5.         position = random.randint(-len(word), len(word)-1)
6.         print word[position]
7.         recurse(word.replace(word[position], "", 1))# call the function again with the printed letter missing from the word
8.
Sep 19 '07 #8
bvdet
2,851 Expert Mod 2GB
Why not use random.sample()?
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. def randWord(word):
4.     for letter in random.sample(word, len(word)):
5.         print letter
Sep 19 '07 #9
This is starting to drive me crazy. I dont see how this can be accomplished without using a while loop. We havent really covered functions in this class as of yet, so there must be another way...without getting fancy.

Thanks for all the suggestions thus far.
Sep 19 '07 #10
bartonc
6,596 Expert 4TB
This is starting to drive me crazy. I dont see how this can be accomplished without using a while loop. We havent really covered functions in this class as of yet, so there must be another way...without getting fancy.

Thanks for all the suggestions thus far.
OK. Here's a trick (thanks bvdet for your excellent use of the random module!):
Expand|Select|Wrap|Line Numbers
1. import random
2.
3. word = "index"
4.
5. for letter in random.sample(word, len(word)):
6.     print letter
Sep 19 '07 #11