in reply to Critique yet another List-to-Range function
I like your code but would suggest that the output is perhaps incorrect. Surely the two ranges 0-2,2.5-4.5 are a single logical range 0-4.5??? The range concept becomes a bit iffy once you stop using integers. Any two numbers will constitute a range - it just depends on your step size. Using my definition that a range means that any two adjacent numbers within that range are no more than unity apart....the range should be 0-4.5 not the 0-2,2.5-4.5...
Anyway, did somebody say GOLF? A bloated 93 chars, surely well over par.
cheers
tachyon
@test =(1,2,2.5,3.5,4.5,7,6,-5,-6,-7,7,8,9,10,0,12,18); sub range { @_=sort{$a<=>$b}@_;@r=($l)=$_[0]; map{if(abs$l-$_>1){push@r,$r=$_; $r[-2].="~$l";}$l=$_;}@_;@r; } @result = range(@test); print "$_\n" for @result; Prints: -7~-5 0~4.5 6~10 12~12 18
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Critique yet another List-to-Range function
by scott (Chaplain) on Jun 15, 2001 at 20:19 UTC | |
|
Re: Re: Critique yet another List-to-Range function
by shotgunefx (Parson) on Jun 19, 2002 at 02:11 UTC | |
|
Re: Re: Critique yet another List-to-Range function
by larryk (Friar) on Jun 15, 2001 at 17:11 UTC |