Skip to content

Instantly share code, notes, and snippets.

@buranmert
Created July 12, 2015 16:08
Show Gist options
  • Save buranmert/293d2d5cb452ee4c5ba0 to your computer and use it in GitHub Desktop.
Save buranmert/293d2d5cb452ee4c5ba0 to your computer and use it in GitHub Desktop.
Subarray with given sum
#!/usr/bin/env/python
test_data = [3, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100, 200, 100]
test_goal = 5
def find_subsequence(data, goal):
temp = list(data)
depth = 1
while len(temp) > 0:
depth = depth + 1
for index in range(len(temp)-1):
temp[index] = temp[index] + data[index+depth-1]
if temp[index] == goal:
return (index, depth)
del temp[-1]
return (0, 0)
def solution(data, goal):
temp = []
for i in data:
if i == goal:
return [i]
temp.append(i)
index, range = find_subsequence(temp, goal)
return test_data[index:index+range]
if __name__ == "__main__":
print solution(test_data, test_goal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment