Coding Challenge

Nonsanity form at nonsanity.com
Fri Mar 11 13:54:08 EST 2011


Try this. I haven't tested, but the logic looks sound enough...

private function resolveRelation pPers1,pPers2,pRelation
   put sRelations[pPers1][pPers2] into relationship
   -- if the two are equal but we're testing for > or < then return false
   if relationship is "=" and pRelation is not "=" then
      return false
   -- else if the relationship is a direct match, return true
   else relationship = pRelation then
      return true
   end if
   -- otherwise, interate to see if the relationship is true
   -- note that at this point, we know the two aren't directly equal
   -- this is important because the first interation HAS to be > or < and
not >= or <=
   -- further interations can be >= and <=, but the first cannot
   get traverseList(pPers1,pPers2,pRelation)
   -- if the relationship is valid, then put it into the DB to save time
later
   if it is true then
      set sRelations[pPers1][pPers2] to pRelation
   end if
   return it
end resolveRelation

private function traverseList pPers1,pPers2,pRelation
   -- are the two people of the correct relationship and/or equal?
   if sRelations[pPers2][pPers2] = pRelation or sRelations[pPers2][pPers2] =
pRelation then
      return true
   end if
   local tOther
   put the keys of sRelations[pPers1] into tOther
   repeat for each line personB in tOther
      -- only follow branches leading to the same relationship we are
testing (< or >)
      -- OR =, so long as the first iteration was < or >
      -- So if: A<B and B<C and C=D and D<E then "A<E" must be true
      if sRelations[pPers2][personB] = pRelation or
sRelations[pPers2][personB] = "=" then
          return traverseList(personB,pPers2,pRelation)
      end if
   end repeat
   return false
end traverseList


 ~ 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
>
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list