KenKen puzzle addends: REDUX A (corrected) non-recursive algorithm

后端 未结 9 1733
眼角桃花
眼角桃花 2021-01-03 05:24

This question relates to those parts of the KenKen Latin Square puzzles which ask you to find all possible combinations of ncells numbers with values x such that 1 <= x &

9条回答
  •  情书的邮戳
    2021-01-03 05:59

    First of all, I am learning Python myself so this solution won't be great but this is just an attempt at solving this. I have tried to solve it recursively and I think a recursive solution would be ideal for this kind of problem although THAT recursive solution might not be this one:

    def GetFactors(maxVal, noOfCells, targetSum):
        l = []
        while(maxVal != 0):
            remCells = noOfCells - 1
            if(remCells > 2):
                retList = GetFactors(maxVal, remCells, targetSum - maxVal)
                #Append the returned List to the original List
                #But first, add the maxVal to the start of every elem of returned list.
                for i in retList:
                    i.insert(0, maxVal)
                l.extend(retList)
    
            else:
                remTotal = targetSum - maxVal
                for i in range(1, remTotal/2 + 1):
                    itemToInsert = remTotal - i;
                    if (i > maxVal or itemToInsert > maxVal):
                        continue
                    l.append([maxVal, i, remTotal - i])
            maxVal -= 1
        return l
    
    
    
    if __name__ == "__main__":
        l = GetFactors(5, 5, 15)
        print l
    

提交回复
热议问题