วันเสาร์ที่ 31 ตุลาคม พ.ศ. 2558

Lab 7 - (Class) Insertion Sort Planning

def sort_insertion(my_list):
    i = 1
    while i < len(my_list):

        value_current = my_list[i]
        pos = i

        while((pos > 0) and (my_list[pos-1] > value_current)):
            my_list[pos] = my_list[pos-1]
            pos = pos-1
           
        if (pos != i):
            my_list[pos] = value_current
        i = i + 1
       
    return my_list
   

def setup():
   
    my_list = [15,26,28,17,77,31,44,55,20]
    print(my_list)
    print(sort_insertion(my_list))
   
setup()

Lab 7 - (Class) Display student records, sorted by age, use insertion sort

class student:

    def __init__(self,order,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
    def get_name(self):
        return self.name
    def get_weight(self):
        return self.weight
    def get_height(self):
        return self.height
       
   
    def set_age(self,value):
        self.age = value
        return self.age
    def set_name(self,value):
        self.name = value
        return self.name
    def set_weight(self,value):
        self.weight = value
        return self.weight  
    def set_height(self,value):
        self.height = value
        return self.height  
       
   
def setup():
    i = 0
    info = [student(1,"Buntun",18,54,160),
            student(2,"FlukeKnub",19,75,199),
            student(3,"PeeJa",32,60,150),
            student(4,"Nutdech",18,51,185),
            student(5,"BasBomba",20,65,180)]
    sort_insertion(info)
   
    while (i<len(info)):
        info[i].display()
        i = i+1
   
       
def sort_insertion(info):
    i = 1
    while i < len(info):

        value_current_age = info[i].get_age()
        value_current_name = info[i].get_name()
        value_current_weight = info[i].get_weight()
        value_current_height = info[i].get_height()
        pos = i

        while((pos > 0) and (info[pos-1].get_age() > value_current_age)):
            info[pos].set_age(info[pos-1].get_age())
            info[pos].set_name(info[pos-1].get_name())
            info[pos].set_weight(info[pos-1].get_weight())
            info[pos].set_height(info[pos-1].get_height())
            pos = pos-1
           
        if (pos != i):
            info[pos].set_age(value_current_age)
            info[pos].set_name(value_current_name)
            info[pos].set_weight(value_current_weight)
            info[pos].set_height(value_current_height)
           
        i = i + 1
       
    return info
   
setup()

Lab 7 - (Class) Find minimum weight of students

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,54,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,51,185),
            student("BasBomba",20,65,180)]
    find_weight(info)
       
def find_weight(info):
    min = 0
    i = 1
    count = 0
    while (i<len(info)):
        weight_min = info[min].get_weight()
        weight = info[i].get_weight()
        if (weight_min > weight):
           weight_min = weight
           count = i
        i = i+1
    info[count].display()
   
setup()

Lab 7 - (Class) Display student records with weight < 50

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,43,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_weight(info)
       
def find_weight(info):
    i = 0
    count = 0
    while (i<len(info)):
        weight = info[i].get_weight()
        if (weight < 50):
           info[i].display()
        i = i+1
    print()
   
setup()

Lab 7 - (Class) Find/count number of students with weight < 50

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
   
def setup():
    info = [student("Buntun",18,43,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_weight(info)
       
def find_weight(info):
    i = 0
    count = 0
    while (i<len(info)):
        weight = info[i].get_weight()
        if (weight < 50):
           info[i].display()
           count = count+1
        i = i+1
    print()
    print ("Number member has weight less than 50 =",count)
   
setup()

Lab 7 - (Class) Find/count number of students with age < 30

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",32,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    find_age(info)
       
def find_age(info):
    i = 0
    count = 0
    while (i<len(info)):
        age = info[i].get_age()
        if (age < 30):
           info[i].display()
           count = count+1
        i = i+1
    print()
    print ("Number member has age less than 30 =",count)
   
setup()

Lab 7 - (Class) Find average age of students

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_age(self):
        return self.age
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    average_age(info)
    print ("Summary age =", average_age(info))
   
def average_age(info):
    i = 0
    sum_age = 0
    while(i<len(info)):
       sum_age = sum_age+info[i].get_age()
       i = i+1
    average = sum_age/len(info)
    return average
   
setup()

Lab 7 - (Class) Display student records with BMI > 25

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
    i = 0
    sum = 0
    while(i<len(info)):
        bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
        if ( bmi > 25):
            info[i].display()
            print(bmi)
            print()
        i = i+1
   
setup()

Lab 7 - (Class) Find/count number of students with BMI > 25

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,150),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
    i = 0
    sum = 0
    while(i<len(info)):
        bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
        if ( bmi > 25):
            info[i].display()
            print(bmi)
            print()
            sum = sum+1
        i = i+1
    print ("Number member who has bmi more than 25 is",sum )

setup()

Lab 7 - (Class) Find student BMI while version

class student:

    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height

    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
         
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
   
def setup():
    info = [student("Buntun",18,50,160),
            student("FlukeKnub",19,75,199),
            student("PeeJa",19,60,160),
            student("Nutdech",18,58,185),
            student("BasBomba",20,60,180)]
    cal_bmi(info)
   
def cal_bmi(info):
   i=0
   while(i<len(info)):
      bmi = info[i].get_weight()/((info[i].get_height()/100)*(info[i].get_height()/100))
      info[i].display()
      print("BMI is", bmi)
      print()
      i=i+1

setup()

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

Lab 7 - (Class) Find student BMI

class student:
 
    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height
 
    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
     
    def get_weight(self):
        return self.weight
     
    def get_height(self):
        return self.height
     
def setup():
    a = student("Buntun",18,50,160)
    b = student("FlukeKnub",19,75,199)
    c = student("PeeJa",19,60,160)
    d = student("Nutdech",18,58,185)
    e = student("BasBomba",20,60,180)
 
    a.display(),print(cal_bmi(a))
    print()
    b.display(),print(cal_bmi(b))
    print()
    c.display(),print(cal_bmi(c))
    print()
    d.display(),print(cal_bmi(d))
    print()
    e.display(),print(cal_bmi(e))
    print()
 
   
def cal_bmi(student):
    bmi = student.get_weight()/((student.get_height()/100)*(student.get_height()/100))
    return bmi
 
setup()
 

Lab 7 - (Class) Display each student record

class student:
 
    def __init__(self,name,age,weight,height):
        self.name = name
        self.age = age
        self.weight = weight
        self.height = height
 
    def display(self):
        print(self.name, end = "   ")
        print(self.age, end = "   ")
        print(self.weight, end = "   ")
        print(self.height, end = "   ")
        print()
     
def setup():
    a = student("Buntun",18,50,160)
    b = student("FlukeKnub",19,75,199)
    c = student("PeeJa",19,60,160)
    d = student("Nutdech",18,58,185)
    e = student("BasBomba",20,60,180)
 
    a.display()
    b.display()
    c.display()
    d.display()
 
setup()
 

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()

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

Lab 5 - (Function) my_endswith()

def my_endswith(word,check):
   i = len(word)-len(check)
   a = 0
   if(len(word)>=len(check)):
      while(i<len(word)):
         if(word[i] == check[a]):
            i = i + 1
            a = a + 1
         else:
            return False
      return True

my_endswith("thailand","land")
assert my_endswith("thailand","land")
assert my_endswith("thailand","thai") == False



Lab 5 - (Function) my_startswith ()

def my_startswith(word,check):
   i = 0
   while(i<len(check)):
       if(word[i] == check[i]):
           i = i + 1
       else:
           return False
   return True

my_startswith("Thailand","That")
assert my_startswith("Thailand","That")
assert my_startswith("Thailand","That") == False


Lab 5 - (Function) my_strip()

def my_replace(word):
   result=""
   i = 0
   while(i<len(word)):
        if(word[i] != " "):
           result = result + word[i]
        i = i+1
   return result
     
my_replace("Thailand    ")
assert (my_replace("Thailand    ") == "Thailand" )
print (my_replace("Thailand    "))


Lab 5 - (Function) my_find()

def my_find(word,charac):
   i = 0
   x = 0

   while(i<len(word)):
        if(charac == word[i]):
           x = i
           print(x)
           break
        i = i+1
       
my_find("Thailandi","i")


Lab 5 - (Function) my_count()

def my_count(word,check):
    i = 0
    count = 0
    while (i<len(word)):
        if (word[i] == check):
            count = count + 1
        i = i + 1
    print (count)
 
my_count("thailand","a")