วันอาทิตย์ที่ 25 ตุลาคม พ.ศ. 2558

Lab 6 : Multiply two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   multiply_matrix(matrix_1,matrix_2)
 
def multiply_matrix(matrix_1,matrix_2):
   i = 0
   i_i = 0
   k_i = 0
   while(i_i<len(matrix_1)):
      j = 0
      i_j = 0
      k_j = 0
      print("|",end="")
      while(i_j<len(matrix_2[i])):
         total_1=matrix_1[i+k_i][j]*matrix_2[i][j+k_j]
         total_2=matrix_1[i+k_i][j+1]*matrix_2[i+1][j+k_j]
         total_3=matrix_1[i+k_i][j+2]*matrix_2[i+2][j+k_j]
         print("",total_1+total_2+total_3,end="")
         k_j = k_j+1
         i_j = i_j+1
      print("|")
      k_i = k_i+1
      i_i = i_i+1
     
setup()

Lab 6 : Subtract two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]-matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Add two matrices

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   row_4 = [3,6,9]
   row_5 = [2,5,8]
   row_6 = [1,4,7]
   matrix_2 = [row_4,row_5,row_6]
   add_2(matrix_1,matrix_2)
 
def add_2(matrix_1,matrix_2):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j]+matrix_2[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Display the matrix

def setup():
   row_1 = [1,2,3]
   row_2 = [4,5,6]
   row_3 = [7,8,9]
   matrix_1 = [row_1,row_2,row_3]
   display_matrix(matrix_1)
 
def display_matrix(matrix_1):
   i = 0
   while(i<len(matrix_1)):
      j = 0
      print("|",end="")
      while(j<len(matrix_1[i])):
         print("",matrix_1[i][j],"",end="")
         j = j+1
      print("|")
      i = i+1
     
setup()

Lab 6 : Find indices of the room(s) with maximum number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   num = building_1[0][0]
   for i in range(0, len(building_1)):
      i_room = 0
      for i in range(0,len(building_1[i_floor])):
         if(building_1[i_floor][i_room] > num):
            num = building_1[i_floor][i_room]
         i_room = i_room+1
      i_floor = i_floor+1
   i_floor = 0
   for i in range(i_floor,len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         if(num==building_1[i_floor][i_room]):
            print("indices of the room(s) with maximum number of chairs :","[",i_floor,"][",i_room,"]")
         i_room = i_room+1
      i_floor = i_floor+1
     
setup()

Lab 6 : Find index of the floor(s) with maximum number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   total = 0
   num = 0
   i_max = 0
   for i in range(i_floor, len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         total = total+building_1[i_floor][i_room]
         i_room = i_room+1
      if(total>num):
         num = total
         i_max = i_floor
      i_floor = i_floor+1
   print("index of the floor(s) with maximum number of chairs :",i_max)
 
setup()

Lab 6 : Find total number of chairs (in the building)

def setup():
   floor_1 = [12,25,36,42,18]
   floor_2 = [21,36,41,17,22]
   floor_3 = [45,23,39,51,18]
   building_1 = [floor_1,floor_2,floor_3]
   find_chair(building_1)
 
def find_chair(building_1):
   i_floor = 0
   total = 0
   for i in range(i_floor, len(building_1)):
      i_room = 0
      for i in range(i_room,len(building_1[i_floor])):
         total = total+building_1[i_floor][i_room]
         i_room = i_room+1
      i_floor = i_floor+1
   print("total number of chairs (in the building) :",total)
 
setup()

Lab 6 : Find/count number of students with weight < 50

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with BMI < 50 :",count_weight(weights))

def count_weight(weight):
   i = 0
   num = 0
   for i in range(0, len(weight)):
      if(weight[i]<50):
         num = num+1
   return num

setup()

Lab 6 : Find minimum weight of students

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("minimum weight of students :",min(weights))
 
setup()

lab 6 : Find/count number of students with age < 30

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with age < 30 :",count_age(ages))

def count_age(age):
   i = 0
   num = 0
   for i in range(0, len(age)):
      if(age[i]<30):
         num = num+1
   return num

setup()

lab 6 : Find average age of students

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("average age of students :",find_average(ages))

def find_average(age):
   i = 0
   sum = 0
   for i in range(0, len(age)):
      sum = sum + age[i]
      aver = sum/len(age)
   return aver

setup()

Lab 6 : Display student records with BMI > 25

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   find_bmi(names,weights,heights)

def find_bmi(name,weight,height):
   i = 0
   num = 0
   for i in range(0, len(name)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      if(bmi>25):
         print("Name",name[i])
         print("BMI =",bmi)

setup()

Lab 6 : Find/count number of students with BMI > 25

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   print("number of students with BMI > 25 :",count_bmi(weights,heights))

def count_bmi(weight,height):
   i = 0
   num = 0
   for i in range(0, len(weight)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      if(bmi>25):
         num = num+1
   return num

setup()

Lab 6 : Find student BMI

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   find_bmi(names,weights,heights)

def find_bmi(name,weight,height):
   i = 0
   for i in range(0, len(name)):
      Hm=height[i]/100
      bmi=weight[i]/(Hm*Hm)
      print("*****Number",i,"*****")
      print("Name",name[i])
      print("BMI =",bmi)

setup()

Lab 6 : Display student records (data)

def setup():
   names=["a","b","c"]
   IDs=[123,456,789]
   ages=[20,25,30]
   weights=[50,67,71]
   heights=[164,175,151]
   data(names,IDs,ages,weights,heights)

def data(name,ID,age,weight,height):
   i = 0
   for i in range(0, len(name)):
      print("*****Number",i,"*****")
      print("Name",name[i])
      print("ID",ID[i])
      print("Age",age[i])
      print("Weight",weight[i])
      print("Height",height[i])
   return data

setup()

lab 5 : Increase/decrease values in array (by fixed value or percentage)

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i1 = 0
   i2 = 0
   while(i1 < len(n)):
      #sum = sum + n[ i ]
      print("n[",i1,"] =", n[i1])
      i1 = i1 + 1
   while(i2 < len(n)):
      new_n = input("Put new value =")
      print("n[",i2,"] = ",new_n)
      i2 = i2 + 1

setup()

Lab 5 : Find average of values in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      sum = sum + n[ i ]
      i = i + 1
   aver = sum/len(n)
   print("average =", aver)

setup()

Lab 5 : Find/count number of positive values in array

def setup():
   n = [ -1, 8, -7, 12, 3, -6, 9 ]
   i = 0
   num = 0
   while(i < len(n)):
      if(n[i]>=0):
         num = num + 1
      i = i + 1
   print("number of positive values  =", num)

setup()

Lab 5 : Find sum of positive values in array

def setup():
   n = [ 1, 8, 7, 12, 3, -6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      if(n[i]>=0):
         sum = sum + n[ i ]
      i = i + 1
   print("sum =", sum)

setup()

Lab 5 : Find sum of values in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   sum = 0
   while(i < len(n)):
      sum = sum + n[ i ]
      i = i + 1
   print("sum =", sum)

setup()

Lab 5 : Find index of (the last) maximum value in array

def setup():
   n = [ 1, 9, 12, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   indexMax = 0
   n.reverse()
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
         indexMax = i
      i = i+1
   print("(Max)Index position from last :",indexMax)

setup()

Lab 5 : Find index of (the first) maximum value in array

def setup():
   n = [ 1, 9, 12, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   indexMax = 0
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
         indexMax = i
      i = i+1
   print("(Max)Index position from first :",indexMax)

setup()

Lab 5: Find the maximum value in array

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   numMax = 0
   while(i<len(n)):
      if(numMax<n[i]):
         numMax = n[i]
      i = i+1
   print("Maximum :",numMax)

setup()

Lab 5 : Display elements (value) of array and its index

def setup():
   n = [ 1, 8, 7, 12, 3, 6, 9 ]
   i = 0
   while(i < len(n)):
      print("n[",i,"] =", n[i])
      i = i + 1

setup()