Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #25069
| Newsgroups | comp.lang.forth |
|---|---|
| Date | 2013-08-08 03:24 -0700 |
| References | <kpfq17$p7f$1@dont-email.me> |
| Message-ID | <c1ccc5d3-93e3-4d9f-8ccd-4f5fc42136a4@googlegroups.com> (permalink) |
| Subject | Re: combinations |
| From | november.nihal@gmail.com |
( --------------------------------------------------------------------------- )
s" abcde" 2constant test
( --------------------------------------------------------------------------- )
: cellbits ( n -- n1 )
0 swap begin dup 0<> while dup 1 and if >r 1+ r> then 2/ repeat drop ;
( a - number of members in the set )
( b - how many to pick at a time )
: chooseinit ( a b -- m n m )
dup 0> >r over over > >r over 32 < r> r> and and if >r 1 swap lshift r> over else abort then ;
: choose ( m n a -- m n a' f )
dup 0= if FALSE else 1- 2dup cellbits = if TRUE else FALSE then then ;
( --------------------------------------------------------------------------- )
: str>member ( string n -- )
." (" begin dup 0> while
dup 1 and if >r 2dup + 1- c@ emit ." " r> then
2/ swap 1- swap
repeat drop 2drop ." )" ;
( --------------------------------------------------------------------------- )
: start ( -- )
cr
TEST NIP ( len ) 3 CHOOSEINIT
BEGIN DUP 0> WHILE
CHOOSE IF dup test rot str>member cr THEN
REPEAT DROP DROP DROP
; start
( --------------------------------------------------------------------------- )
\ To use call chooseinit first then choose.
\ for example to say '5 choose 3' load test$ with 5 unique members.
\ call 5 3 chooseinit which sets up the parameters for choose on the stack
\ call choose which returns a flag, you know you are done when count goes to
\ zero. if choose returns true then number is part of your set &
\ false means ignore number.
( --------------------------------------------------------------------------- )
Back to comp.lang.forth | Previous | Next — Next in thread | Find similar
Re: combinations november.nihal@gmail.com - 2013-08-08 03:24 -0700 Re: combinations Coos Haak <chforth@hccnet.nl> - 2013-08-09 02:44 +0200
csiph-web