My last contribution to this, just cos I am playing with closures and i was having fun.
The following is an "OO" solution, with an interesting testcase to prove it works.
#! perl -sw use strict; sub roundRobin { my @q; my $next = -1; my %self = ( values => sub { return @q[@_]; }, size => sub { return scalar @q; }, pos => sub { return 0+$next; }, add => sub { return push @q, @_; }, del => sub { my ($i, %del) = (0); @del{@_} = undef; @q = grep { exists $del{$_} ? ($next -= $i <= $next) && 0 +: ++$i } @q; return 1; }, next => sub { return undef if not @q; ++$next; $next %= @q; return $q[$next]; }, shuffle => sub { my $t; $t = $_ + rand @q - $_ and @q[$_, $t] = @q[$t, $_] for (0. +.$#q); }, ); return %self; } my %rr = roundRobin(); $rr{add}( 'a' .. 'z' ); print $rr{size}(), $rr{values}(0..$rr{size}()-1), $/; $rr{add}( 'k' .. 'q' ); print $rr{size}(), $rr{values}(0..$rr{size}()-1), $/; $rr{shuffle}(); while ( my $item = $rr{next}() ) { my $pos = $rr{pos}(); print " @{[$rr{values}( 0 .. $pos -1 )]}". "<@{[$rr{values}($pos)]}>". "@{[$rr{values}($pos+1..$rr{size}()-1 )]}"; if (rand(8) < 1) { my $new = chr(95+rand 26); $rr{add}( $new ); print "\t --added ", $new; } if (rand(8) < 1) { my $toDelete = $item; $toDelete = do { $rr{values}( rand( $rr{size}()) ) } until $toDelete ne $item; print "\t--deleting: '", $toDelete; $rr{del}( $toDelete ); } print $/; select undef, undef, undef, .5; }
Its worth running just to see the testcase in action, leastwise I think so:)
I've attach a short snippet of output here.
C:\test>^Croundrobin 26abcdefghijklmnopqrstuvwxyz 33abcdefghijklmnopqrstuvwxyzklmnopq <q>p z e u h j s a k w l o r k g v m x t q d i c m n p b l f y o n q<p>z e u h j s a k w l o r k g v m x t q d i c m n p b l f y o n + --added j q p<z>e u h j s a k w l o r k g v m x t q d i c m n p b l f y o n j q p z<e>u h j s a k w l o r k g v m x t q d i c m n p b l f y o n j q p z e<u>h j s a k w l o r k g v m x t q d i c m n p b l f y o n j + --deleting: 'v q p z e u<h>j s a k w l o r k g m x t q d i c m n p b l f y o n j + --added _ --deleting: 'e q p z u h<j>s a k w l o r k g m x t q d i c m n p b l f y o n j _ + --deleting: 'i q p z u h j<s>a k w l o r k g m x t q d c m n p b l f y o n j _ q p z u h j s<a>k w l o r k g m x t q d c m n p b l f y o n j _ q p z u h j s a<k>w l o r k g m x t q d c m n p b l f y o n j _ q p z u h j s a k<w>l o r k g m x t q d c m n p b l f y o n j _ q p z u h j s a k w<l>o r k g m x t q d c m n p b l f y o n j _ q p z u h j s a k w l<o>r k g m x t q d c m n p b l f y o n j _ + --added v q p z u h j s a k w l o<r>k g m x t q d c m n p b l f y o n j _ v q p z u h j s a k w l o r<k>g m x t q d c m n p b l f y o n j _ v q p z u h j s a k w l o r k<g>m x t q d c m n p b l f y o n j _ v + --added b q p z u h j s a k w l o r k g<m>x t q d c m n p b l f y o n j _ v b q p z u h j s a k w l o r k g m<x>t q d c m n p b l f y o n j _ v b q p z u h j s a k w l o r k g m x<t>q d c m n p b l f y o n j _ v b q p z u h j s a k w l o r k g m x t<q>d c m n p b l f y o n j _ v b + --deleting: 's q p z u h j a k w l o r k g m x t q<d>c m n p b l f y o n j _ v b + --deleting: 'z q p u h j a k w l o r k g m x t q d<c>m n p b l f y o n j _ v b q p u h j a k w l o r k g m x t q d c<m>n p b l f y o n j _ v b q p u h j a k w l o r k g m x t q d c m<n>p b l f y o n j _ v b q p u h j a k w l o r k g m x t q d c m n<p>b l f y o n j _ v b q p u h j a k w l o r k g m x t q d c m n p<b>l f y o n j _ v b q p u h j a k w l o r k g m x t q d c m n p b<l>f y o n j _ v b q p u h j a k w l o r k g m x t q d c m n p b l<f>y o n j _ v b + --added b q p u h j a k w l o r k g m x t q d c m n p b l f<y>o n j _ v b b q p u h j a k w l o r k g m x t q d c m n p b l f y<o>n j _ v b b + --added l q p u h j a k w l o r k g m x t q d c m n p b l f y o<n>j _ v b b l q p u h j a k w l o r k g m x t q d c m n p b l f y o n<j>_ v b b l + --added g q p u h j a k w l o r k g m x t q d c m n p b l f y o n j<_>v b b l g q p u h j a k w l o r k g m x t q d c m n p b l f y o n j _<v>b b l g q p u h j a k w l o r k g m x t q d c m n p b l f y o n j _ v<b>b l g q p u h j a k w l o r k g m x t q d c m n p b l f y o n j _ v b<b>l g q p u h j a k w l o r k g m x t q d c m n p b l f y o n j _ v b b<l>g + --deleting: '_ q p u h j a k w l o r k g m x t q d c m n p b l f y o n j v b b l<g> <q>p u h j a k w l o r k g m x t q d c m n p b l f y o n j v b b l g q<p>u h j a k w l o r k g m x t q d c m n p b l f y o n j v b b l g q p<u>h j a k w l o r k g m x t q d c m n p b l f y o n j v b b l g q p u<h>j a k w l o r k g m x t q d c m n p b l f y o n j v b b l g q p u h<j>a k w l o r k g m x t q d c m n p b l f y o n j v b b l g + --deleting: 'h q p u j<a>k w l o r k g m x t q d c m n p b l f y o n j v b b l g q p u j a<k>w l o r k g m x t q d c m n p b l f y o n j v b b l g q p u j a k<w>l o r k g m x t q d c m n p b l f y o n j v b b l g q p u j a k w<l>o r k g m x t q d c m n p b l f y o n j v b b l g q p u j a k w l<o>r k g m x t q d c m n p b l f y o n j v b b l g
In reply to Re: round-robin on varying sequence
by BrowserUk
in thread round-robin on varying sequence
by dpuu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |