# Calculate Approximate Health Care Budget in Python Homework Sample

You’ve been asked to estimate the health care costs for the next 2 year period. You have the 2 year statistics for the amount spent on each person during the last 3 periods of 2 years each. You need to take into account the increases in spending and the increases in population. For more Python programming assignments contact us for details.

Solution:

numeric = set(“0123456789.”)

QUICK = {“costs_2012.txt”: (71656661846.58186, 4132602),
“costs_2014.txt”: (77704444154.36708, 4372674),
“costs_2016.txt”: (84403490129.5643, 4527838),
}

“””
:param file: The filename to load
:return: The total, and number of entries
“””
if file in QUICK:
return QUICK[file]
total = 0 # Total sum of all values
people = 0 # The number of people involved
with open(file) as f: # Open the file
for line in f: # Read the number
# Get the digits and .
number = “”.join(ch for ch in line if ch in numeric)
# If it was a number
if number:
# Convert the number string into a numeric value
price = float(number)
total += price # Increase the total spent
people += 1 # Increase the number of records

def main():
“””
Run the logic
:return: None
“””
prev = []
titles = [“Total”, “Population”, “Avg”, “Total Change”, “Pop Change”, “Avg Change”]
print(“%12s %15s %14s %12s %12s %12s %12s” % (“File”, *titles))
# Process each file in order
for file in [“costs_2012.txt”, “costs_2014.txt”, “costs_2016.txt”]:
average = total / people # Calculate the average
data = [total, people, average] # For display
changes = [] # Percent changes
if prev: # If it is the first, we can’t calculate the change
for new, old in zip(data, prev[-1]): # Get the new value and old value
change = new – old # The change
changes.append(change / old * 100) # The percentage change
else:
changes = [0, 0, 0]
prev.append(data) # Keep the summary data
data += changes # Append the percentages for display
# Display the values
print(“%12s %13.2f %12.2f %12.2f %10.2f%% %10.2f%% %10.2f%%” % (file, *data))
print()