Week 8: Homework Solutions

2.

"""
Print out tables of two functions getting bigger.
"""


def limit_to_infinity_table(f, n):
    """Print a table of value of f(x) up to 10**n"""

    horizontal_line = "-"*(19+n)

    print(horizontal_line)
    # Attempt to center x and f(x) at the top of the columns
    template = (" "*(n//2+1) + "{0:"+str(n//2+1)+"}  " +
                " "*6 + "{1:14}")
    print(template.format("x", "f(x)"))
    print(horizontal_line)

    for i in range(n+1):
        print(("{0: "+str(n+2)+"}  {1: 14.9f}").
              format(10**i, f(10**i)))

    print(horizontal_line, "\n")


def f_3(x):
    """Calculate x**(1/x)."""
    return x**(1/x)


def f_4(x):
    """Calculate (1+1/x)**x."""
    return (1+1/x)**x


print("\nf(x) = x**(1/x).")
limit_to_infinity_table(f_3, 10)

print("\nf(x) = (1+1/x)**x.")
limit_to_infinity_table(f_4, 10)