by shreshta (content) + yuan (slides)
as a programming paradigm: using a function that calls itself until a specified condition is met
def fibonacci(n):
"return the nth fibonacci number"
# base case 1: f1 = 0
if n == 1:
return 0
# base case 2: f2 = 1
elif n == 2:
return 1
# if n > 2, call the function
else:
return fibonacci(n-1) + fibonacci(n-2)base cases:
function:
in natural language: "nonterminal" (e.g., S, NP, VP) is expanded into a set of terminals and nonterminals, which are themselves recursively expanded
[The boy] [believed [the clown laughed]].
DET + N
V
DET + N
V
NP
VP
S
useful data structure for hw3
# store word choices
nouns = {1: "Olivia", 2: "Liam", 3: "baker", 4: "telescope", 5: "animals"}
# initialize an empty dict
sentence_freq = {}
# loop through sentences you generated
for s in sentences:
# if already in dict, increase count by 1
if s in sentence_freq:
sentence_freq[s] += 1
# otherwise, start count at 1
else:
sentence_freq[s] = 1