Lesson 1 – Challenge Solutions

Challenge 1.1
def border_text(text_var):
     # Let's create our function

     # Take the length of the text and multiply it by "=" (the border)
     border = '='*len(text_var)

     # Print the border, the test, and then the border with separating lines

     print(border+'\n'+text_var+'\n'+border)

     return 0


border_text("Hello World")

border_text("World")
Output
===========
Hello World
===========
=====
World
=====
Challenge 1.2
# First, import the math library
import math

# The math library's sin, cos, tan, etc. functions
#  deal in radians so we need to convert degrees to radians
angle_b = math.radians(54)

# Let's find side c
# cos(54 degrees)=4 inches/c inches
# c inches*cos(54 degrees)=4 inches
# c inches=4 inches/cos(54 degrees)
side_c = 4/math.cos(angle_b)

# Now let's find side b
# sin(54 degrees)=b inches/side_c
# side_c*sin(54 degrees)=b inches
side_b = side_c*math.sin(angle_b)

# Okay, let's print everything out
print("Side a: 4 inches")
print("Side b: "+str(side_b)+" inches")
print("Side c: "+str(side_c)+" inches")
Output
Side a: 4 inches
Side b: 5.505527681884694 inches
Side c: 6.805206466816319 inches
Challenge 1.3
# Used to prevent a link from the last_level and pascals_triangle
import copy

# Let's create our list and start it will 2 levels
pascals_triangle = [[1], [1, 1]]

# Let's create a while loop that will stop on the tenth level
while len(pascals_triangle) != 10:

    # We need a copy otherwise it will be linked to pascals_triangle
    last_level = copy.deepcopy(pascals_triangle[-1])

    # Okay, for our next loop, it's going to create the next level
    new_level = [1]
    while 1:
        new_level.append(last_level[0]+last_level[1])
        last_level.pop(0)
        if len(last_level) == 1:
            new_level.append(1)
            break

    pascals_triangle.append(new_level)


print(pascals_triangle[0])
print(pascals_triangle[1])
print(pascals_triangle[2])
print('...')
print(pascals_triangle[8])
print(pascals_triangle[9])
Output
[1]
[1, 1]
[1, 2, 1]
...
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]