Page 1 of 1 1
Topic Options
#112377 - 2004-01-23 11:44 PM Recursion Mind Bender
jtokach Offline
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
#112378 - 2004-01-24 12:13 AM Re: Recursion Mind Bender
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
I don't mind even checking before asking what is this supposed to do?

another than messing your mind I mean
_________________________
!

download KiXnet

Top
#112379 - 2004-01-24 01:42 AM Re: Recursion Mind Bender
Howard Bullock Offline
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

_________________________
Home page: http://www.kixhelp.com/hb/

Top
#112380 - 2004-01-24 07:46 PM Re: Recursion Mind Bender
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
And where in your original code is the global variable as I seen no GLOBAL in the code?
_________________________
There are two types of vessels, submarines and targets.

Top
#112381 - 2004-01-26 06:13 AM Re: Recursion Mind Bender
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
$tmp (by default it is GLOBAL)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#112382 - 2004-01-27 10:53 PM Re: Recursion Mind Bender
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Unless you are just try to learn recursion, I really don't think there's a need for it here

Code:

Function unbitwise($x)
dim $c, $ret
while $x > 0
$c = 1
while $c*2 <= $x
$c = $c*2
loop
$ret = + $ret + "," + $c
$x = $x - $c
loop
$unbitwise = substr($ret,2)
endfunction

_________________________
Eric

Top
#112383 - 2004-01-29 11:30 PM Re: Recursion Mind Bender
jtokach Offline
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
#112384 - 2004-01-30 12:16 AM Re: Recursion Mind Bender
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Recursively:

Code:

Function unbitwise($x)
dim $c
$c = 1
while $c*2 <= $x
$c = $c*2
loop
if $x-$c >0
$unbitwise = "" + $c + "," + unbitwise($x-$c)
else
$unbitwise = $c
endif
endfunction



For recursive functions, you want to have some sort of base condition, that when passed means you are done with the recursion. Usually it's in the beginning of the function, but for this particular one, it's at the end. If $x-$c = 0, I'm done with the recursion and just return $c.
_________________________
Eric

Top
#112385 - 2004-01-30 03:49 AM Re: Recursion Mind Bender
Howard Bullock Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#112386 - 2004-01-30 03:51 AM Re: Recursion Mind Bender
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
This looks much better than mine
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#112387 - 2004-01-30 09:22 AM Re: Recursion Mind Bender
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, when do we golf?


ps:
Code:




nah, didn't worky


Edited by Lonkero (2004-01-30 09:35 AM)
_________________________
!

download KiXnet

Top
#112388 - 2004-01-30 10:10 AM Re: Recursion Mind Bender
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, this does!
damned, wasted 1 hour playing with this when could have done that at work!

Code:

Function unbitwise($x)
dim $c,$n
$c=1
while $c<$x
if $x&$c
$n=""+$c+","+$n
endif
$c = $c*2
loop
$unbitwise = left($n,-1)
endfunction

_________________________
!

download KiXnet

Top
#112389 - 2004-01-30 01:58 PM Re: Recursion Mind Bender
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4401
Loc: New Jersey
Golf?? Most of my posts are more appropriate for "BasketKix" - highest score (char count) wins! My latest script is 194K...

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#112390 - 2004-01-30 03:58 PM Re: Recursion Mind Bender
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
glenn, I've noticed
_________________________
!

download KiXnet

Top
#112391 - 2004-02-02 06:19 PM Re: Recursion Mind Bender
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
Can one of guys package this in UDF format and submit to the UDF collection? I would, but its not even my code any more! =) Thanks again for the help on this.
_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.

Top
#112392 - 2004-02-02 08:16 PM Re: Recursion Mind Bender
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
doesn't matter.
you can post it as the initiative (is that correctly written?) was yours.
just add a special thanks or other developers section to remember those who participated
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 874 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.068 seconds in which 0.025 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org