แสดงบทความที่มีป้ายกำกับ Lab 6 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Lab 6 แสดงบทความทั้งหมด

วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2558

Lab 6 - Multiply two matrices

def setup():
   row_11=[1,2]
   row_12=[3,4]
   matrix_1=[row_11,row_12]
   row_21=[5,6]
   row_22=[7,8]
   matrix_2=[row_21,row_22]
   add_matrix(matrix_1,matrix_2)
 
def add_matrix(matrix_1,matrix_2):
 
   i=0
   j=0
   k=0
 
   add1=0
   add2=0
   add=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         add1 = matrix_1[i][k]*matrix_2[k][j]
         add2 = matrix_1[i][k+1]*matrix_2[k+1][j]
         add = add1+add2
         print(add,end=" ")
         j = j+1
      j = 0
      print("|")
      i = i+1
     
setup()

Lab 6 - Write a function that takes a string as an argument and prints out the string in large letters

def setup():
   word=input()
   display(word)

def display(word):
   j1="#######"
   j2="   #   "
   j3="#  #   "
   j4="  #    "
   j=[j1,j2,j3,j4]
   a1="  # #  "
   a2=" #   # "
   a3="#######"
   a4="#     #"
   a=[a1,a2,a3,a4]
   v1="#     #"
   v2=" #   # "
   v3="  # #  "
   v4="   #   "
   v=[v1,v2,v3,v4]
   i=0
   j=0
   while (i < len(n)): // Check by side
      while (j < len(word)): //  Check by column
         if (word[j] == 'j'):
            print(n[i] , end=" ")
         if(word[j]=='a'):
            print(u[i] , end=" ")
         if (word[j]=='v'):
            print(t[i] , end=" ")
         j=j+1
      j=0
      print()
      i=i+1

setup()

Lab 6 - Transpose a square matrix in place without creating a new matrix.

def setup():
   row_11 = [44,10]
   row_12 = [32,21]
   matrix = [row_11,row_12]
   transpose_matrix(matrix)
 
def transpose_matrix(matrix):
   
   i=0
   j=0
   while(i<len(matrix)):
      print("|",end=" ")
      while(j<len(matrix[i])):
         print(matrix[j][i],end=" ")
         j = j+1
      j = 0
      print("|")
      i = i+1
     
setup()

Lab 6 - Subtract two matrices

def setup():
   row_11=[44,10]
   row_12=[32,21]
   matrix_1=[row_11,row_12]
   row_21=[0,0]
   row_22=[3,33]
   matrix_2=[row_21,row_22]
   new_matrix(matrix_1,matrix_2)
 
def new_matrix(matrix_1,matrix_2):
   i=0
   j=0
   result=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         result = matrix_1[i][j]-matrix_2[i][j]
         print(result ,end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
     
setup()

Lab 6 - Add two matrices

def setup():
   row_11=[44,10]
   row_12=[32,21]
   matrix_1=[row_11,row_12]
   row_21=[0,0]
   row_22=[3,33]
   matrix_2=[row_21,row_22]
   new_matrix(matrix_1,matrix_2)
 
def new_matrix(matrix_1,matrix_2):
   i=0
   j=0
   result=0
 
   while(i<len(matrix_1)):
      print("|",end=" ")
      while(j<len(matrix_1)):
         result = matrix_1[i][j]+matrix_2[i][j]
         print(result ,end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
     
setup()

Lab 6 - Display the matrix

def setup():
   row_11=[1,5]
   row_12=[3,0]
   matrix_1=[row_11,row_12]
   display_matrix(matrix_1)
 
def display_matrix(matrix):
   i=0
   j=0
   while(i<len(matrix)):
      print("|",end=" ")
      while(j<len(matrix[i])):
         print(matrix[i][j],end=" ")
         j=j+1
      j=0
      print("|")
      i=i+1
setup()

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

def setup():
   floor_1=[30,31,33,20,34]
   floor_2=[50,33,23,45,53]
   floor_3=[65,34,21,34,14]
   building=[floor_1,floor_2,floor_3]
   find_max_chairs(building)
   print("Floor have the most of chairs = ",find_max_chairs(building))
 
def find_max_chairs(building):
   i=0
   j=0
   maximum_chair=0
   total=0
   floor=0
   while(i<len(building)):
      while(j<len(building[i])):
         total=total+building[i][j]
         j=j+1
      j=0
      if(total>maximum_chair):
         maximum_chair=total
         floor=i+1
      total=0
      i=i+1
   return floor
 
setup()

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

def setup():
   floor_1=[30,31,33,20,34]
   floor_2=[50,33,23,45,53]
   floor_3=[65,34,21,34,14]
   building=[floor_1,floor_2,floor_3]
   find_total(building)
   print("total of chairs = ",find_total(building))
 
def find_total(building):
   i=0
   j=0
   total=0
   while(i<len(building)):
      while(j<len(building[i])):
         total=total+building[i][j]
         j=j+1
      j=0
      i=i+1
   return total
 
setup()

Lab 6 - Find minimum weight, weight < 50, Display records

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,23,26,21]
   weight=[43,55,60,54,63]
   height=[175,171,187,181,183]
   find_weight(student_id,names,age,weight,height)
 
def find_weight(student_id,names,age,weight,height):
   i=0
   minimun_weight=weight[1]
   count=0
   while(i<len(weight)):
      if(weight[i]<minimun_weight):
         minimun_weight=weight[i]
      if(weight[i]<50):
         count=count+1
         print(student_id[i],"  ",names[i],"       ",weight[i],"  kg")
      i=i+1
   print("student have weight less than 50 = ",count)
 
setup()

Lab 6 - Display student records, sorted by age, use insertion sort

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,23,26,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   sort_age(student_id,names,age,weight,height)
   display(student_id,names,age,weight,height)

def sort_age(student_id,names,age,weight,height):
    i=1
    while(i<len(student_id)):
      new_s=student_id[i]
      new_n=names[i]
      new_a=age[i]
      new_w=weight[i]
      new_h=height[i]
      j=i
      while(j>0 and age[j-1]>new_a):
         student_id[j]=student_id[j-1]
         names[j]=names[j-1]
         age[j]=age[j-1]
         weight[j]=weight[j-1]
         height[j]=height[j-1]
         j=j-1
      student_id[j]=new_s
      names[j]=new_n
      age[j]=new_a
      weight[j]=new_w
      height[j]=new_h
      i=i+1
     
def display(student_id,names,age,weight,height):
    i=0
    print("ID  ","names     ","age    ","weight  ","height")
    while(i<len(student_id)):
        print(student_id[i],"  ",names[i],"    ",age[i],"       ",weight[i],"       ",height[i])
        print()
        i=i+1

setup()

Lab 6 - Find average age of students

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_average_age(student_id,names,age,weight,height)

def find_average_age(student_id,names,age,weight,height):
    i=0
    summation=0
   
    while(i<len(student_id)):
      summation=summation+age[i]
      i=i+1
    print("Average Age = ",summation/len(student_id) )
    return

setup()

Lab 6 - Find/count number of students with age < 30

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_age(student_id,names,age,weight,height)

def find_age(student_id,names,age,weight,height):
    i=0
    count=0
    while(i<len(student_id)):
      if(age[i]<30):
         print(student_id[i],"   ",names[i],"     ",age[i],"   year old")
         count=count+1
      i=i+1
    print("number student have age less than 30 = ",count)  

setup()

Lab 6 - Find student BMI

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   cal_bmi(student_id,names,age,weight,height)

def cal_bmi(student_id,names,age,weight,height):
    i=0
    bmi=0
    count=0
    while(i<len(student_id)):
      bmi=weight[i]/((height[i]/100)*2)
      print(student_id[i],"  ",names[i],"   ",bmi)
      i=i+1
setup()

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

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   find_bmi(student_id,names,age,weight,height)

def find_bmi(student_id,names,age,weight,height):
    i=0
    bmi=0
    count=0
    while(i<len(student_id)):
      bmi=weight[i]/((height[i]/100)*2)
      if(bmi>25):
         print(student_id[i],"  ",names[i],"   ",bmi)
         count=count+1
      i=i+1
    print("number student have BMI more than 25 = ",count)
setup()

Lab 6 - Display student records (data)

def setup():
   student_id=[1,2,3,4,5]
   names=["A","B","C","D","E"]
   age=[15,16,20,21,21]
   weight=[51,55,60,54,63]
   height=[175,171,187,181,183]
   display(student_id,names,age,weight,height)
 
def display(student_id,names,age,weight,height):
   i=0  
   print("ID     ","names     ","age     ","weight     ","height")
   while(i<len(student_id)):
      print(student_id[i],"        ",names[i],"        ",age[i],"        ",weight[i],"          ",height[i])
      print()
      i=i+1
setup()