Skip to content

Instantly share code, notes, and snippets.

@NoahCardoza
Last active May 15, 2022 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoahCardoza/378a85b0a394aa3bfd23450b9aacee02 to your computer and use it in GitHub Desktop.
Save NoahCardoza/378a85b0a394aa3bfd23450b9aacee02 to your computer and use it in GitHub Desktop.
Recursive Algorithm Modeling The Population Growth Of A Fluffle
"""
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