# function mymin(a, b){
# if (a < b){
# return a;
# } else {
# return b;
# };
# }
#
# function square(x){
# return x * x;
# }
#
# write(mymin(square(-2), square(3)));
def mymin(a, b):
if (a < b):
return a
else:
return b
def square(x):
return x * x
print mymin(square(-2), square(3))
[1,2,3,4,5,6,7]
def odds_only(numbers):
for N in numbers:
if N % 2 == 1:
yield N
def small_words(words):
for word in words:
if len(word) <= 3:
yield word
print [w for w in small_words(["The","quick","brown","fox"])]
grammar = [
("exp",["exp","+","exp"]),
("exp",["exp","-","exp"]),
("exp",["(","exp",")"]),
("exp",["num"]),
]
def expand(tokens, grammar):
for pos in range(len(tokens)):
for rule in grammar:
depth = 1
utterances = [["exp"]]
def sublists(big_list, selected_so_far):
if big_list == []:
print selected_so_far
else:
current_element = big_list[0]
rest_of_big_list = big_list[1:]
sublists(rest_of_big_list, selected_so_far + [current_element])
sublists(rest_of_big_list, selected_so_far )
dinner_guests = ["LM", "ECS", "SBA"]
sublists(dinner_guests, [])
def cfgempty(grammar, symbol, visited):
if symbol in visited:
return None
elif not any([rule[0] == symbol for rule in grammar ]):
return [symbol]
else:
new_visited = visited + [symbol]
for rhs in [r[1] for r in grammar if r[0] == symbol]:
if all([None] != cfgempty(frammar, r, new_visited) for r in rhs):
result = []
for r in rhs:
result = result + cfgempty(grammar, r, new_visited)
return result
return None