Creating a partition of a set in Scheme

后端 未结 2 1638
北海茫月
北海茫月 2021-01-28 04:12

I\'m pretty new to scheme overall and I\'m having some issues with figuring out an assignment for school. So please no full answers, just looking for a little insight or a nudg

2条回答
  •  心在旅途
    2021-01-28 04:47

    For a first pass, you may wish to avoid trying to couple "subset" logic to "do two subsets add to the same value" logic. Small procedures are easier to write than long ones.

    But this can be kind of a complicated problem.

    One way to approach it is called "wishful thinking." We'll work from the end: what is the last step in solving this problem? Comparing whether two subsets of the same length sum to the same value. So, let's solve that. Then we just do that to all the subsets of the same length. But now we need to know: what are all the groups of subsets of the same length? If we solved that, then everything to the end is finished. So let's solve that. Then we'll just apply that to the set of all subsets. But now we need to know: how do we get the set of all subsets? If we solved that, then everything to the end is finished. We'll just apply it to our set. But now we need to know: what is our set? —oh! that's the problem itself. So we're done!

    Actually it's a little trickier in the fine details, but with the above exposition I think you can see the logic of this chain:

    (find-first-equal-sum ; we can stop as soon as #t, if there is #t
      (make-pairs ; problem only wants us to consider pairs
        (sum-subsets ;we need sums eventually
           (at-least-two-in-a-group ; can't make pairs without at least two in a group
            (group-by-length ; only care about matching subsets of the same length
             (at-least-length-2 ; singles can't match because each element of a set is unique
              (subsets problem-set)))))))
    

    Note: I did actually write this program and test it before posting. The above "outline" is copied directly from DrRacket.

提交回复
热议问题