Skip to content

Instantly share code, notes, and snippets.

@SergKolo
Last active March 16, 2016 18:44
Show Gist options
  • Select an option

  • Save SergKolo/c8659ddba71ecfc30c1d to your computer and use it in GitHub Desktop.

Select an option

Save SergKolo/c8659ddba71ecfc30c1d to your computer and use it in GitHub Desktop.
A recursive solution to Checkio task
# A recursive solution to https://github.com/Bryukh-Checkio-Tasks/checkio-task-stair-steps/blob/master/info/task_description.html
def golf(nums,skipped):
if nums:
x = nums.pop(0) # grab current number
if x < 0 and not skipped: # is this one negative ?
print 'Skipping',x
return golf(nums[0:],True) # then skip
else:
print 'returning',x
return x+golf(nums[0:],False) # otherwise, don't skip
else: return 0 # prevents returning none
#numbers=[5,6,-10,-7,4]
#numbers=[-11, 69, 77, -51, 23, 67, 35, 27, -25, 95]
numbers=[-21, -23, -69, -67, 1, 41, 97, 49, 27]
print 'numbers are' ,numbers
print golf(numbers,False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment