Random Letters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdmytryk
    New Member
    • Sep 2007
    • 12

    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:
    [CODE=python]
    import random

    word = "index"
    print "The word is: ", word, "\n"

    high = len(word)
    low = -len(word)
    x=len(word)


    for i in range(x):
    position = random.randrang e(low, high)
    print "word[", position, "]\t", word[position]

    raw_input("\n\n Press the enter key to exit.")
    [/CODE]
    thanks
    Last edited by bartonc; Sep 19 '07, 04:12 PM. Reason: Added [CODE=python][/CODE] tags.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    [CODE=python]
    >>> word = "hello"
    >>> x = len(word)
    >>> beenUsed = [] # and empty list of used indeces
    >>> while len(beenUsed) < x:
    ... position = random.randint( 0, x - 1)
    ... if position not in beenUsed:
    ... beenUsed.append (position)
    ... print "word[", position, "]\t", word[position]
    ...
    word[ 3 ] l
    word[ 2 ] l
    word[ 0 ] h
    word[ 4 ] o
    word[ 1 ] e
    >>> [/CODE]

    Comment

    • KaezarRex
      New Member
      • Sep 2007
      • 52

      #3
      Originally posted by mdmytryk
      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:
      [CODE=python]
      import random

      word = "index"
      print "The word is: ", word, "\n"

      high = len(word)
      low = -len(word)
      x=len(word)


      for i in range(x):
      position = random.randrang e(low, high)
      print "word[", position, "]\t", word[position]

      raw_input("\n\n Press the enter key to exit.")
      [/CODE]
      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:
      [CODE=python]position = random.randrang e(len(word))
      [/CODE]
      That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by KaezarRex
        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:
        [CODE=python]position = random.randrang e(len(word))
        [/CODE]
        That way it will automatically adjust to the length of your word, and the lower bound will be 0 by default.
        Except that random.randrang e() returns an iterator and this one needs an int.

        Comment

        • KaezarRex
          New Member
          • Sep 2007
          • 52

          #5
          Originally posted by bartonc
          Except that random.randrang e() returns an iterator and this one needs an int.
          Maybe it's an accident, but this works:
          [CODE=python]>>> import random
          >>> word = "index"
          >>> while len(word):
          ... position = random.randrang e(len(word))
          ... print word[position]
          ... word = word.replace(wo rd[position], "", 1)
          ...
          e
          d
          i
          n
          x
          >>>[/CODE]
          I use 2.5 if that matters

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by KaezarRex
            Maybe it's an accident, but this works:
            [CODE=python]>>> import random
            >>> word = "index"
            >>> while len(word):
            ... position = random.randrang e(len(word))
            ... print word[position]
            ... word = word.replace(wo rd[position], "", 1)
            ...
            e
            d
            i
            n
            x
            >>>[/CODE]
            I use 2.5 if that matters
            Well, I'll be... You taught me something, there. Thank you. (2.5 doesn't matter)

            Comment

            • mdmytryk
              New Member
              • Sep 2007
              • 12

              #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.

              Comment

              • KaezarRex
                New Member
                • Sep 2007
                • 52

                #8
                Originally posted by mdmytryk
                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.
                [CODE=python]def recurse(word):
                if word == "":# base case
                return
                else:
                position = random.randint(-len(word), len(word)-1)
                print word[position]
                recurse(word.re place(word[position], "", 1))# call the function again with the printed letter missing from the word
                [/CODE]
                Last edited by KaezarRex; Sep 19 '07, 06:20 PM. Reason: incorrect code

                Comment

                • bvdet
                  Recognized Expert Specialist
                  • Oct 2006
                  • 2851

                  #9
                  Why not use random.sample() ?[code=Python]import random

                  def randWord(word):
                  for letter in random.sample(w ord, len(word)):
                  print letter[/code]

                  Comment

                  • mdmytryk
                    New Member
                    • Sep 2007
                    • 12

                    #10
                    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.

                    Comment

                    • bartonc
                      Recognized Expert Expert
                      • Sep 2006
                      • 6478

                      #11
                      Originally posted by mdmytryk
                      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!):[code=Python]import random

                      word = "index"

                      for letter in random.sample(w ord, len(word)):
                      print letter[/code]

                      Comment

                      Working...