Skip to content

Instantly share code, notes, and snippets.

@roskakori
Created November 18, 2014 22:37
Show Gist options
  • Save roskakori/11d0a37468ea6a0c7476 to your computer and use it in GitHub Desktop.
Save roskakori/11d0a37468ea6a0c7476 to your computer and use it in GitHub Desktop.
"""
This source code can act as base for a coding dojo.
It implements and test a function `iter_is_equal()` that take two sequences
as parameters and returns `True` if all there items are equal.
>>> iter_is_equal([1, 2, 3], [1, 2, 3])
True
>>> iter_is_equal([1, 2, 3], [1, 3])
False
The implementation provided works but is horribly inefficient. When passed
two generators, it produces all items of both generators.
The challenge is to compare one item after another (consequently using less
memory) and once two items differ, stop and return `False` (consequently
using less CPU).
For a proper function see <TODO>.
Written by Thomas Aglassinger, placed in the public domain.
"""
import unittest
def iter_is_equal(a, b):
# FIXME: implement in a more efficient way
return list(a) == list(b)
def some():
yield 1
yield 2
yield None
yield 3
def same():
yield 1
yield 2
yield None
yield 3
def less():
yield 1
yield 2
yield None
def more():
yield 1
yield 2
yield None
yield 3
yield 4
def nothing():
# Yield no data.
# HACK: Use yield in the code to change the function into a generator but
# never actually yield anything.
if False:
yield None
class IterIsEqualTest(unittest.TestCase):
def test_some_is_equal(self):
self.assertTrue(iter_is_equal(some(), some()))
def test_same_is_equal(self):
self.assertTrue(iter_is_equal(some(), same()))
def test_less_differs(self):
self.assertFalse(iter_is_equal(some(), less()))
def test_more_differs(self):
self.assertFalse(iter_is_equal(some(), more()))
def test_more_differs(self):
self.assertFalse(iter_is_equal(some(), more()))
def test_nothing_differs(self):
self.assertFalse(iter_is_equal(some(), nothing()))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment