|
First Summer 2006 |
def mat(size):
a = "*" * size
m = []
for i in range(size):
m.append(a.split())
return mHere's how it works:
>>> mat(4) [['****'], ['****'], ['****'], ['****']] >>> mat(3) [['***'], ['***'], ['***']] >>>
Now you're thinking: couldn't we teach the matrix to show itself nicely?
class Matrix(object):
def __init__(self, size):
a = "*" * size
m = []
for i in range(size):
m.append(a.split())
self.m = mSo you choose an object oriented approach:
>>> m = Matrix(4) >>> m <__main__.Matrix object at 0x00BE5290> >>> m.m [['****'], ['****'], ['****'], ['****']] >>>
It now becomes possible to work on rendering:
class Matrix(object):
def __init__(self, size):
a = " *" * size
m = []
for i in range(size):
m.append(a.split())
self.m = m
def report(self):
for row in self.m:
for elem in row:
print elem,
print
You will also notice that the constructor has changed a bit (in the interest of split()).
class Matrix(object):
def __init__(self, size):
a = " *" * size
m = []
for i in range(size):
m.append(a.split())
self.m = m
def report(self):
for row in range(len(self.m)):
for col in range(len((self.m)[row])):
print (self.m)[row][col],
print Reporting also needs to change since we express our patterns in terms of properties of rows and columns.
class Matrix(object):
def __init__(self, size):
a = " *" * size
m = []
for i in range(size):
m.append(a.split())
self.m = m
def report(self):
size = len(self.m)
for row in range(size):
for col in range(size):
if row == 0 or row == size-1 or row + col == size - 1:
print self.m[row][col],
else:
print " ",
print So we can change it to print a scalable Z as we did last Thursday (the pattern was E then).
Here's how it behaves then:
>>> m = Matrix(6)
>>> m
<__main__.Matrix object at 0x01370F80>
>>> m.report()
* * * * * *
*
*
*
*
* * * * * *
>>> m = Matrix(12)
>>> m
<__main__.Matrix object at 0x01379AB0>
>>> m.report()
* * * * * * * * * * * *
*
*
*
*
*
*
*
*
*
*
* * * * * * * * * * * *
>>> I think we owe it to ourselves to enhance this where it can be customized indefinitely:
class Matrix(object):
def __init__(self, size):
self.resize(size)
def resize(self, size):
a = " *" * size
m = []
for i in range(size):
m.append(a.split())
self.m = m
self.size = size
def setPattern(self, letter):
size = self.size
if letter == 'Z':
for i in range(size):
for j in range(size):
if i+j==size-1 or i==0 or i==size-1:
self.m[i][j] = '*'
else:
self.m[i][j] = ' '
elif letter == 'E':
for i in range(size):
for j in range(size):
if i==0 or i==size-1 or i == size/2 and jHere's how it works (the design is almost good):
>>> m = Matrix(3)
>>> m
<__main__.Matrix object at 0x00C02380>
>>> m.show()
* * *
* * *
* * *
>>> m.resize(7)
>>> m.show()
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
>>> m.setPattern("E")
>>> m.show()
* * * * * * *
*
*
* * *
*
*
* * * * * * *
>>> m.resize(5)
>>> m.show()
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
>>> m.setPattern("Z")
>>> m.show()
* * * * *
*
*
*
* * * * *
>>>