I’d do it this way:
defmodule CodingTest do
@moduledoc """
* Implement a method that will determinate if exists a range of
* non-negative elements in an array that sum up to a specific non-
* negative number
*
* Example 1 - targetSum 3, numbers = [1,7,1,1,1,5,6,1] - output true (the sum of the elements in range 2 to 4 is 3
* Example 2 - targetSum 7, numbers = [0,4,5,1,8,9,12,3,1] - output false (no range sums to 7)
*
"""
def solution([h | t], target_sum) do
# start by taking first element as the current sublist
find_solution([h], t, h, target_sum)
end
# defp find_solution(current_sublist, rest_of_list, current_sum, target_sum)
# if target sum is zero, we require zero number somewhere
# it is not enough to simply say that zero element sublist has zero length
# NOTE: that edge case should be clarified because it complicates simple recursion
# in which empty range sums up to zero (even if there no zeros)
# we've found the sublist!
# NOTE: previous solution where `target_sum` was twice in parameters was perfectly fine and idiomatic
# However, in this particular example, I wanted to see three `when` clauses with `==`, `<` and `>`
defp find_solution(current_sublist, _rest_of_list, current_sum, target_sum) when current_sum == target_sum and length(current_sublist) > 0, do: :OK
# we need more, so we take add first element from the reset to the current sublist
# NOTE: this merges a case where the current sum is too low AND where current_sublist has zero elements and both current_sum and target_sum are 0
defp find_solution(current_sublist, [h | t] = _rest_of_list, current_sum, target_sum) when current_sum <= target_sum do
find_solution(current_sublist ++ [h], t, current_sum + h, target_sum)
end
# we are over target, so we drop first item from the current sublist
defp find_solution([h | t] = _current_sublist, rest_of_list, current_sum, target_sum) when current_sum > target_sum do
find_solution(t, rest_of_list, current_sum - h, target_sum)
end
defp find_solution(_, _, _, _), do: :KO
end
Thoughts:
- The algorithm would be simpler if zero element sublist would be allowed for zero sum; In fact, it is so much simpler, that I am 90% sure that it was the intended solution (unless it is some kind of class about algorithms where you should look for edge cases)
- you can use
[h | t] = _nameto convey variable meaning - you can use
andin guards, so havinglength(current_sublist) > 0drops one of the cases that started from scratch and merges it with the case where we have too little elements (they were doing the same thing conceptually - taking a new element from the rest of the list) - it is often good practice to put all the terminal cases first and then all the recursive ones; however, this problem is simpler if we:
- check if we have the solution
- check if we should extend the current_sublist
- check if we should shrink the current_sublist
- stop if we can’t do any of those
We could potentially still put stop conditions first, but the check would be we would be more compiicated and I’d rather reduce the number of clauses.


















