Re: Code War: Quick Structure Check

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

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