From 572e314a2d691c0ff84ea14e8dd8c4343bf1bced Mon Sep 17 00:00:00 2001 From: rajban1918 Date: Sun, 9 Nov 2014 21:24:22 -0800 Subject: [PATCH 1/3] adding grid assignment --- .../ralbright/Session01/grid session01.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Students/ralbright/Session01/grid session01.py diff --git a/Students/ralbright/Session01/grid session01.py b/Students/ralbright/Session01/grid session01.py new file mode 100644 index 00000000..5e5c7d38 --- /dev/null +++ b/Students/ralbright/Session01/grid session01.py @@ -0,0 +1,66 @@ +# def grid1(): +# print '+ - - - - + - - - - +' +# print '| | |' +# print '| | |' +# print '| | |' +# print '| | |' +# print '+ - - - - + - - - - +' +# print '| | |' +# print '| | |' +# print '| | |' +# print '| | |' +# # print '+ - - - - + - - - - +' + +# #grid1() + +def printed_grid(n): # This function lets you choose how many boxes, with 2 columns only + s = '+ - - - - + - - - - +' + '\n' + t = ('| | |' + '\n')*4 + mid_lines = int((n-2) // 2) + middle = mid_lines*(t + s) + t + + if n ==1: + print "Sorry, you need more than 1 box!" + + if n%2 != 0: + print "The number of boxes is: ",n,"- 1 =", n-1 + n = n - 1 + elif n%2==0: + print "The number of boxes is: ", n + + if n != 0: + print s, middle, s + +# printed_grid(6) + +def printed_grids(length): #This function changes the sizes of the boxes , but is always a 2x2 grid + c = int((length-2)//2) + s = ('+' + ' -' * c + ' +' + ' -' * c + ' +') + '\n' + t = ('| ' + ' ' * c + '| ' + ' ' * c + '|') + '\n' + middle = t*c + s + t*c + + if (length<4) or (length%2 == 0): + print 'Sorry! The argument should be bigger than 3 and an odd number' + else: + print s,middle,s + +# printed_grids(19) + +def dynamic_printed_grids(rows, columns, size): #this function lets you choose how many rows, columns, and the size of the boxes + cSize = int((size-2)//2) + s = '+ ' + ((('- ' * cSize ) + '+ ')* columns) + '\n' + t = '| ' + ((' ' * cSize + '| ') * columns) + '\n' + middle = (t*cSize + s) * rows + + if size <2: + print "Choose an integer greater than 2" + elif size%2 != 0: + print "The size is: ", size, "- 1 =", size - 1, "and there are", rows, "rows with", columns, "columns" + else: + print "The size is", size, "and there are", rows, "rows with", columns, "columns" + + if size >=2: + print s, middle + +dynamic_printed_grids(4,3,6) + From bc0ea784fdddaa2861d8514c901e790b4a6e0d37 Mon Sep 17 00:00:00 2001 From: rajban1918 Date: Sun, 16 Nov 2014 18:11:23 -0800 Subject: [PATCH 2/3] added ackermann --- Students/ralbright/Session02/ack.py | 39 +++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Students/ralbright/Session02/ack.py diff --git a/Students/ralbright/Session02/ack.py b/Students/ralbright/Session02/ack.py new file mode 100644 index 00000000..a6af098c --- /dev/null +++ b/Students/ralbright/Session02/ack.py @@ -0,0 +1,39 @@ +def ack (m, n): + """Return a value based on the Ackermann-Peter function. + Testing against the wikipedia page for inputs between 0 and 4. + """ + if m < 0 or n < 0: #This is so it doesn't compute for negative numbers + return None + elif m == 0: + return n + 1 + elif n == 0: + return ack(m - 1, 1) + else: + return ack(m - 1, ack(m, n - 1)) + + +if __name__ == "__main__": + assert ack(-1, 0) == None + assert ack(0,0) == 1 + assert ack(0,1) == 2 + assert ack(0,2) == 3 + assert ack(0,3) == 4 + assert ack(0,4) == 5 + assert ack(1,0) == 2 + assert ack(1,1) == 3 + assert ack(1,2) == 4 + assert ack(1,3) == 5 + assert ack(1,4) == 6 + assert ack(2,0) == 3 + assert ack(2,1) == 5 + assert ack(2,2) == 7 + assert ack(2,3) == 9 + assert ack(2,4) == 11 + assert ack(3,0) == 5 + assert ack(3,1) == 13 + assert ack(3,2) == 29 + assert ack(3,3) == 61 + assert ack(3,4) == 125 + assert ack(4,0) == 13 + +print "all tests pass" \ No newline at end of file From da956ef34cfd71588b69e6615b308d41a0ee3804 Mon Sep 17 00:00:00 2001 From: rajban1918 Date: Sun, 16 Nov 2014 18:11:53 -0800 Subject: [PATCH 3/3] added series hw --- Students/ralbright/Session02/series.py | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Students/ralbright/Session02/series.py diff --git a/Students/ralbright/Session02/series.py b/Students/ralbright/Session02/series.py new file mode 100644 index 00000000..eac1b101 --- /dev/null +++ b/Students/ralbright/Session02/series.py @@ -0,0 +1,56 @@ +def fib(n): + """Return the nth value in the fibonacci function, which is a recursive function. """ + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) + +# print fib(7) + + +def lucas(n): + """Return the nth value in the Lucas function, which is also a recursive function but starts with 2 and 1 rather than 0 and 1""" + if n == 0: + return 2 + elif n == 1: + return 1 + else: + return lucas(n-1) + lucas(n-2) +# print lucas (2) +# print lucas (7) + + +def sum_series(n, x=0,y=1): + """ Return the nth value of a recursive function. There are two optional parameters, x and y. + x is the 0th value and y is the 1st value. + + When x = 0 and y = 1, the result is the fibbonacci sequence + When x = 2 and y = 1, the result is the lucas sequence + When x and y are other values, there are different sequences + """ + if n == 0: + return x + elif n == 1: + return y + else: + return sum_series(n-1, x, y) + sum_series(n-2, x, y) # it took me a while to figure out why it was going back to fibbonacci + # even when x =2, of course have to re-add x and y in the else part!!! + +# print sum_series(0,2) +# print sum_series(1,0,4) +# print sum_series(2) +# print sum_series(7) +# print sum_series(7,2) +# print sum_series(7,3,1) + + +if __name__ == "__main__": + assert fib(7) == 13 + assert lucas (7) == 29 + assert sum_series(7) == 13 + assert sum_series(7,2) == 29 + assert sum_series(7,3) == 37 + +print "all good here" \ No newline at end of file