Problem 1: Center Grid Circle
def center_circle_soln(grid, radius, ch):
# First, put an uppercase ch in the center of the grid.
# Next, fill in all grid locations with ch that create a circle
# in the center of the grid with the given radius.
# Basic idea: traverse entire grid, and if a coordinate
# has a distance to the center of the radius, put ch in that
# grid location
center_x = grid.width // 2
center_y = grid.height // 2
grid.set(center_x, center_y, ch.upper())
for y in range(grid.height):
for x in range(grid.width):
if distance(x, y, center_x, center_y) == radius:
grid.set(x, y, ch)
def distance(x1, y1, x2, y2):
return int(math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)))
def main():
# Usage:
# python3 circles.py width height radius ch
width = int(sys.argv[1])
height = int(sys.argv[2])
radius = int(sys.argv[3])
ch = sys.argv[4][0]
grid = Grid(width, height)
center_circle(grid, radius, ch)
print_grid(grid)
Problem 2: spell_number
def spell_number_soln(n):
if n == 100:
return 'one hundred'
if n > 100:
return str(n)
low_numbers = ['zero', 'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten',
'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen',
'sixteen', 'seventeen',
'eighteen', 'nineteen']
tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty',
'ninety', 'one hundred']
if n < 20:
return low_numbers[n]
ones_digit = n % 10
tens_digit = n // 10
word = tens[tens_digit - 2]
if ones_digit > 0:
word += f'-{low_numbers[ones_digit]}'
# alternate: word += '-' + low_numbers[ones_digit]
return word
Problem 3: Lines Illusion
def lines_illusion_soln(canvas, width, height, num_lines):
side_line_length = width / num_lines / 3
num_y_lines = num_lines * 5
# canvas.draw_rect(0, 0, width, height)
for i in range(num_y_lines):
y_start = i / num_y_lines * height
y_end = y_start + side_line_length
for j in range(num_lines):
if j % 2 == 0:
x_start = j / num_lines * width
x_end = x_start + side_line_length
else:
x_end = j / num_lines * width
x_start = x_end + side_line_length
canvas.draw_line(x_start, y_start, x_end, y_end)