<filename>tests/unit/seed_tree.py
"""This gathers information about the random number generator function so that
it can be seeded to give any particular series of virtual coin tosses up to a
certain length.
"""
import random

FAIR_COIN = lambda: random.randint(0, 1)
RANDOM_SEED = random.seed

def has_all_sequences(seeds_for_sequences, length):
    """Return True if we have all permutations up to the length specified."""
    return len(seeds_for_sequences) == 2 ** (length + 1) - 2

def sequence_for_seed(rand_func, seed_func, seed_val, length):
    """Return a list of booleans generated by rand_func after seeding."""
    seed_func(seed_val)
    return [rand_func() for _ in range(length)]

def find_seeds_for_sequences(max_length, seed_func, rand_func):
    """Return a dict mapping sequences to the random seed required to
    generate them.

    >>> find_seeds_for_sequences(FAIR_COIN, RANDOM_SEED, 2)
    {(0,): 1, (0, 0): 9, (0, 1): 8, (1,): 0, (1, 0): 10, (1, 1): 6}
    """
    seeds_for_sequences = {}
    for length in range(1, max_length + 1):
        seed_val = 0
        while not has_all_sequences(seeds_for_sequences, length):
            sequence = sequence_for_seed(rand_func, seed_func, seed_val, length)
            seeds_for_sequences[tuple(sequence)] = seed_val
            seed_val += 1
    return seeds_for_sequences

<filename>tests/unit/seed_tree.py
"""This gathers information about the random number generator function so that
it can be seeded to give any particular series of virtual coin tosses up to a
certain length.
"""
import random

FAIR_COIN = lambda: random.randint(0, 1)
RANDOM_SEED = random.seed

def has_all_sequences(seeds_for_sequences, length):
    """Return True if we have all permutations up to the length specified."""
    return len(seeds_for_sequences) == 2 ** (length + 1) - 2

def sequence_for_seed(rand_func, seed_func, seed_val, length):
    """Return a list of booleans generated by rand_func after seeding."""
    seed_func(seed_val)
    return [rand_func() for _ in range(length)]

def find_seeds_for_sequences(max_length, seed_func, rand_func):
    """Return a dict mapping sequences to the random seed required to
    generate them.

    >>> find_seeds_for_sequences(FAIR_COIN, RANDOM_SEED, 2)
    {(0,): 1, (0, 0): 9, (0, 1): 8, (1,): 0, (1, 0): 10, (1, 1): 6}
    """
    seeds_for_sequences = {}
    for length in range(1, max_length + 1):
        seed_val = 0
        while not has_all_sequences(seeds_for_sequences, length):
            sequence = sequence_for_seed(rand_func, seed_func, seed_val, length)
            seeds_for_sequences[tuple(sequence)] = seed_val
            seed_val += 1
    return seeds_for_sequences

This gathers information about the random number generator function so that it can be seeded to give any particular series of virtual coin tosses up to a certain length. Return True if we have all permutations up to the length specified. Return a list of booleans generated by rand_func after seeding. Return a dict mapping sequences to the random seed required to     generate them.      >>> find_seeds_for_sequences(FAIR_COIN, RANDOM_SEED, 2)     {(0,): 1, (0, 0): 9, (0, 1): 8, (1,): 0, (1, 0): 10, (1, 1): 6}