[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LOGO-L> eliminating items randomly from a list



Wendy Petti wrote:
> I have a list with 11 words in it; each is the name of a procedure.  I am
> trying to pick one of these words at random and then eliminate it from the
> list.  (At a certain button click, all of the words can be restored to the
> list.)  But all I can manage to do is either run through the list in the
> same order as originally entered, eliminating the first (or last) each time
> (using butfirst or butlast), or select randomly without eliminating any
> words and sometimes have the same word get chosen repeatedly.  How can I
> select a word randomly and then place that word at the beginning or end of
> the list so it can be eliminated?  Or if this is not possible, is there
> some handy way of entering the list in a newly scrambled order each time a
> button is clicked?  Does it help to put the list into a text box?

This will remove a random element from a list
;;
;; Remove a random element from a List
;; Report the updated list
;;
to remRandomFrom :aList
	;; first pick the element to remove

	let [anItem pick :aList]

	;; now remove it

	op remItemFrom :anItem :aList
end

to remItemFrom :anItem :aList
	;; empty list? output nil
	if empty? :aList [op [] ]

	;; if anItem isn't in aList output  aList
	if not member? :anItem :aList [op :aList]

	;; if anItem is the first element in aList output the butfirst of aList
	if equal? :anItem first :aList [op bf :aList]

	;; other wise create a list (with se) with the first element in aList
	;; and call us again with the rest of the list.
	op (se first :aList remItemFrom :anItem bf :aList)
end

You would use it like:
make "foo [1 2 3 4 5 6]
show :foo
-> 1 2 3 4 5 6
make "foo remRandomFrom :foo
show :foo
-> 1 2 4 5 6

Or

make "foo [1 2 3 4 5 6 7 8 9]
repeat count :foo [make "foo remRandomFrom :foo show :foo]
1 3 4 5 6 7 8 9
1 3 4 5 7 8 9
3 4 5 7 8 9
3 4 5 7 8
4 5 7 8
4 7 8
4 8
4

Hope this helps.  
Be sure to check out my web site for more logo stuff.

regards
-- 
Frank Caggiano
caggiano@atlantic.net
http://www.atlantic.net/~caggiano
---------------------------------------------------------------
Please post messages to the Logo forum to logo-l@gsn.org.  Mail
questions about the list administration to logofdn@gsn.org.  To
unsubscribe send    unsubscribe logo-l    to majordomo@gsn.org.



Global SchoolNet Foundation - Linking Kids Around the World!
Copyright GSN - All Rights Reserved - Comments & Questions
Visit GSN's Global Schoolhouse for more exciting learning resources!
Search our Site - Home