def mysum(a : list):
sum = 0
for x in a:
sum += x
return sum
>>> mysum([3,5,1,2,6,4])
21
def myproduct(a : list):
product = 1
for x in a:
product *= x
return product
>>> myproduct([3,5,1,2,6,4])
720
def mymax(a : list):
guess = a[0]
for x in a[1:]:
if x > guess:
guess = x
return guess
>>> mymax([3,5,1,2,6,4])
6
def mymin(a : list):
guess = a[0]
for x in a[1:]:
if x < guess:
guess = x
return guess
>>> mymin([3,5,1,2,6,4])
1
def square(width : int, height : int, c : str):
for i in range(height):
print(c * width)
>>> square(5, 3, '*')
*****
*****
*****
>>> square(8, 4, '#')
########
########
########
########
def hollow_square(width : int, height : int, c : str):
if height == 1:
print(c * width)
elif width == 1:
for i in range(height):
print(c)
elif height > 1 and width > 1:
print(c * width)
for i in range(height - 2):
print(c + " " * (width - 2) + c)
print(c * width)
>>> hollow_square(1, 1, '$')
$
>>> hollow_square(1, 5, '$')
$
$
$
$
$
>>> hollow_square(5, 1, '$')
$$$$$
>>> hollow_square(3, 5, 'x')
xxx
x x
x x
x x
xxx
>>> hollow_square(2, 2, 'x')
xx
xx
>>> hollow_square(0, 2, 'x')
>>> hollow_square(2, 0, 'x')
>>>
def triangle(size : int, c : str):
for i in range(size):
print(c * (i + 1))
>>> triangle(1, '#')
#
>>> triangle(2, '#')
#
##
>>> triangle(5, '#')
#
##
###
####
#####
>>> triangle(0, '#')
>>>
def hollow_triangle(size : int, c : str):
if size == 1:
print(c)
elif size >= 2:
print(c)
for i in range(size - 2):
print(c + " " * i + c)
print(c * size)
>>> hollow_triangle(0, '#')
>>> hollow_triangle(1, '#')
#
>>> hollow_triangle(2, '#')
#
##
>>> hollow_triangle(3, '#')
#
##
###
>>> hollow_triangle(4, '#')
#
##
# #
####
>>> hollow_triangle(5, '#')
#
##
# #
# #
#####
>>> hollow_triangle(8, '#')
#
##
# #
# #
# #
# #
# #
########
>>>
Edge cases are cases that are close to the possible values for a function’s parameters or a program’s input
Example: for the hollow triangle function edges cases for the size are 0 (or a negative number), 1, 2, and 3
3 is an edge case because it’s the smallest size where spaces actually get printed
Always test the edge cases. They are the values that are most likely to break your program
def box(message, c):
print(c * (len(message) + 4))
print(c + " " + message + " " + c)
print(c * (len(message) + 4))
>>> box("This is a message!", "#")
######################
# This is a message! #
######################