Created
March 6, 2016 18:38
-
-
Save petrushev/cb1cec0a8b3e0f991e50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def same(i): | |
a = next(i) | |
return _same(a, i) | |
def _same(a, i): | |
try: | |
b = next(i) | |
except StopIteration: | |
return True | |
if a != b: | |
return False | |
return _same(a, i) | |
assert same(iter([1,2,3])) is False | |
assert same(iter([1])) | |
assert same(iter([1,1,1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In Python, it's important to remember you also have unsized interables so your solution is nice but recursion will fail if you got more than 1000 items on a standard CPython config. Plus that equality is not the same as idententy, and both should be something you want to check against. So: