LOGO-L> More word-shuffling 
Author Message
 LOGO-L> More word-shuffling

I'd like to get some opinions about the following versions of yet another
word-scrambling procedure. All of this has come about from exploring the
recently-posted "shuffling words" procedures and comments.
(The "remove_item" and "remove_pick" procedures are shown following the
"jumble" procedures.)

----------------------------------------
; Version A
; this version of jumble sets things up for the 'helper' function
; that does the real work

to jumble :w
if (count :w) < 2 [output :w]
output jumble1 :w (1 + random count :w)
end

to jumble1 :w :n
if (count :w) < 2 [output :w]
output word (item :n :w)
            (jumble1 (remove_item :n :w) (1 + random ((count :w) - 1)))
end

----------------------------------------
; Version B
; this version of jumble does not need a 'helper' function but
; does need a local variable for the randomly chosen position

to jumble :w
if (count :w) < 2 [output :w]
local "n
make "n (1 + random count :w)
output word (item :n :w) (jumble (remove_item :n :w))
end

----------------------------------------
; Version C
; this version of jumble does not use recursive calls;
; it selects a random position from the full word each time
; instead of from a recursively smaller word

to jumble :w
local "n
repeat (count :w)
  [make "n (1 + random count :w)
   make "w word (item :n :w) (remove_item :n :w)]
output :w
end
----------------------------------------

; Version A-pick
; similar to Version A but I used a local variable;
; could have put (remove_pick :p :w) in place of :nextw

to jumble :w
output jumble1 :w pick :w
end

to jumble1 :w :p
if (count :w) < 2 [output :w]
local "nextw
make "nextw (remove_pick :p :w)
output word :p (jumble1 :nextw pick :nextw)
end

----------------------------------------
; Version B-pick

to jumble :w
if (count :w) < 2 [output :w]
local "p
make "p pick :w
output word :p (jumble (remove_pick :p :w))
end

----------------------------------------

; I copied the "butn" procedure that was recently posted to the
; listserv but named it "remove_item" since it is being used
; with "item" and switched the inputs to match "item" inputs.

to remove_item :n :w
if :n < 2 [output butfirst :w]
output word (first :w) (remove_item (:n - 1) (butfirst :w))
end

; remove_pick is very similar to remove_item but it is
; removing a specific character instead of the character
; at a specified position

to remove_pick :p :w
if empty? :w [output " ]
if :p = first :w [output butfirst :w]
output word (first :w) (remove_pick :p (butfirst :w))
end

----------------------------------------

Just some other comments:

1.  Microworlds returns an error for "random 0" (minimum value for random
    is 1). This makes sense since the definition of random is to report a
    random non-negative integer less than the input; so, if the input is 0
    there is nothing possible to return that fits the definition.  The
    following procedure could be defined in Microworlds to allow for the 0
    input:

        to random0 :n
           if :n = 0 [output 0]
           output random :n
        end

2.  Since the statement "1 + random :something" seems to get used a lot, the
    following procedure could be defined:

        to random1 :n
          output 1 + random0 :n
        end

3.  In the JUMBLE procedures, the need to get a random character position might
    better be a procedure such as:

        to random_pos :w
          output random1 count :w
        end

All of these could be defined in the special startup file called -tools-.txt
for Microworlds. If you are not familiar with this file, their web site has
an example file but they are not willing to share very much information about
its use (unfortunately).

---------------------------------------------------------------





Mon, 06 Dec 1999 03:00:00 GMT  
 LOGO-L> More word-shuffling

|; Version A
|; this version of jumble sets things up for the 'helper' function
|; that does the real work
|
|to jumble :w
|if (count :w) < 2 [output :w]
|output jumble1 :w (1 + random count :w)
|end
|
|to jumble1 :w :n
|if (count :w) < 2 [output :w]
|output word (item :n :w)
|            (jumble1 (remove_item :n :w) (1 + random ((count :w) - 1)))
|end

I like this best.  It's mathematically correct (does a fair shuffle) and
avoids assignment.  If having a helper procedure bothers you, in
Berkeley Logo you can avoid that by using an optional input:

to jumble :w [:n 1 + random count :w]
if (count :w) < 2 [output :w]
output word (item :n :w) ~
            (jumble (remove_item :n :w) (1 + random ((count :w) - 1)))
end



Mon, 06 Dec 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. LOGO-L> Shuffling words and privacy

2. LOGO-L> Shuffling words

3. LOGO-L> Logo 15-Word Challenge Update

4. LOGO-L> Logo 15-Word Challange Update

5. LOGO-L> Re: Logo 15-Word Challenge

6. LOGO-L> Logo 15-Word Challenge

7. LOGO-L> Logo 15-word challenge update

8. LOGO-L> Logo 15-Word Challenge Update

9. LOGO-L> Logo 15-Word Challenge Update

10. LOGO-L> Shuffling words & Word Games

11. LOGO-L> Shuffling words & Word Games

12. LOGO-L> Shuffling words & word games

 

 
Powered by phpBB® Forum Software