Last active
May 15, 2022 21:12
-
-
Save NoahCardoza/378a85b0a394aa3bfd23450b9aacee02 to your computer and use it in GitHub Desktop.
Recursive Algorithm Modeling The Population Growth Of A Fluffle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Author : Noah Cardoza | |
School : De Anza | |
Class : MATH 22 | |
Useage : python rabbit.py <number-of-months> | |
""" | |
from argparse import ArgumentParser | |
from tabulate import tabulate | |
def breed(n): | |
"""Recursivelly breed rabbits until the nth month is reached | |
Args: | |
n (int): number of months of breeding | |
Returns: | |
int: the number of pairs of rabbits after n months | |
""" | |
if n <= 1: | |
return 1 | |
return breed(n-1) + breed(n-2) * 4 | |
def simulate(months): | |
"""Simulate the breeding of rabbits at monthly intervals | |
Args: | |
months (int): number of months to simulate | |
Returns: | |
iter[tuple[int, int]]: a list of tuples containing the month and number of pairs | |
""" | |
return ((n, breed(n)) for n in range(1, months + 1)) | |
def main(months): | |
"""Main function for the CLI""" | |
print(tabulate(simulate(months), headers=('Month', 'Pairs'))) | |
if __name__ == '__main__': | |
parser = ArgumentParser() | |
parser.add_argument('months', type=int, help='Number of months to simulate') | |
args = parser.parse_args() | |
main(args.months) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment