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

Lab7-student

class student:
   def __init__(self,name,ID,age,weight,height):
      self.name = name
      self.ID = ID
      self.age = age
      self.weight = weight
      self.height = height
   
   def get_weight(self):
      return self.weight

   def get_height(self):
      return self.height

   def display(self):
      print('name:  ',self.name)
      print('ID:    ',self.ID)
      print('age:   ',self.age)
      print('weight:',self.weight)
      print('height:',self.height)


def setup():
   S =[student('jinx',58102,18,44,164),
       student('Vi',58201,19,65,169),
       student('Kat',58101,20,70,160)]
   i = 0
   while(i<len(S)):
      S[i].display()
      print('BMI:',BMI(S,i))
      print()
      i+=1

   print('There is',count_over_25_BMI(S),'student who got BMI over 25')
   find_over_25_BMI(S)

   print('There is',count_weight_less_than_50(S),'student who got weight less than 50')
   find_weight_less_than_50(S)

   print('minimum weight of students is',find_minimum_weight(S))
 
def BMI(S,i):
   BMI = S[i].get_weight()/(S[i].get_height()/100)**2
   return BMI

def count_over_25_BMI(array):
   i = 0
   count = 0
   while(i<len(array)):
      if(BMI(array,i)>25):
         count+=1
      i+=1
   return count

def find_over_25_BMI(array):
   i = 0
   print("Let's have a look who got BMI over 25")
   while(i<len(array)):
      if(BMI(array,i)>25):
         array[i].display()
         print()
      i+=1

def count_weight_less_than_50(array):
   i = 0
   count = 0
   while(i<len(array)):
      if(array[i].weight<50):
         count+=1
      i+=1
   return count

def find_weight_less_than_50(array):
   i = 0
   print("Let's have a look who got weight less than 50")
   while(i<len(array)):
      if(array[i].get_weight()<50):
         array[i].display()
      i+=1  

def find_minimum_weight(array):
   i = 0
   minimun_weight = array[i].get_weight()
   while(i<len(array)):
      if(minimun_weight>array[i].get_weight()):
         minimun_weight = array[i].get_weight()
      i = i+1
   return minimun_weight    
setup()

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

Lab6-age

def setup():
   name = ['jinx','Vi','Kat','Zed']
   ID = [58102,58201,58101,58202]
   age = [18,19,17,16]
   weight = [44,65,54,70]
   height = [164,169,162,180]
   i = 0

   sort(name,ID,age,weight,height)
   while(i<len(name)):
      print('name :',name[i])
      print('ID :', ID[i])
      print('age :',age[i])
      print('weight :',weight[i])
      print('height :',height[i])
      print('BMI :',BMI(weight[i],height[i]))
      print('\n')
      i+=1
   print('The average age of those student is' ,find_average_age(age))



def BMI(weight,height):
   BMI = weight/((height/100)**2)
   return BMI

def find_average_age(age):
   i=0
   sum_age = 0
   while(i<len(age)):
      sum_age+=age[i]
      i+=1
   average_age = sum_age/len(age)
   return average_age

def sort(name,ID,age,weight,height):
   i = 1
   while(i<len(age)):
      data_name = name[i]
      data_ID = ID[i]
      data_age = age[i]
      data_weight = weight[i]
      data_height = height[i]
      j = i
      while(j>0 and age[j-1]>data_age):
         name[j] = name[j-1]
         ID[j] = ID[j-1]
         age[j] = age[j-1]
         weight[j] = weight[j-1]
         height[j] = height[j-1]
         j-=1
       
      name[j] = data_name
      ID[j] = data_ID
      age[j] = data_age
      weight[j] = data_weight
      height[j] = data_height
      i+=1  



setup()

Result
name : Zed
ID : 58202
age : 16
weight : 70
height : 180
BMI : 21.604938271604937


name : Kat
ID : 58101
age : 17
weight : 54
height : 162
BMI : 20.576131687242793


name : jinx
ID : 58102
age : 18
weight : 44
height : 164
BMI : 16.359309934562763


name : Vi
ID : 58201
age : 19
weight : 65
height : 169
BMI : 22.758306781975424


The average age of those student is 17.5

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

Lab6-matrix Display, Add , Subtract two matrices

def setup():
   matrix_1 = [[6,9],[1,3]]
   matrix_2 = [[2,4],[1,7]]
   display(matrix_1)
   display(matrix_2)
   print('^//////^')
   display(add_matrix(matrix_1,matrix_2))
   print('^//////^')
   display(Subtract_matrix(matrix_1,matrix_2))

def display(matrix):
   print(matrix)

def make_matrix(matrix):
   i = 0
   j = 0
   result_matrix = zero_matrix(matrix)
   count = 0
   while(count < len(matrix)):
      result_matrix[count] = zero_matrix(matrix[i])
      count += 1
   while(i<len(matrix)):
      while(j<len(matrix[i])):
         result_matrix[i][j] = matrix[i][j]
         j+=1
      j = 0
      i+=1
   return result_matrix  
def zero_matrix(matrix):
   i = 0
   zero_matrix = []
   while(i<len(matrix)):
      zero_matrix += ['0']
      i+=1
   return zero_matrix

def add_matrix(matrix_1,matrix_2):
   i = 0
   j = 0
   result_matrix = zero_matrix(matrix_1)
   count = 0
   while(count < len(matrix_1)):
      result_matrix[count] = zero_matrix(matrix_1[i])
      count += 1
   while(i<len(matrix_1)):
      while(j<len(matrix_1[i])):
         result_matrix[i][j] = matrix_1[i][j] + matrix_2[i][j]
         j+=1
      j = 0
      i+=1
   return result_matrix

def Subtract_matrix(matrix_1,matrix_2):
   i = 0
   j = 0
   result_matrix = zero_matrix(matrix_1)
   count = 0
   while(count < len(matrix_1)):
      result_matrix[count] = zero_matrix(matrix_1[i])
      count += 1
   while(i<len(matrix_1)):
      while(j<len(matrix_1[i])):
         result_matrix[i][j] = matrix_1[i][j] - matrix_2[i][j]
         j+=1
      j = 0
      i+=1
   return result_matrix

setup()

Result
[[6, 9], [1, 3]]
[[2, 4], [1, 7]]
^//////^
[[8, 13], [2, 10]]
^//////^
[[4, 5], [0, -4]]

Lab6-word

def setup():
   set_word('FOX')

def set_word(word):
   F1="# # # # #"
   F2="#"
   F3="# # # #"
   F4="#"
   F5="#"
   F6="#"
   F = [F1,F2,F3,F4,F5,F6]
   O1=" # # # # "
   O2="#       #"
   O3="#       #"
   O4="#       #"
   O5="#       #"
   O6=" # # # # "
   O=[O1,O2,O3,O4,O5,O6]
   X1="#       #"
   X2="  #   #  "
   X3="    #    "
   X4="    #    "
   X5="  #   #  "
   X6="#       #"
   X =[X1,X2,X3,X4,X5,X6]

   word_i = 0
   alphabet_i = 0
 
   while(word_i<len(word)):
      while(alphabet_i<6):
         if(word[word_i] == 'F'):
            print(F[alphabet_i])
         elif(word[word_i] == 'O'):
            print(O[alphabet_i])
         elif(word[word_i] == 'X'):
            print(X[alphabet_i])
         alphabet_i+=1
      print()  
      alphabet_i = 0  
      word_i+=1
setup()


Result

# # # # #
#
# # # #
#
#
#

 # # # # 
#       #
#       #
#       #
#       #
 # # # # 

#       #
  #   #  
    #    
    #    
  #   #  
#       #


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

Lab6-- Find total number of chairs,find index maximum chair of floor of this building

def setup():
   floor1=[50,55,49,40,50]
   floor2=[70,55,100,1000]
   floor3=[49,53,53,60]
   building=[floor1,floor2,floor3]
   print('this building have',find_num_total_chair(building),'chair')
   print('the floor which have maximum chair is floor',find_max_chair_in_floor(building))

def find_num_total_chair(b):
   room = 0
   floor = 0
   total_chair = 0
   while(floor<len(b)):
      while(room<len(b[floor])):
         total_chair+=b[floor][room]
         room+=1
      room = 0
      floor+=1
   return total_chair
 
def find_max_chair_in_floor(b):
   room = 0
   floor = 0
   total_chair = 0
   total_of_max_chair = 0
   max_chair_floor = 0
   while(floor<len(b)):
      while(room<len(b[floor])):
         total_chair+=b[floor][room]
         room+=1
      if(total_chair>total_of_max_chair):
         total_of_max_chair = total_chair
         max_chair_floor = floor+1
      room = 0
      total_chair = 0
      floor+=1
   return max_chair_floor


setup()



Lab6-- Display student records with weight < 50

def setup():
   name = ['jinx','Vi','Kat','Zed']
   ID = [58102,58201,58101,58202]
   age = [18,19,17,16]
   weight = [44,65,70,70]
   height = [164,169,162,180]
   print("Let's have a look who got weight less than 50")
   assert(count_weight_less_than_50(weight)==1)
   find_weight_less_than_50(name,ID,age,weight,height)
   print('minimum weight of students is',find_minimum_weight(name,ID,age,weight,height))
   
def count_weight_less_than_50(weight):
   i = 0
   count = 0
   while(i<len(weight)):
      if(weight[i]<50):
         count+=1
      i+=1
   return count

def find_weight_less_than_50(name,ID,age,weight,height):
   i = 0
   while(i<len(weight)):
      if(weight[i]<50):
         print('name :',name[i])
         print('ID :', ID[i])
         print('age :',age[i])
         print('weight :',weight[i])
         print('height :',height[i])
         print('\n')
      i+=1

     
def find_minimum_weight(name,ID,age,weight,height):
   i = 0
   minimun_weight = weight[i]
   while(i<len(weight)):
      if(minimun_weight>weight[i]):
         minimun_weight = weight[i]
      i = i+1
   return minimun_weight

setup()
Result
Let's have a look who got weight less than 50
name : jinx
ID : 58102
age : 18
weight : 44
height : 164


minimum weight of students is 44
 
  

Lab6-- Display student records with BMI > 25

def setup():
   name = ['jinx','Vi','Kat','Zed']
   ID = [58102,58201,58101,58202]
   age = [18,19,17,16]
   weight = [44,65,70,70]
   height = [164,169,162,180]
   print("Let's have a look who got BMI over 25")
   assert(count_over_25_BMI(weight,height)==1)
   find_over_25_BMI(name,ID,age,weight,height)

     
def BMI(weight,height):
   BMI = weight/((height/100)**2)
   return BMI

def count_over_25_BMI(weight,height):
   i = 0
   count = 0
   while(i<len(weight)):
      if(BMI(weight[i],height[i])>25):
         count+=1
      i+=1
   return count

def find_over_25_BMI(name,ID,age,weight,height):
   i = 0
   #BMI = 25  
   while(i<len(weight)):
      if(BMI(weight[i],height[i])>25):
         print('name :',name[i])
         print('ID :', ID[i])
         print('age :',age[i])
         print('weight :',weight[i])
         print('height :',height[i])
         print('BMI :',BMI(weight[i],height[i]))
         print('\n')
      i+=1
     
setup()

Result
Let's have a look who got BMI over 25
name : Kat
ID : 58101
age : 17
weight : 70
height : 162 
BMI : 26.672763298277697   

Lab 6 - Display student data

def setup():
   name = ['jinx','Vi','Kat','Zed']
   ID = [58102,58201,58101,58202]
   age = [18,19,17,16]
   weight = [44,65,54,70]
   height = [164,169,162,180]
   i = 0
 
   while(i<len(name)):
      print('name :',name[i])
      print('ID :', ID[i])
      print('age :',age[i])
      print('weight :',weight[i])
      print('height :',height[i])
      print('BMI :',BMI(weight[i],height[i]))
      print('\n')
      i+=1

def BMI(weight,height):
   BMI = weight/((height/100)**2)
   return BMI

setup()  

Result
name : jinx
ID : 58102
age : 18
weight : 44
height : 164
BMI : 16.359309934562763


name : Vi
ID : 58201
age : 19
weight : 65
height : 169
BMI : 22.758306781975424


name : Kat
ID : 58101
age : 17
weight : 54
height : 162
BMI : 20.576131687242793


name : Zed
ID : 58202
age : 16
weight : 70
height : 180
BMI : 21.604938271604937

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

Lab5-My replace

def my_replace(word,old,new):
   i_word = 0
   i_Alphabet = 0
   newWord = ''
   if(len(old)<len(word)):
      while(i_word<len(word)):
         if(cheakWord(i_word,i_Alphabet,old,word)):
            newWord += new[i_Alphabet]
            i_Alphabet += 1
         else:
            newWord += word[i_word]
         i_word += 1
      return newWord
 
def cheakWord(i_word,i_Alphabet,old,word):
   if(i_Alphabet<len(old)):
      if(word[i_word] == old[i_Alphabet]):
         return True
   return False

def setup():
   S='DevilMayCry'
   old='May'
   new='Die'
   print(my_replace(S,old,new))
 
 
setup()

Result
DevilDieCry

Lab5-My find

def my_find(string,alphabet):
   i = 0
   posAlpha = 0
   print('position of',alphabet,'in',string,'is')
   while(i<len(string)):
      if(string[i] == alphabet):
         print(i)
      i+=1

def setup():
   S = 'Straightforward'
   alp = 'r'
   my_find(S,alp)
setup()  

Result
position of r in Straightforward is
2
10
13

Lab5-My count

def my_count(string,alphabet):
   i = 0
   numAlpha = 0
   while(i<len(string)):
      if(string[i] == alphabet):
         numAlpha+=1
      i+=1
   return numAlpha

def setup():
   S = 'Straightforward'
   alp = 'r'
   assert(my_count(S,alp) == 3 )
   print('number of',alp,'in',S,'is',my_count(S,alp))
setup()

Result
number of r in Straightforward is 3

Lab5-Increase/decrease value in array

def InDeValue(array,value):
   percent = (100+value)/100
   i = 0
   newArray = 0
   print('before  after')
   while(i<len(array)):
      newArray = percent*array[i]
      print(array[i],newArray)
      i+=1

def setup():
   A=[20,30,90,60,100,60,50]
   InDeValue(A,20)
   InDeValue(A,-20)
setup()
 
Result
before  after
20 24.0
30 36.0
90 108.0
60 72.0
100 120.0
60 72.0
50 60.0
before  after
20 16.0
30 24.0
90 72.0
60 48.0
100 80.0
60 48.0
50 40.0

Lab5-Find/count number of positive value

def countPositve(array):
   i = 0
   numPosi = 0
   while(i<len(array)):
      if(array[i]>0):
         numPosi+=1
      i+=1
   return numPosi

def setup():
   A=[28,-32,-90,60,-100,12,-50,60]
   print("number of positive value =",countPositve(A))
setup()
Result
number of positive value = 4

Lab5-Sum of postive value

def sumOfPositve(array):
   i = 0
   sumPosi = 0
   while(i<len(array)):
      if(array[i]>0):
         sumPosi+=array[i]
      i+=1
   return sumPosi

def setup():
   A=[28,-32,-90,60,-100,12,-50,60]
   print("Sum of positive value =",sumOfPositve(A))
setup()

Result
Sum of positive value = 160

Lab5-startwith

def startswith(string,firstString):
   i=0
   if(len(firstString)<len(string)):
      while(i<len(firstString)):
         if(string[i] == firstString[i]):
            return True
         i+=1
      return False
def setup():
   print(startswith('Thailand','Thai'))

setup()

Result
True

Lab5-Find maximum number in array

def findMaximumNumber(array):
   i = 0
   maxNum = 0
   while(i<len(array)):
      if(maxNum<array[i]):
         maxNum = array[i]
      i = i+1
   return maxNum

def setup():
   A=[28,32,90,60,100,12,50]
   print("maximum of array is",findMaximumNumber(A))

setup()

result
maximum of array is 100