Re: Code War: Quick Structure Check

new topic     » goto parent     » topic index » view thread      » older message » newer message

Very good code Carl.  The examples I gave you obviously weren't
extensive enough.  I wasn't sure of exactly what I wanted until I started
adding a few more tests.

    Your function appears to handle the following example incorrectly.

sequence s
s = {{{}}}--'s' is obviously 3 dimesions and...

? equal_dimension(1, s)--Your code thinks it is 1.

To fix it just change 'return 1' after the for loop to 'return (dimension >
0)'.

Your code is much tighter than the bloat I had created.  Thanks for your
contribution.

        Lucius L. Hilley III
        lhilley at cdc.net
+----------+--------------+--------------+
| Hollow   | ICQ: 9638898 | AIM: LLHIII  |
|  Horse   +--------------+--------------+
| Software | http://www.cdc.net/~lhilley |
+----------+-----------------------------+

----- Original Message -----
From: "Carl R. White" <cyrek at BIGFOOT.COM>
To: "Removing Subscription" <EUPHORIA at LISTSERV.MUOHIO.EDU>
Cc: "Carl R. White" <cyrek at BIGFOOT.COM>
Sent: Thursday, May 11, 2000 8:45 AM
Subject: Re: Code War: Quick Structure Check


Since most of Lucius' message is quoted in the code below, I'll not quote
here :)

The closest 'magic' sequence trick I can think of is:

function same_structure(object a, object b)
    return equal({a}*0, {b}*0)
end function

...but this only returns if they have *exactly* the same structure, right
down to the atoms. To get to the level of abstraction outside atoms, a
function has to be written (either that or I've gotten rusty at sequence
magic recently :) ).

Here's my attempt at a generic solution, but I haven't extensively tested
it. It works with the supplied sequences though:

function equal_dimension(integer dimension, object x)
    if atom(x) then
        return (dimension = 0)
    else
        if not length(x) then return (dimension = 1) end if
        for i = 1 to length(x) do
            if not equal_dimension(dimension - 1, x[i]) then return 0 end if
        end for
        return 1
    end if
end function

type three_dimensional(object x)
    return equal_dimension(3, x)
end type

three_dimensional s

s = repeat(repeat(repeat(0,2),3),4)

s = {                     --this passes
     {{} ,{0,0,0}},
     {{0,0},{0,0},{0,0},{0,0,0}},
     {{},{}}
    }

s = {                     --this fails
     {   0 ,{0,0},{0,0}}, --error
     0,                   --error
     {0, 0, 0, 0},        --error
     {{0,0},{0,0},{0,0}}
    }

HTH,
Carl

--
Carl R. White       - Software Tester @ CTD - Carl- at -comodo.net
Cyrek the Illogical -          Geek         - cyrek- at -bigfoot.com
         Remove hyphens before mailing s'il vous plait...

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu