Quiz 12 code to test with:
from math import *
def distance(x1,y1,x2,y2):
d1=sqrt((x2-x1)**2 + (y2-y1)**2)
return d1
def process(xp,yp,mean1,mean2):
close1x=[]
close1y=[]
close2x=[]
close2y=[]
# Code goes here
return close1x,close1y,close2x,close2y
def centroid(listX,listY):
sX=0
sY=0
# find the centroid
return [float(sX)/len(listX),float(sY)/len(listY)] #return [x,y] of
#the averages of xSum, ySum
xp=[1,2,3,4,5,6,7,8]
yp=[.5,1,1.5,2,2.5,3,3.5,4]
mean1=[2,3]
mean2=[5,2]
close1x,close1y,close2x,close2y=process(xp,yp,mean1,mean2)
print "xp: %s, yp: %s,\nmean1: %s, mean2: %s" % (xp,yp,mean1,mean2)
print "x,y points:"
for i in range(len(xp)):
print "(%s,%s)" % (xp[i],yp[i])
print "close1x:%s, close1y:%s,\nclose2x:%s, close2y:%s" % (close1x,close1y,close2x,close2y)
print "x,y points closest to mean1:"
for i in range(len(close1x)):
print "(%s,%s)" % (close1x[i],close1y[i])
print "x,y points closest to mean2:"
for i in range(len(close2x)):
print "(%s,%s)" % (close2x[i],close2y[i])
print "Mean1: %s Centroid1: %s" % (mean1,centroid(close1x,close1y))
print "Mean2: %s Centroid2: %s" % (mean2,centroid(close2x,close2y))
Quiz 14 code to test with:
send your typed in .py file to rdlatimer@fcps.edu
def printBoard(board,rows,cols):
for r in range(rows):
for c in range(cols):
index = r*cols + c
print "%3d" % (board[index]),
print
def fillBoardNonRandom():
board=[1,1,1,1,2,0,2,0,
1,0,2,1,0,1,2,2,
2,2,1,2,1,0,0,2,
0,1,2,0,2,2,2,1,
2,2,2,1,1,2,1,2,
2,2,1,1,0,1,1,2,
2,2,2,2,1,1,2,2,
0,2,2,2,1,1,1,2]
return board
def getSegregatedCells(board,rows,cols): # returns a list of cells
segregated=[0] # having no neighbors of another type
unsegregated=[2]
# . . .
return segregated,unsegregated
rows=8
cols=8
board=fillBoardNonRandom()
printBoard(board,rows,cols)
segregated,unsegregated=getSegregatedCells(board,rows,cols)
print "Segregated cells: %s, unsegregated: %s" % (segregated,unsegregated)
print
for i in range(len(segregated)):
print "index %d: (%d,%d)" % (segregated[i],segregated[i]%cols,int(segregated[i]/cols))
proportion=float(len(segregated))/len(unsegregated)
print "Proportion of segregated cells: %.2f (%d/%d)" % (proportion,len(segregated),len(unsegregated))