MTHSC 360 HW11

3349 days ago by lpmcint

MTHSC 360 - HW11


Your name: _____________________________

Date: ________________

 

 
       

Exercise 1:  Five ways to count by three's.  In this exercise generate a List of the multiples of 3 running from 3 to 99 inclusive in five different ways.

  1. Create an empty list, L1, then using a while loop fill it with the multiples of 3 running from 3 to 99 inclusive.
  2. Create an empty list, L2, then using a for loop fill it with the multiples of 3 running from 3 to 99 inclusive.
  3. Using list comprehension create a list, L3, that contains the multiples of 3 running from 3 to 99 inclusive.
  4. Using the range function create a list, L4, that contains the multiples of 3 running from 3 to 99 inclusive.
  5. Using the ellipsis notation create a list, L5, that contains the multiples of 3 running from 3 to 99 inclusive.
L1 = [] k=3 while k < 100: L1.append(k) k = k+3 L1 
       
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54,
57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
L2 = [] for x in range(3,100,3): L2.append(x) L2 
       
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54,
57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
L3 = [x for x in range(3,100) if x%3==0] L3 
       
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54,
57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
L4 = range (3,100,3) L4 
       
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54,
57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
L5 = [3,6,..,99] L5 
       
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54,
57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

Exercise 2:  Four regular polygons.  For each of the following cases generate the coordinates of vertices of the specified regular polygon, then using the built-in line function draw and display the polygon.

  1.  N = 3, the equilateral triangle
  2.  N = 4, the square
  3.  N = 5, the regular pentagon
  4.  N = 6, the regular hexagon

Before you start coding you should decide on the size and orientation of you polygons.  Then you should try to draw each one with code that is both clear and concise.

N = 3 g = Graphics() theta = 2.0*pi/N points = [] for k in range(N): xk = cos(k*theta) yk = sin(k*theta) points.append((2*xk+2,2*yk+2)) g += point(points,pointsize=20) for i in range(N): g += line([points[i-1],points[i]]) show(g,aspect_ratio=1) 
       
N = 4 g = Graphics() theta = 2.0*pi/N points = [] for k in range(N): xk = cos(k*theta) yk = sin(k*theta) points.append((2*xk+2,2*yk+2)) g += point(points,pointsize=20) for i in range(N): g += line([points[i-1],points[i]]) show(g,aspect_ratio=1) 
       
N = 5 g = Graphics() theta = 2.0*pi/N points = [] for k in range(N): xk = cos(k*theta) yk = sin(k*theta) points.append((2*xk+2,2*yk+2)) g += point(points,pointsize=20) for i in range(N): g += line([points[i-1],points[i]]) show(g,aspect_ratio=1) 
       
N = 6 g = Graphics() theta = 2.0*pi/N points = [] for k in range(N): xk = cos(k*theta) yk = sin(k*theta) points.append((2*xk+2,2*yk+2)) g += point(points,pointsize=20) for i in range(N): g += line([points[i-1],points[i]]) show(g,aspect_ratio=1) 
       

Exercise 3:  Complete the definition of a function which given $N$ will return the graphic image of the regular polygon with $N$ sides.  Then test your function by executing the subsequent block.

def regular_polygon(N): g = Graphics() theta = 2.0*pi/N points = [] for k in range(N): xk = cos(k*theta) yk = sin(k*theta) points.append((2*xk+2,2*yk+2)) g += point(points,pointsize=20) for i in range(N): g += line([points[i-1],points[i]]) return g 
       
show(regular_polygon(3),aspect_ratio=1) show(regular_polygon(4),aspect_ratio=1) show(regular_polygon(5),aspect_ratio=1) show(regular_polygon(6),aspect_ratio=1) 
       






Exercise 4:  Construct a function, build_F, which returns a two-dimensional list, F, with N rows and M columns.  The entry at F[i][j] should be the tuple (i,j+1).  Then evaluate the subsequent block.

def build_F(N,M): F = [] for i in range(N): row = [] for j in range(M): row.append((i,j+1)) F.append(row) return F 
       
N = 10 M = 10 F = build_F(N,M) for i in range(N): print F[i] 
       
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9),
(0, 10)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9),
(1, 10)]
[(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9),
(2, 10)]
[(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9),
(3, 10)]
[(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9),
(4, 10)]
[(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9),
(5, 10)]
[(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9),
(6, 10)]
[(7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9),
(7, 10)]
[(8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9),
(8, 10)]
[(9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9),
(9, 10)]
[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10)]
[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10)]
[(2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10)]
[(3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10)]
[(4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10)]
[(5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10)]
[(6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10)]
[(7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10)]
[(8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10)]
[(9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10)]

Using upward sloping diagonals we can write this two dimensional array as a one dimensional list.  It would start as follows:

[(0,1), (1,1), (0,2), (2,1), (1,2), (0,3), (3,1), . . . ]

Exercise 5:  Construct a function, build_F1d, which returns this one dimensional list.  You can assume that N and M are as large as necessary to insure that no upward sloping diagonal is cut short. Then evaluate the subsequent block.

def build_F1d(n): F1d = [(0,1)] if n <= 0: return F1d count = 1 for diag in range (1,n): i = diag j = 1 while 0<= i: F1d.append ((i,j)) count += 1 if count == n: return F1d i -= 1 j += 1 
       
print build_F1d(7) print build_F1d(15) 
       
[(0, 1), (1, 1), (0, 2), (2, 1), (1, 2), (0, 3), (3, 1)]
[(0, 1), (1, 1), (0, 2), (2, 1), (1, 2), (0, 3), (3, 1), (2, 2), (1, 3),
(0, 4), (4, 1), (3, 2), (2, 3), (1, 4), (0, 5)]
[(0, 1), (1, 1), (0, 2), (2, 1), (1, 2), (0, 3), (3, 1)]
[(0, 1), (1, 1), (0, 2), (2, 1), (1, 2), (0, 3), (3, 1), (2, 2), (1, 3), (0, 4), (4, 1), (3, 2), (2, 3), (1, 4), (0, 5)]

Exercise 6:  Construct a function, F1d, that given n returns the n-th tuple that would have been created by build_F1.

def F1d(n): if n < 0: return None if n <= 0: return(0,1) count = 1 for d in range(1,N): i = d j = 1 while 0<= i: if count == n: return(i,j) i = i-1 j = j+1 count += 1 
       
for n in range(15): print n,F1d(n) 
       
0 (0, 1)
1 (1, 1)
2 (0, 2)
3 (2, 1)
4 (1, 2)
5 (0, 3)
6 (3, 1)
7 (2, 2)
8 (1, 3)
9 (0, 4)
10 (4, 1)
11 (3, 2)
12 (2, 3)
13 (1, 4)
14 (0, 5)
0 (0, 1)
1 (1, 1)
2 (0, 2)
3 (2, 1)
4 (1, 2)
5 (0, 3)
6 (3, 1)
7 (2, 2)
8 (1, 3)
9 (0, 4)
10 (4, 1)
11 (3, 2)
12 (2, 3)
13 (1, 4)
14 (0, 5)

Exercise 7:  Construct the recursive function, fusc, which is defined by the following properties:

  1.  fusc(0) = 0
  2.  fusc(1) = 1
  3.  fusc(2n) = fusc(n)
  4.  fusc(2n+1) = fusc(n) + fusc(n+1)

Then evaluate the subsequent block.  Hint, the first 6 values are: 0, 1, 1, 2, 1, 3.

def fusc(n): if n < 0: return None if n == 0: y = 0 elif n == 1: y = 1 elif n%2==0: y = fusc(n/2) else: y = fusc((n-1)/2) + fusc((n-1)/2+1) return y 
       
[fusc(k) for k in range(64)] 
       
[0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, 1, 5, 4, 7, 3, 8, 5, 7,
2, 7, 5, 8, 3, 7, 4, 5, 1, 6, 5, 9, 4, 11, 7, 10, 3, 11, 8, 13, 5, 12,
7, 9, 2, 9, 7, 12, 5, 13, 8, 11, 3, 10, 7, 11, 4, 9, 5, 6]
[0, 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, 1, 5, 4, 7, 3, 8, 5, 7, 2, 7, 5, 8, 3, 7, 4, 5, 1, 6, 5, 9, 4, 11, 7, 10, 3, 11, 8, 13, 5, 12, 7, 9, 2, 9, 7, 12, 5, 13, 8, 11, 3, 10, 7, 11, 4, 9, 5, 6]