import math

#an array is another name for a list
def print_2d_array(arr, max_col_width):
    #print("...print_2d_array...")
    
    for row in arr:
        for val in row:
            #max_col_width is a 'string format specifier'
            formatted_val = f"{val:>{max_col_width}}"
            print(formatted_val, end="")
        print()
#end print_2d_array

def main():
    print("=== Pythag Triple Table Driver ===")
    # get user-input: how many triples
    #n = int(input("\nHow many triples? (n > 0) "))
    
    #for advanced input, the technique below
    #gives more options
    n_str = input("\nHow many triples? (n > 0) ")
    n = int(n_str)
    if n < 0:
        n = 1
    
    # initialize the table with headers and separators
    headers = ["n", "x", "y", "|", "a", "b", "c"] #a list container
    #print(headers)
    
    #create a list of 7 dashes (explicitly)
    separators = []
    for i in range(7):
        separators.append("-")
    
    #print(separators)
    
    trip_table = [headers, separators] #a list of lists
    #print(trip_table)
    
    # setup column sizes and control variables
    count = 0
    c_max = 1 #used to determine max col width
    row = 2

    # generate Pythagorean triples
    # a loop inside a loop: nested loop
    for x in range(2, n + 2):
        for y in range(x-1, 0, -1):
            count += 1
            a = x*x - y*y
            b = 2*x*y
            c = x*x + y*y
            trip_table.append([str(count), str(x), str(y), "|", str(a), str(b), str(c)])
            if c > c_max:
                c_max = c
            row += 1
            if count == n:
                break
        #end y loop
        if count == n:
            break
    #end x loop   
    

    # determine column width based on the largest value of c

    # print the table
    print()
    col_width = 3
    print_2d_array(trip_table, col_width)
    print("\nThanks for using our program!")
#end main


if __name__ == "__main__":
    main()