module-2-annotated 2025-04-13, 9:43 PM
In [4]:
from cs103 import *
Module 2, Day 1 Notes
Here is a summary of our How to Design Functions Recipe:
1. Write:
A. the typecheck annotation,
B. signature,
C. purpose, and
D. stub (technically, the body of the stub).
2. Write examples/tests
3. Write or copy the template
4. Code the function body
5. Test and debug until correct
meets_expectations Quiz Problem
Let's revisit the steps of: Design a function that takes a string and determines whether
or not it meets expectations. By expectations, we mean the word is a palindrome and is
at least 5 characters long (i.e., there are at least 5 letters in the word). A palindrome is
a word or phrase that reads the same backwards and forwards (e.g., level).
In [5]:
# Design meets_expectations here.
# We already included the signature, TWO possible purposes, and a stub.
# We still need: tests/examples, template comment, and body.
# If we do not have a stub body e.g. no return True
# then we will get a typecheck error because our function
# now returns None.
# typecheck ensures that our function returns the correct type
# we claim to say, and it polices the user from calling the
# function with a value of an incorrect type.
# Your stub value can be any value that matches the return type
# e.g. If your function returns int,
# stubs like return 0 / return 4 / return -123 are all valid.
# The template is not meant to run. You will get an error, but that's okay.
@typecheck
about:srcdoc Page 1 of 4
module-2-annotated 2025-04-13, 9:43 PM
def meets_expectations(word: str) -> bool:
"""
return True if the word is a palindrome with at least five characters, and Fals
"""
# return True # stub
# return ...(word) # template - tells us to do some computation with word
# Coding the function body.
# Let's make a plan
# Two things we need to do:
# (1) Whether it's a palindrome
# (2) Whether the length is at least 5
# Let's focus on (2) first.
# Use len to determine the length of the string
if len(word) >= 5:
# do something
# We need to check if it's a palindrome
# Know how to reverse a string? Check the Python Language Rules!
if word == word[::-1]:
return True
else:
return False
else:
# do something else
return False
# Can we shorten the implementation of this function?
# Yes! Alternative function bodies could be:
# if len(word) >= 5:
# return word == word[::-1]
# else:
# return False
# Or it can be even shorter as:
# return len(word) >= 5 and word == word[::-1]
# How do we determine how many tests to write?
# Two axes of variation in our inputs:
# (1) Whether it's a palindrome
# (2) The length of the string (whether the length is >= 5)
start_testing()
# The string is not a palindrome and it is less than 5 characters long
# Use expect(____, _____)
# The first blank we will fill with a call to the function we are testing
# The second blank will be the expected return value from the function
expect(meets_expectations("abc"), False)
# The string is not a palindrome and it is more than five characters long
expect(meets_expectations("abcdef"), False)
# Think about what other test cases do we need
# Is a palindrome and less than five characters
expect(meets_expectations("mom"), False)
# Is not a palindrome and equal to five characters
about:srcdoc Page 2 of 4
module-2-annotated 2025-04-13, 9:43 PM
expect(meets_expectations("birds"), False)
# Is a palirome and more than five characters
expect(meets_expectations("racecar"), True)
# Is a palindrome and exactly five characters
expect(meets_expectations("civic"), True)
# When we have a string as input, the empty string
# is like a special case.
expect(meets_expectations(""), False)
# What if we had "Civic", let's assume lowercase for now.
summary()
7 of 7 tests passed
In [6]:
meets_expectations(True)
---------------------------------------------------------------------------
TypecheckError
while checking parameter word: True is a bool, not a str
File "<ipython-input-6-476d50b0c5f6>", line 1, in <module>
meets_expectations(True)
as expected in
@typecheck
def meets_expectations(word: str) -> bool:
"""
return True if the word is a palindrome with at least five characters, a
nd False otherwise
"""
# return True # stub
# return ...(word) # template - tells us to do some computation with wor
d
# Coding the function body.
# Let's make a plan
# Two things we need to do:
# (1) Whether it's a palindrome
# (2) Whether the length is at least 5
# Let's focus on (2) first.
# Use len to determine the length of the string
if len(word) >= 5:
# do something
# We need to check if it's a palindrome
# Know how to reverse a string? Check the Python Language Rules!
if word == word[::-1]:
return True
else:
return False
else:
# do something else
about:srcdoc Page 3 of 4
module-2-annotated 2025-04-13, 9:43 PM
return False
---------------------------------------------------------------------------
TypecheckError Traceback (most recent call last)
<ipython-input-6-476d50b0c5f6> in <module>
----> 1 meets_expectations(True)
/opt/conda/lib/python3.8/site-packages/cs103/typecheck/[Link] in wrappe
r(*args)
128 for (name, val) in zip(parm_names, args):
129 if name in types:
--> 130 subtype("parameter %s" % name, val, types[name], fn, T
rue)
131 else:
132 # Type missing for a parameter.
/opt/conda/lib/python3.8/site-packages/cs103/typecheck/[Link] in subtyp
e(value_description, va, tb, fn, strict, error_type)
102 elif not issubclass(ta, tb):
103 if strict:
--> 104 raise error_type(va,name,astr(tb), fn,value_description)
105 return False
106
TypecheckError: while checking parameter word: True is a bool, not a str
In [ ]:
about:srcdoc Page 4 of 4