Coding Challenge

Nonsanity form at nonsanity.com
Fri Mar 11 13:37:12 EST 2011


I don't think this works... The Iterating function doesn't really iterate.
It just does a bit of tire spinning without going anywhere. And gives a
wrong result.

Let say the array looked like this:

[B][A] = >
[B][C] = <
[B][D] = <
[C][E] = <
[D][E] = <

Which is enough information to determine this order using my algorithm
(which isn't complete itself):
    A B (C D) E
Where A is the youngest, E is the oldest, but we don't know the relationship
between C and D.

Now if we want to find out the relationship between B and E and call
resolveRelation:

1) Is there an entry for [B][E]? No, so call traverseList.
2) Repeat for each element of [B] (which is A C and D).
3) Is it "E"? No, none of them (A C D) are E or ever will be, since we
already tested for that pair.
4) Call traverseList with [B] and the element we are testing first, [A].
5) Repeat for each element of [B] (which is still A C and D).
6) Is it "A"? Yes it is. It had to be. We just did this.
7) Put the relationship between A and B into the output global.
8) Exit this traverseList  with true.
9) Exit the top traverseList  with true.

Query: What is the relationship between B and E?
Result: The relationship between B and A is >.
Exclamation: That wasn't what I asked for, stupid computer!
Grumble: ...always doing what I TELL it to do instead of what I WANT it to
do...
(Real Answer: < )


 ~ Chris Innanen
 ~ Nonsanity





On Fri, Mar 11, 2011 at 8:26 AM, Malte Brill <revolution at derbrill.de> wrote:

> Ok,
>
> I will not claim I understand how this works, but it appears to do. :-)
> Still needs more testing, but for now I think it works.
>
> Cheers,
>
> Malte
>
> I have been asking a colleague for help (Danke danke danke Steffen) and
> here is what he came up with:
>
> local sRelations, sOpList
>
> on mouseUp
>    local tValid
>    delete variable sRelations
>    delete variable sOpList
>    set the itemdel to TAB
>    -- fld Data is TAb delimited. Person1 TAB Operator TAB Person2
>    repeat for each line theLine in fld "data"
>        if item 2 of theLine = "=" then
>            put "=" into sRelations[item 1 of theLine][item 3 of theLine]
>            put "=" into sRelations[item 3 of theLine][item 1 of theLine]
>        end if
>        if item 2 of theLine = "<" then
>            put "<" into sRelations[item 1 of theLine][item 3 of theLine]
>            put ">" into sRelations[item 3 of theLine][item 1 of theLine]
>        end if
>        if item 2 of theLine = ">" then
>            put ">" into sRelations[item 1 of theLine][item 3 of theLine]
>            put "<" into sRelations[item 3 of theLine][item 1 of theLine]
>        end if
>    end repeat
>    answer checkForValidRelation(the text of fld "pers1",the text of fld
> "pers2",the label of btn "relation")
>    -- returns true or false for the validity of the relation you are about
> to set.
> end mouseUp
>
> private function checkForValidRelation pPers1,pPers2,pRelation
>   local tResolved
>   put resolveRelation(pPers1,pPers2,pRelation) into tResolved
>   if not tResolved then return true
>   return (checkOperator(pRelation) = pRelation)
> end checkForValidRelation
>
> private function resolveRelation pPers1,pPers2,pRelation
>    if sRelations[pPers1][pPers2] is not empty then
>        put sRelations[pPers1][pPers2] & cr after sOpList
>        return true
>    end if
>    -- iterate through list
>    return traverseList(pPers1,pPers2,pRelation)
> end resolveRelation
>
> private function traverseList pPers1,pPers2,pRelation
>    local tOther
>    put the keys of sRelations[pPers1] into tOther
>    repeat for each line personB in tOther
>        if personB = pPers2 then
>            put sRelations[pPers1][personB] & cr after sOpList
>            return true
>        else
>            if traverseList(pPers1,personB,pRelation) then
>                return true
>             end if
>        end if
>    end repeat
>     return false
> end traverseList
>
> private function checkOperator pRelation
>    local tSame, tLast
>    put true into tSame
>    put pRelation into tLast
>    repeat for each line theLine in sOpList
>        if theLine is not empty then
>            if theLine <> pRelation then
>                put false into tSame
>            end if
>            put theLine into tLast
>        end if
>    end repeat
>    if not tSame then
>        return tLast
>    end if
>    return pRelation
> end checkOperator
>
>



More information about the use-livecode mailing list