#112377 - 2004-01-23 11:44 PM
Recursion Mind Bender
|
jtokach
Seasoned Scripter
   
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
I have this nice code, that can only be run once due to a global variable:
Code:
? unbitwise(15) ? unbitwise(35)
Function unbitwise($x) Dim $factor,$bit $factor=1 While ($x>=$Factor) $bit=$factor $Factor=$Factor*2 Loop
If $tmp $tmp=$tmp+","+$bit Else $tmp="$bit" EndIf
$x=$x-$bit If $x $nul=unbitwise($x) EndIf $unbitwise=$tmp EndFunction
So, it needs to be fixed... Here's my current thought pattern; some sort of recursion with an optional pointer that get passed up the river. Problem is, it gets lost coming back down the river. You'll probably have better luck starting from the original above as I went off in few directions below. My head hurts.
Code:
? unbitwise(15) ? unbitwise(35)
Function unbitwise($x,optional $tmp) Dim $factor,$bit,$tmp,$tmp2,$flag $factor=1 While ($x>=$Factor) $bit=$factor $Factor=$Factor*2 Loop
If $tmp ? "has "$tmp $tmp=$tmp+","+$bit ;$unbitwise=$tmp Else ? "has not" $tmp="$bit" ;$unbitwise=$tmp EndIf
$x=$x-$bit If $x $tmp2=unbitwise($x,$tmp) Else If Not $Flag $tmp2=$tmp $Flag=1 EndIf ? "Check1 "$tmp2 EndIf ? "Check2 "$tmp2 EndFunction
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
|
Top
|
|
|
|
#112379 - 2004-01-24 01:42 AM
Re: Recursion Mind Bender
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Code:
Function unbitwise($x, optional $factor) if vartypename($factor) = "Empty" $factor = 1 else $factor = 2 * $factor endif if $x >= $factor $unbitwise = "" + iif(($factor & $x), "," + $factor, "") + unbitwise($x, $factor) endif if $factor = 1 $unbitwise = substr($unbitwise,2) Dim $array $array = split($unbitwise,",") $unbitwise = "" for $x=ubound($array) to 0 step -1 $unbitwise = "" + $unbitwise + "," + $array[$x] next $unbitwise = substr($unbitwise,2) endif EndFunction
|
|
Top
|
|
|
|
#112381 - 2004-01-26 06:13 AM
Re: Recursion Mind Bender
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
$tmp (by default it is GLOBAL)
|
|
Top
|
|
|
|
#112383 - 2004-01-29 11:30 PM
Re: Recursion Mind Bender
|
jtokach
Seasoned Scripter
   
Registered: 2001-11-15
Posts: 513
Loc: PA, USA
|
Man, finally have some time in between virus attacks...
Jooel, the purpose of this is to list in decimal format the bits that make up a bitwise value. For instance, 27 is created by the bits 16,8,2,1. I suppose a & is the simplest way, but I just don't find it readable, and now I have all the bits at hand in one operation.
Howard, nice code for the recursive version.
Maciep, this seemed like a decent task to perform recursion on. Your solution is terrific. I really am trying to get a better hold on recursion cuz it's just so damn confusing, but in some cases, it can really save some rewriting; not to mention the elegance of a nicely formed recursive operation. Judging by Howard's code, this isn't as simple as I first thought it would be.
_________________________
-Jim
...the sort of general malaise that only the genius possess and the insane lament.
|
|
Top
|
|
|
|
#112385 - 2004-01-30 03:49 AM
Re: Recursion Mind Bender
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Don't judge elegance or ease from my code. I just threw it together in 10 minutes without much thought at all. It should be able to be made much better...if I were golfing...
Half of the code is reversing the order. This was necessary since I did not use a loop to find the largest factor of 2 in the number. I instead started at 1 and worked up.
|
|
Top
|
|
|
|
#112386 - 2004-01-30 03:51 AM
Re: Recursion Mind Bender
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
This looks much better than mine
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 429 anonymous users online.
|
|
|