huogerac
12/31/2016 - 3:27 PM

## cofrinho.py

``````"""

>>> cofrinho(2016)
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104]

>>> cofrinho(2016, group_by='month')
[20, 52, 84, 150, 156, 188, 280, 260, 370, 332, 364, 500]

>>>
"""

def cofrinho(year, factor=2, group_by='week'):
"""
Return the amount of money to be saved per week.
"""

import arrow
initial_date = arrow.get(year, 1, 1)
weeks_from = year
week_number = 1
amount_saved = []
amount_saved_month = []
amount_per_month = 0
initial_month = 1

initial_date = initial_date.replace(weeks=+1)

while weeks_from == year:
amount_saved.append( week_number * factor )
# print "---> ", week_number, "-->", initial_date
initial_date = initial_date.replace(weeks=+1)

amount_per_month += week_number * factor
if initial_month != initial_date.month:
# print "    -->", amount_per_month
amount_saved_month.append(amount_per_month)
initial_month = initial_date.month
amount_per_month = 0

weeks_from = initial_date.year
week_number += 1

# print "amount_saved:", amount_saved
if group_by != 'week':
return amount_saved_month
return amount_saved

if __name__ == "__main__":
import doctest
doctest.testmod()
``````