Rosalind: Mortal Fibonacci Rabbits

Python

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
SEXUAL_MATURITY_AGE = 1

def model_population_growth(generations: int,
                            lifespan: int,
                            sexual_maturity_age: int = SEXUAL_MATURITY_AGE) -> int:
    """
    Models population growth dynamics over time.

    Params:
        generations (int): The number of time points a population will go through
            before a census on their size.
        lifespan (int): The lifespan of any given member of the population.
        sexual_maturity_age (int, optional): The age at maturity for any given member of
            the population. Defaults to the value of SEXUAL_MATURITY_AGE.

    Returns:
        int: The number of members in a population after a given number of generations.

    Examples:
        >>> population_size = model_population_growth(5, 10)
        >>> print(population_size)
        10
    """
    # A list of length equal to the lifespan of a population member
    # Each element in the list is a bin for members at that stage in life
    population = [1] + (lifespan - 1) * [0]
    # Iterating `generations` minus 1 times to get the population size after `generations` times
    for _ in range(generations - 1):
        # Subset for the sexually mature population
        sexually_mature_members = population[sexual_maturity_age:]
        # Each member produces 1 offspring
        offspring = sum(sexually_mature_members)
        # Adding offspring to the earliest stage of life
        population = [offspring] + population[:-1]
    return sum(population)

Solution 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
class Animal(object):
    def __init__(self):
        """Initialize an Animal object with an initial age of 0."""
        self.age = 0

    @property
    def reached_lifespan(self):
        """
        Check if the animal has reached its lifespan.

        Returns:
            bool: True if the animal has reached its lifespan, False otherwise.
        """
        return self.age >= LIFESPAN

    @property
    def is_sexually_mature(self):
        """
        Check if the animal is sexually mature.

        Returns:
            bool: True if the animal is sexually mature, False otherwise.
        """
        return self.age > SEXUAL_MATURITY_AGE

    def procreate(self):
        """
        Procreate and return a new Animal instance if the animal is sexually mature.

        Returns:
            Animal|None: A new Animal instance if the animal is sexually mature, None otherwise.
        """
        if self.is_sexually_mature:
            return self.__class__()

    def epoch(self):
        """Increment the animal's age by 1."""
        self.age += 1


class Population(object):
    def __init__(self, nanimals: int):
        """
        Initialize a Population object with a specified number of animals.

        Args:
            nanimals (int): The number of animals in the population.
        """
        self.animals = [Animal() for _ in range(nanimals)]

    def procreate(self):
        """Procreate and add offspring to the population for sexually mature animals."""
        for i, _ in enumerate(self.animals):
            offspring = self.animals[i].procreate()
            if offspring is not None:
                self.animals.append(offspring)

    def cull(self):
        """Remove animals from the population that have reached their lifespan."""
        self.animals = [animal for animal in self.animals if not animal.reached_lifespan]

    def epoch(self):
        """Advance the population by one epoch, incrementing the age of each animal."""
        for animal in self.animals:
            animal.epoch()

    def census(self):
        """
        Count the number of animals in the population.

        Returns:
            int: The number of animals in the population.
        """
        return len(self.animals)
0%