aranthe ([personal profile] aranthe) wrote in [community profile] intro_to_cs2010-05-05 02:34 pm

PS3: Problem 3 » Solutions

PS3: Problem 3 » Solutions

# Problem 3: constrainedMatchPair, constrainedMatchPairR (recursive)
def constrainedMatchPair( firstMatch, secondMatch, length ):
    allN = ()
    for n in firstMatch:
        tryK = n + length + 1
        if tryK in secondMatch:
            allN += n,
    return allN

def constrainedMatchPairR( firstMatch, secondMatch, length ):
    if not firstMatch:
        return ()
    else:
        tryK = firstMatch[-1] + length + 1
        if tryK in secondMatch:
            return (firstMatch[-1],) + constrainedMatchPairR(firstMatch[:-1], secondMatch, length)
        else:
            return constrainedMatchPairR(firstMatch[:-1], secondMatch, length)