Calculating the sign (-1,1 or 0) of a number:
def sign_function(x):
if x < 0:
return -1
elif x == 0:
return 0
else:
return 1
print sign_function(8)
The sign function is a function, but not a recursive function.
The following function is recursive because it calls itself.
This means that the name of the function shows up in a return statement.
Calculating factorials:
def fact(n):
if n == 0 or n == 1:
return 1
else:
return (n * fact(n-1))
print fact(7)
2) Write a recursive function that determines whether a word is a palindrome (i.e. it reads the same in either direction, such as radar, civic, rotator). Hints:
3) Calculate the greatest common divisor of two integers using the Euclidean algorithm:
For example: find the greatest common divisor of 26 and 14.
m = 14; n = 26
26 % 14 = 12
find the greatest common divisor of (14, 12)
m = 12; n = 14
14 % 12 = 2
find the greatest common divisor of (12, 2)
m = 2; n = 12
12 % 2 = 0
find the greatest common divisor of (2, 0)
m = 0; n = 2
return 2
4) Optional exercise: write a recursive function that checks whether an expression contains matching, nested brackets. For example, the expression "(c(()b))" is ok, whereas ")n()" is not. The brackets must be nested one inside the other, thus, "()()" is not ok.
properties/points | graph |
---|---|
domain | x-values for which the function is defined |
range/image (a subset of the co-domain) | y-values which the function actually takes |
symmetric | mirrored along y-axis |
continuous | can be drawn in one line without stopping |
bijective/strictly monotonous | graph is strictly increasing or strictly decreasing |
bounded | values don't approach infinity |
root/zero | point where the graph intersects with x-axis |
local maximum or minimum | point where graph changes between rising/falling |
global maximum or minimum | highest/lowest point of the graph |
inflection point | graph changes its concavity |
Use Wolfram Alpha to check the properties of the following functions based on their graphs: