Commit 77ae42a0 authored by Taddeus Kroes's avatar Taddeus Kroes

Added pair iterator.

parent ecf5d466
...@@ -194,3 +194,14 @@ def evals_to_numeric(node): ...@@ -194,3 +194,14 @@ def evals_to_numeric(node):
return node.op in (OP_ADD, OP_MUL, OP_DIV, OP_POW, OP_SQRT) \ return node.op in (OP_ADD, OP_MUL, OP_DIV, OP_POW, OP_SQRT) \
and all(map(evals_to_numeric, node)) and all(map(evals_to_numeric, node))
def iter_pairs(list_iterable):
"""
Iterate over a list iterable in left-right pairs.
"""
if len(list_iterable) < 2:
raise StopIteration
for i, left in enumerate(list_iterable[:-1]):
yield left, list_iterable[i + 1]
from src.rules import utils from src.rules import utils
from src.rules.utils import least_common_multiple, is_fraction, partition, \ from src.rules.utils import least_common_multiple, is_fraction, partition, \
find_variables, first_sorted_variable, find_variable, substitute, \ find_variables, first_sorted_variable, find_variable, substitute, \
divides, dividers, is_prime, prime_dividers, evals_to_numeric divides, dividers, is_prime, prime_dividers, evals_to_numeric, \
iter_pairs
from tests.rulestestcase import tree, RulesTestCase from tests.rulestestcase import tree, RulesTestCase
...@@ -103,3 +104,9 @@ class TestRulesUtils(RulesTestCase): ...@@ -103,3 +104,9 @@ class TestRulesUtils(RulesTestCase):
self.assertFalse(evals_to_numeric(tree('int 1'))) self.assertFalse(evals_to_numeric(tree('int 1')))
self.assertFalse(evals_to_numeric(tree('int a'))) self.assertFalse(evals_to_numeric(tree('int a')))
self.assertTrue(evals_to_numeric(tree('sqrt 1'))) self.assertTrue(evals_to_numeric(tree('sqrt 1')))
def test_iter_pairs(self):
self.assertEqual(list(iter_pairs([1])), [])
self.assertEqual(list(iter_pairs([1, 2])), [(1, 2)])
self.assertEqual(list(iter_pairs([1, 2, 3])), [(1, 2), (2, 3)])
self.assertEqual(list(iter_pairs([1, 2, 3, 4])), [(1, 2), (2, 3), (3, 4)])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment