0

I have a recursive Backtracking problem for school, and I do not understand how I would go about solving it.

Given an array of integers, determine if it is possible to choose a group of those integers that add to a particular sum. Use a recursive method called sum_to_total to solve the problem (no loops!).

Examples:

  • "Array [3, 6, 7]" and "Sum 10" returns true, since 3 + 7 = 10
  • "Array [1, 2, 3]" and "Sum 6" returns true, since 1 + 2 + 3 = 6
  • "Array [2, 4, 6]" and "sum 5" returns false, since no combination of these numbers sum to 5

This is what I have got so far:

def self.sum_to_total(sum, array, index)

  @@sum_num += array[index]
  return false if @@sum_num > sum || @@sum_num < sum
  return false if index<board.length || index<0
  return true if @@sum_num == sum

  return solvable(sum, array, index+1) 
end


@@sum_num = 0
puts sum_to_total(10, [3, 5, 7], 0)

A few pointers would help.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
JosephKidd
  • 23
  • 3
  • 1
    When the system prevents you from submitting your question because the title is poor, please do not add "(Ruby)" to the end of it to get past the filter. That's what tags are for. There's a section in https://stackoverflow.com/help/how-to-ask about writing titles; I suggest reading it, then coming back and making your question's title more descriptive. – anothermh Mar 19 '20 at 22:16
  • Welcome to SO! Please read "[How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822/128421)" and "[How do I format my posts...](https://stackoverflow.com/help/formatting)" along with "[How do I format my code blocks?](https://meta.stackexchange.com/questions/22186/)". You're asking us to put in the effort to help you; We ask that you put in the effort to ask a well-asked question. Grammar and proper formatting are significant on SO. – the Tin Man Mar 19 '20 at 22:43
  • Please read "[MCVE](https://stackoverflow.com/help/minimal-reproducible-example)". Your code doesn't meet the guidelines because `solvable` isn't defined. – the Tin Man Mar 19 '20 at 22:56

1 Answers1

2

You've taken a shot at this, so here are some pointers that should help you move forward.

  1. By "recursive" we mean a method that calls itself. Your solvable needs to be a call to sum_to_total.
  2. A recursive method needs to pass in any values that change during the method, if you have to access the updated values on the next call. So, index as you have it is correct, but you also have to pass in sum_num.
  3. You can use defaults to initialize your index and sum_num on the first call. You don't need to mess with global variables. (And shouldn't. Global variables are evil. Most of the time.)
  4. You only want to return when you have gone through the entire array. You do not use return on your recursive method call, you just call the method.
  5. You don't need to make this a class method by using self.method_name.

I'll show you the basic recursive method, and leave you to work out the (more difficult) backtracking requirement. (This basic method will solve for the case of whether the entire array adds up to the sum. The backtracking part is necessary to determine whether or not a subset of the array does.)

def sum_to_total(sum, array, index = 0, sum_num = 0)
  sum_num += array[index]
  return sum_num == sum if index == array.size - 1
  sum_to_total(sum, array, index + 1, sum_num)
end

The three lines of code do this:

  1. Add the current array value to the sum. (You pretty much had this one ok.)
  2. If you're done going through the array, return whether or not the sum of the array equals the provided sum value.
  3. (If you aren't done going through the array) call the method again, passing in the incremented index value and the value of the accumulated sum so far. (You were on the right track!)

Here's an article that should help you with the backtracking part.

BobRodes
  • 5,990
  • 2
  • 24
  • 26