Ejemplo n.º 1
0
def part2(text):
    def starred(func):
        def wrapper(record):
            return func(**dict(field.split(":") for field in record.split()))

        return wrapper

    def height(hgt):
        if hgt.endswith("cm"):
            return 150 <= int(hgt.removesuffix("cm")) <= 193
        elif hgt.endswith("in"):
            return 59 <= int(hgt.removesuffix("in")) <= 76

    @starred
    def valid(byr=0, iyr=0, eyr=0, hgt="", hcl="", ecl="", pid="", cid=None):
        if not 1920 <= int(byr) <= 2002:
            return False
        elif not 2010 <= int(iyr) <= 2020:
            return False
        elif not 2020 <= int(eyr) <= 2030:
            return False
        elif not height(hgt):
            return False
        elif not re.match(r"#[0-9a-f]{6}", hcl):
            return False
        elif ecl not in ECL:
            return False
        elif not len(pid) == 9 or not pid.isdecimal():
            return False

        return True

    return count_items(text.split("\n\n"), valid)
Ejemplo n.º 2
0
    def valid(string):
        if 8 in string or 11 in string or 14 in string:
            return False

        if not any(starmap(straight, successive(string, 3))):
            return False

        pairs = unique_everseen(starfilter(eq, successive(string)))

        return 1 < count_items(pairs)
Ejemplo n.º 3
0
def test_count_failure5():
    # eq True but no pred
    with pytest.raises(TypeError):
        count_items([T(0)], eq=True)
Ejemplo n.º 4
0
def test_count_failure4():
    # Too few arguments
    with pytest.raises(TypeError):
        count_items()
Ejemplo n.º 5
0
def test_count_failure3():
    # Regression test when accessing the next item of the iterable resulted
    # in an Exception. For example when the iterable was a filter and the
    # filter function threw an exception.
    with pytest.raises(_hf.FailNext.EXC_TYP, match=_hf.FailNext.EXC_MSG):
        count_items(_hf.FailNext())
Ejemplo n.º 6
0
def count(a_gen, b_gen, sample):
    def valid(a, b):
        return a & BITS == b & BITS

    return count_items(islice(zip(a_gen, b_gen), sample), packed(valid))
Ejemplo n.º 7
0
def part1(text):
    def valid(record):
        return FIELDS.issubset(field.split(":")[0] for field in record.split())

    return count_items(text.split("\n\n"), valid)
Ejemplo n.º 8
0
def test_count_failure8():
    # Changing next method
    with pytest.raises(_hf.CacheNext.EXC_TYP, match=_hf.CacheNext.EXC_MSG):
        count_items(_hf.CacheNext(1))
Ejemplo n.º 9
0
def test_count_normal5():
    assert count_items([T(1), T(2), T(3)], lambda x: x > T(2)) == 1
Ejemplo n.º 10
0
def test_count_normal4():
    assert count_items([], iteration_utilities.return_identity) == 0
Ejemplo n.º 11
0
def test_count_normal3():
    # None as pred is equal to not giving any predicate
    assert count_items([T(0), T(0), T(1), T(1)], None) == 4
Ejemplo n.º 12
0
def test_count_normal2():
    assert count_items([T(0), T(0), T(1)], bool) == 1
Ejemplo n.º 13
0
def test_count_normal1():
    assert count_items([T(0), T(0)]) == 2
Ejemplo n.º 14
0
def test_count_empty1():
    assert count_items([]) == 0
Ejemplo n.º 15
0
def test_count_failure6():
    # eq True but pred None (like not given)
    with pytest.raises(TypeError):
        count_items([T(0)], pred=None, eq=True)
Ejemplo n.º 16
0
def test_count_failure7():
    # function returns item without boolean interpretation
    with pytest.raises(_hf.FailBool.EXC_TYP, match=_hf.FailBool.EXC_MSG):
        count_items([T(0)], lambda x: _hf.FailBool())
Ejemplo n.º 17
0
def test_count_normal6():
    assert count_items([T(1), T(2), T(3)], lambda x: x < T(3)) == 2
Ejemplo n.º 18
0
def part2(text):
    graph, node = process(text)
    unique = partial(ne, node)
    return count_items(takewhile(unique, applyfunc(graph.get, node))) + 1
Ejemplo n.º 19
0
def test_count_normal7():
    assert count_items([T(3), T(1), T(2), T(3), T(3)], T(3), True) == 3
Ejemplo n.º 20
0
def test_count_failure1():
    with pytest.raises(_hf.FailIter.EXC_TYP, match=_hf.FailIter.EXC_MSG):
        count_items(_hf.FailIter())
Ejemplo n.º 21
0
def test_count_failure2():
    with pytest.raises(TypeError):
        count_items([T(1)], T(1))
Ejemplo n.º 22
0
 def valid_combo(k):
     return count_items(combinations(nums, k), target_sum)
Ejemplo n.º 23
0
def part1(text, group="0"):
    parent, find = unite(text)

    return count_items(map(find, parent.values()), find(group), True)