# pseudo code for initialize database
database = Database()
open "catalog.dat" for reading
for each line1 in "catalog.dat":
split line1 using "::" to get rname and rest of the line1
split rest of line1 using ";" to get individual attributes
split attribute using ":"
construct attributes using attribute split
r = Relation(rname,attributes)
dfile = "./"+dir+"/"+rname + ".dat"
open dfile for reading
for each line2 in dfile:
split line2 using ":" to get components of the tuple
construct tuple, t
insert t into r
close dfile
add r to database object
close catalog.dat
# factorial(n) = 1*2*....*(n-1)*n
# factorial(0) = 1
# factorial(n) = n*factorial(n-1)
factorial(4)
= 4 * factorial(3)
= 4 * 3 * factorial(2)
= 4 * 3 * 2 * factorial(1)
= 4 * 3 * 2 * 1 * factorial(0)
= 4 * 3 * 2 * 1 * 1
= 24
In [1]:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
In [2]:
factorial(5)
Out[2]:
120
In [3]:
def sum_list(xs):
if len(xs) == 0:
return 0
return xs[0] + sum_list(xs[1:])
In [4]:
sum_list([1,2,3,4,5])
Out[4]:
15
sum_list([1,2,3])
= 1 + sum_list([2,3])
= 1 + 2 + sum_list([3])
= 1 + 2 + 3 + sum_list([])
= 1 + 2 + 3 + 0
= 6
In [5]:
def max_list(xs):
if len(xs) == 0:
return None
if len(xs) == 1:
return xs[0]
m = max_list(xs[1:])
if xs[0] > m:
return xs[0]
return m
max_list([7,3,12])
= max(7,max_list([3,12]))
= max(7,max(3,max_list([12])))
= max(7,max(3,12))
= max(7,12)
= 12
In [6]:
max(2,3)
Out[6]:
3
In [7]:
max([1,2,3])
Out[7]:
3
In [8]:
max(1,2,3)
Out[8]:
3
In [9]:
max_list([10,5,12,3,7])
Out[9]:
12
In [10]:
def odd(n):
if n == 0:
return False
return even(n-1)
def even(n):
if n == 0:
return True
return odd(n-1)
odd(7)
= even(6)
= odd(5)
= even(4)
= odd(3)
= even(2)
= odd(1)
= even(0)
= True
In [11]:
odd(7)
Out[11]:
True
In [12]:
even(8)
Out[12]:
True
In [13]:
even(9)
Out[13]:
False
In [14]:
odd(6)
Out[14]:
False