Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

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 cases for ifs. 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 cases for ifs. 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 cases for ifs. 2) join the last two cons_sum_equals? calls into one.

deleted 4 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26

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 cases for ifs. 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 cases for ifs. 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 cases for ifs. 2) join the last two cons_sum_equals? calls into one.

added 4 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26

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 cases for ifs. 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 cases for ifs. 2) join the last two cons_sum_equals? calls into one.

added 83 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
deleted 86 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
added 22 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
edited body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
added 8 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
deleted 18 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
edited body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
edited body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
deleted 2 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
added 131 characters in body
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
Source Link
tokland
  • 11.2k
  • 1
  • 21
  • 26
Loading
lang-rb

AltStyle によって変換されたページ (->オリジナル) /