Coding Challenge

Malte Brill revolution at derbrill.de
Fri Mar 11 08:26:31 EST 2011


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