aranthe (
aranthe) wrote in
intro_to_cs2010-05-05 02:34 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
Entry tags:
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)