### Exercises -- finish as many of these as you can during class.
# Write a function f(x,y) which
# returns 0 if x is negative and less than y; or
# returns 1 if x is non-negative and greater than y; or
# returns 2 if x is equal to y; or
# returns 3 otherwise
def f(x,y)
end
# Write function pow(x,y) which returns the number that is x to the y
# power. Don't cheat and use a built-in function!
def pow(x,y)
end
# Write function filter(a,x) which removes all occurences of x from array a.
# Try writing the functon without using Array.delete.
# Try writing the function so it returns a new array instead of operating
# in-place.
def filter(a, x)
end
# Write a program that prints the numbers from 1 to 100. But for multiples of
# three print “Fizz” instead of the number and for the multiples of five print
# “Buzz”. For numbers which are multiples of both three and five print
# “FizzBuzz”.
def fizzbuzz
end
# Write function fact(n) which returns n! (factorial). Try to make a
# version using a for loop, a while loop, and as a recursive function.
def fact(n)
end
# Implement the square root function using Newton's method by repeatedly
# applying the formula z = z - (z^2 - x) / 2z. Ie. initialize z to some value
# (eg. 1.0), compute z - (z^2 - x) / 2z, and then compute it again with the
# resulting value. Repeat this process "precision" times.
def sqrt(x, precision)
end
#---------------------------------------------------------------------
# In all of the functions that follow, the argument es is a list
# (array) of edges, where an edge is itself an array of size two. The
# arguments n and m are nodes, which are simply natural numbers
# (integers starting at 0). We interpret the edges as directed, so
# that [0,1] is an edge going from (source node) 0 to (target node)
# 1.
# Write a function countsource(es,n) that returns an integer, counting
# how often n is the source node (the first element of the pair) of
# any edge appearing in es. For example:
# countsource([[1,2],[2,3],[1,4],[4,5]], 1) returns 2
# countsource([[1,2],[2,3],[1,4],[4,5]], 2) returns 1
# countsource([[1,2],[2,3],[1,4],[4,5]], 6) returns 0
def countsource(es, n)
end
# Write a function istarget(es,n) that returns a boolean indicating
# whether n is the target node (the second element of the pair) of
# any edge appearing in es. For example:
# istarget([[1,2],[2,3],[1,4],[4,5]], 1) returns false
# istarget([[1,2],[2,3],[1,4],[4,5]], 2) returns true
# istarget([[1,2],[2,3],[1,4],[4,5]], 6) returns false
def istarget(es,n)
end
# Write a function sourceedges(es,n) that returns an array of those
# edges in es for which node n is the source node. For example:
# sourceedges([[1,2],[2,3],[1,4],[4,5]], 1) returns [[1,2],[1,4]]
# sourceedges([[1,2],[2,3],[1,4],[4,5]], 2) returns [[2,3]]
# sourceedges([[1,2],[2,3],[1,4],[4,5]], 6) returns []
def sourceedges(es, n)
end
# Write a function reachable(es,n,m) that returns a boolean indicating
# whether node m is reachable from node n, using the given list of
# edges. As examples:
#
# reachable([[1,2],[2,3],[1,4],[4,5]], 4, 5) returns true
# reachable([[1,2],[2,3],[1,4],[4,5]], 1, 3) returns true
# reachable([[1,2],[2,3],[1,4],[4,5]], 1, 5) returns true
# reachable([[1,2],[2,3],[1,4],[4,5]], 2, 5) returns false
# reachable([], 1, 5) returns false
# reachable(0, 1, 5) is a type error
#
# Don't worry about performance -- you haven't learned enough about Ruby
# data structures to do a great job yet
def reachable(es, n, m)
end