def setup():
m1=[[1,2,3],[4,5,6]]
m2=[[4,2,6],[14,19,15]]
SubtractMatrix(m1,m2)
MultipleMatrix(m1,m2)
def SubtractMatrix(a,b):
print('SubtractMatrix')
i=0
while(i<len(a)):
j=0
while(j<len(a[i])):
print(a[i][j]-b[i][j]," ",end = "")
j=j+1
i=i+1
print("")
def MultipleMatrix(a,b):
print('MultipleMatrix')
i=0
while(i<len(a)):
j=0
while(j<len(a[i])):
print(a[i][j]*b[i][j]," ",end = "")
j=j+1
i=i+1
print("")
setup()
วันอาทิตย์ที่ 1 พฤศจิกายน พ.ศ. 2558
Lab6_Matrix_showAndAdd
def setup():
m1=[[1,2,3],[4,5,6]]
m2=[[7,8,9],[10,11,12]]
addMatrix(m1,m2)
def showMatrix(a):
i=0
while(i<len(a)):
print(a[i])
i=i+1
def addMatrix(a,b):
i=0
while(i<len(a)):
j=0
while(j<len(a[i])):
print(a[i][j]+b[i][j]," ",end = "")
j=j+1
i=i+1
print("")
setup()
m1=[[1,2,3],[4,5,6]]
m2=[[7,8,9],[10,11,12]]
addMatrix(m1,m2)
def showMatrix(a):
i=0
while(i<len(a)):
print(a[i])
i=i+1
def addMatrix(a,b):
i=0
while(i<len(a)):
j=0
while(j<len(a[i])):
print(a[i][j]+b[i][j]," ",end = "")
j=j+1
i=i+1
print("")
setup()
Lab6_Find_maximum_chair_index
def setup():
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,48,24]#building[1][0-3]
floor3=[48,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
findRoomWithMaxChair(building)
def findRoomWithMaxChair(a):
i=0
j=0
maximum=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum<a[j][i]):
maximum=a[j][i]
i=i+1
i=0
j=j+1
i=0
j=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum == a[j][i]):
print('floor index:',j,',room index:',i)
i=i+1
i=0
j=j+1
setup()
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,48,24]#building[1][0-3]
floor3=[48,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
findRoomWithMaxChair(building)
def findRoomWithMaxChair(a):
i=0
j=0
maximum=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum<a[j][i]):
maximum=a[j][i]
i=i+1
i=0
j=j+1
i=0
j=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum == a[j][i]):
print('floor index:',j,',room index:',i)
i=i+1
i=0
j=j+1
setup()
Lab6_findIndexofMaximumChair
def setup():
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,28,24]#building[1][0-3]
floor3=[48,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
print('maximum chairs index is ',maxFloor(building))
def maxFloor(a):
i=0#chair
j=0#floor
j2=0
maximum=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum<a[j][i]):
maximum=a[j][i]
j2=j
i=i+1
i=0
j=j+1
return j2
setup()
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,28,24]#building[1][0-3]
floor3=[48,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
print('maximum chairs index is ',maxFloor(building))
def maxFloor(a):
i=0#chair
j=0#floor
j2=0
maximum=0
while(j<len(a)):
while(i<len(a[j])):
if(maximum<a[j][i]):
maximum=a[j][i]
j2=j
i=i+1
i=0
j=j+1
return j2
setup()
Lab6_FindChairsInBuilding
def setup():
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,28,24]#building[1][0-3]
floor3=[24,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
print('there are ',sumChair(building),' chairs in the building')
def sumChair(a):
i=0#chair
j=0#floor
sums=0
while(j<len(a)):
while(i<len(a[j])):
sums=sums+a[j][i]
i=i+1
i=0
j=j+1
return sums
setup()
floor1=[14,25,18,38]#building[0][0-3]
floor2=[34,21,28,24]#building[1][0-3]
floor3=[24,29,12,48]#building[2][0-3]
building = [floor1,floor2,floor3]
print('there are ',sumChair(building),' chairs in the building')
def sumChair(a):
i=0#chair
j=0#floor
sums=0
while(j<len(a)):
while(i<len(a[j])):
sums=sums+a[j][i]
i=i+1
i=0
j=j+1
return sums
setup()
Lab6_DataAndWeight
def setup():
names=["Jack",'John','James','Jane']
idNo=['5801','5802','5803','5804']
age=[18,32,15,41]
weight=[40,69,72,49]
height=[150,162,170,159]
listthedata(names,idNo,age,weight,height)
print('the minimum weight is ',findminW(names,weight))
print('there are ',findMinWeiNumber(names,weight),'students < 50')
def listthedata(names,idNo,age,weight,height):
i=0
while(i<len(names)):
if(weight[i]<50):
print('Name : ',names[i],' ID NO.: ',idNo[i],' Age: ',age[i])
print('Weight: ',weight[i],' Height: ',height[i],' BMI: %.2f'%cal_bmi(i,weight,height))
print()
i=i+1
def cal_bmi(i,weight,height):
bmi=(weight[i])/((height[i]/100)**2)
return bmi
def findminW(names,weight):
i=0
minimum=weight[0]
while(i<len(weight)):
if(minimum>weight[i]):
minimum=weight[i]
i=i+1
return minimum
def findMinWeiNumber(names,weight):
i=0
sums=0
while(i<len(names)):
if(weight[i]<50):
sums=sums+1
i=i+1
return sums
setup()
names=["Jack",'John','James','Jane']
idNo=['5801','5802','5803','5804']
age=[18,32,15,41]
weight=[40,69,72,49]
height=[150,162,170,159]
listthedata(names,idNo,age,weight,height)
print('the minimum weight is ',findminW(names,weight))
print('there are ',findMinWeiNumber(names,weight),'students < 50')
def listthedata(names,idNo,age,weight,height):
i=0
while(i<len(names)):
if(weight[i]<50):
print('Name : ',names[i],' ID NO.: ',idNo[i],' Age: ',age[i])
print('Weight: ',weight[i],' Height: ',height[i],' BMI: %.2f'%cal_bmi(i,weight,height))
print()
i=i+1
def cal_bmi(i,weight,height):
bmi=(weight[i])/((height[i]/100)**2)
return bmi
def findminW(names,weight):
i=0
minimum=weight[0]
while(i<len(weight)):
if(minimum>weight[i]):
minimum=weight[i]
i=i+1
return minimum
def findMinWeiNumber(names,weight):
i=0
sums=0
while(i<len(names)):
if(weight[i]<50):
sums=sums+1
i=i+1
return sums
setup()
Lab6_DataAndAge
def setup():
names=["Jack",'John','James','Jane']
idNo=['5801','5802','5803','5804']
age=[18,32,15,41]
weight=[60,99,72,50]
height=[150,162,170,159]
listthedata(names,idNo,age,weight,height)
print(lessage30(names,age),'student < 30')
print('the avg age is',find_avgAge(names,age))
sortByAge(names,age)
def listthedata(names,idNo,age,weight,height):
i=0
while(i<len(names)):
print('Name : ',names[i],' ID NO.: ',idNo[i],' Age: ',age[i])
print('Weight: ',weight[i],' Height: ',height[i],' BMI: %.2f'%cal_bmi(i,weight,height))
print()
i=i+1
def sortByAge(names,age):
j=1
while(j<len(age)):
n=age[j]
i=j-1
while(i >= 0) and (age[i] > n):
age[i+1]=age[i]
i=i-1
age[i+1] = n
j=j+1
print(age)
def cal_bmi(i,weight,height):
bmi=(weight[i])/((height[i]/100)**2)
return bmi
def lessage30(names,age):
i=0
sums=0
while(i<len(names)):
if(age[i]<30):
sums=sums+1
i=i+1
return sums
def find_avgAge(names,age):
i=0
sums=0
while(i<len(names)):
sums=sums+age[i]
i=i+1
avg=sums/len(names)
return avg
setup()
names=["Jack",'John','James','Jane']
idNo=['5801','5802','5803','5804']
age=[18,32,15,41]
weight=[60,99,72,50]
height=[150,162,170,159]
listthedata(names,idNo,age,weight,height)
print(lessage30(names,age),'student < 30')
print('the avg age is',find_avgAge(names,age))
sortByAge(names,age)
def listthedata(names,idNo,age,weight,height):
i=0
while(i<len(names)):
print('Name : ',names[i],' ID NO.: ',idNo[i],' Age: ',age[i])
print('Weight: ',weight[i],' Height: ',height[i],' BMI: %.2f'%cal_bmi(i,weight,height))
print()
i=i+1
def sortByAge(names,age):
j=1
while(j<len(age)):
n=age[j]
i=j-1
while(i >= 0) and (age[i] > n):
age[i+1]=age[i]
i=i-1
age[i+1] = n
j=j+1
print(age)
def cal_bmi(i,weight,height):
bmi=(weight[i])/((height[i]/100)**2)
return bmi
def lessage30(names,age):
i=0
sums=0
while(i<len(names)):
if(age[i]<30):
sums=sums+1
i=i+1
return sums
def find_avgAge(names,age):
i=0
sums=0
while(i<len(names)):
sums=sums+age[i]
i=i+1
avg=sums/len(names)
return avg
setup()
สมัครสมาชิก:
บทความ (Atom)