Skip to content

Instantly share code, notes, and snippets.

@ijp
Created October 22, 2014 22:04
Show Gist options
  • Save ijp/c46f4fafea1e4fcc3865 to your computer and use it in GitHub Desktop.
Save ijp/c46f4fafea1e4fcc3865 to your computer and use it in GitHub Desktop.
def rindex_func(func, l):
for (i,obj) in reversed(list(enumerate(l))): ## Boo! list()
if func(obj):
return i
return None
def only_as_suffix(obj, l):
try:
i = l.index(obj)
except ValueError:
return True
j = rindex_func(lambda x: x != obj, l)
if j:
return j < i
else:
return True
print only_as_suffix(0, [0,0]) # True
print only_as_suffix(0, [1,2,3,4]) # True
print only_as_suffix(0, [1,2,3,4,0]) # True
print only_as_suffix(0, [1,2,3,4,0,0,0]) # True
print only_as_suffix(0, [1,2,0,3,4,0]) # False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment