As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start + 1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end + 1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work for large inputs, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start + 1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end + 1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work for large inputs, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start + 1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end + 1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work for large inputs, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursivetail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start+1win_start + 1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end + win_end+11, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work for large inputs, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start+1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end+1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start + 1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end + 1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work for large inputs, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start+1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end+1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start+1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end+1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
As others have pointed out, your solution is O(n^2), chunk
is not the right abstraction to use here. A O(n) solution for enumerables would involve linked lists and double-ended queues (with O(1) head/tail insertion/removal operations), but those data-structures are not implemented in the core, so it would require extra infrastructure (immutable-ruby).
However, if the input is always an array, you can use start/end indexes to reference the sliding window. It's O(n) time, O(1) space, functional, tail-recursive:
def cons_sum_equals?(xs, sum, win_start = 0, win_end = 0, win_sum = 0)
case
when win_sum == sum
true
when win_start == xs.size
false
when win_end == xs.size, win_sum > sum
cons_sum_equals?(xs, sum, win_start+1, win_end, win_sum - xs[win_start])
else
cons_sum_equals?(xs, sum, win_start, win_end+1, win_sum + xs[win_end])
end
end
cons_sum_equals?([23, 5, 4, 7, 2, 11, 1], 20) #=> true
Caveat: The previous code is kept simple for demonstration purposes. Sadly, tail-call optimization in CRuby is fairly limited and, to make it work, you should: 1) change the case
s for if
s. 2) join the last two cons_sum_equals?
calls into one.