26 ตุลาคม 2558

Lab6 Matrix

def setup():
   matrix = [[1,2,3], [6,5,4], [7,8,9]]
   matrix2  =[[7,3,3], [6,1,2], [5,6,1]]
   print("matrix 1 :")
   displayMatrix(matrix)
   print()
   print("matrix 2 :")
   displayMatrix(matrix2)
   print()
   addMatrix(matrix,matrix2)
   print()
   subtractMatrix(matrix , matrix2)
   print()
   multiplyMatrix(matrix, matrix2)
   print()
   transposeMatrix(matrix)


def displayMatrix(matrix):
   row = 0
   while(row<len(matrix)):
      column = 0
      while(column<len(matrix[row])):
         print(matrix[row][column]," ",end="")
         column+=1
      print()
      row+=1
   
def addMatrix(matrix, matrix2):
   row = 0
   print("Add matrix :")
   while(row<len(matrix)):
      column = 0
      while(column<len(matrix[row])):
         print(matrix[row][column]+matrix2[row][column]," ",end="")
         column+=1
      print()
      row+=1
def subtractMatrix(matrix , matrix2):
   row = 0
   print("Subtract matrix :")
   while(row<len(matrix)):
      column = 0
      while(column<len(matrix[row])):
         print(matrix[row][column]-matrix2[row][column]," ",end="")
         column+=1
      print()
      row+=1

def multiplyMatrix(matrix1, matrix2):
   if(len(matrix1[0])==len(matrix2)):#if(column number of matrix1 == row number of matrix2)
      row = 0  
      print("Multiply matrix :")
      while(row<len(matrix1)):#loop for shift row of multiplicand matrix
         column = 0
         while(column<len(matrix2[row])):#loop for shift column of multiplier matrix
            k = 0
            val = 0
            while(k<len(matrix1[row])):#loop for shift column of multiplicand matrix and shift row of multiplier matrix
               val += matrix1[row][k]*matrix2[k][column]#matrix_row[i]_column[k]*matrix2_row[k]_column[j]
               k+=1
            print(val," ",end="")
            column+=1
         print()
         row+=100
   else:
      print("Can't multiply matrix")
      print("Column number of multiplicand matrix not equal row number of multiplier matrix")
   
   
def transpose(matrix):
    i = 0
    j = 0
    while i < len(matrix):
         j += i
         while (j < len(matrix[i])):
            dataCopy = matrix[i][j]
            matrix[i][j] = matrix[j][i]
            matrix[j][i] = dataCopy
            j+=1
         print(matrix[i])
         j = 0
         i+=1
    displayMatrix(matrix)
   


setup()

Lab6 Write letter

def setup():
   A = ["     #     ",
        "    # #    ",
        "   #   #   ",
        "  #######  ",
        " #       # ",
        "#         #"]
   B = ["#########",
        "#       #",
        "#######  ",
        "#      # ",
        "#       #",
        "######## ",]
   C = [" ########",
        "#        ",
        "#        ",
        "#        ",
        "#        ",
        " ########"]
   D = ["#######  ",
        "#      # ",
        "#       #",
        "#       #",
        "#      # ",
        "#######  ",]
   E = ["#########",
        "#        ",
        "#######  ",
        "#        ",
        "#        ",
        "#########"]
   i = 0
 
   while(i<len(A)):
      print(A[i]," ",B[i]," ",C[i]," ",D[i]," ",E[i])
      i+=1
     
setup()

Lab6 Chairs in the building

def setup():
   floor1 = [20,43,32,43]
   floor2 = [39,33,24,43]
   floor3 = [25,22,30,43]
   building = [floor1,floor2,floor3]
   totalChairs(building)
   maxFloor(building)
   maxRoom(building)

def totalChairs(building):
   total_chairs = 0
   i = 0
   while(i<len(building)):
      j = 0
      while(j<len(building[i])):
            total_chairs += building[i][j]
            j+=1
      i+=1
   print("Total chairs in the building :",total_chairs)
   print("-----------------------------------------------------------------")

def maxFloor(building):
   i = 0
   max_chair = 0;
   i_max = 0;
   print("Index of the floor with maximum number of chairs")
   while(i<len(building)):
      j = 0
      total_chairs = 0
      while(j<len(building[i])):
            total_chairs += building[i][j]
            j+=1
      if(total_chairs>max_chair):
         max_chair = total_chairs
         i_max = i
      i+=1
   print("Index of floor : ",i_max,"(",max_chair,")",sep="")
   print("-----------------------------------------------------------------")

def maxRoom(building):
   i = 0
   max_chair_room = 0
   while(i<len(building)):
      j = 0
      while(j<len(building[i])):
         if(building[i][j]>max_chair_room):
            max_chair_room=building[i][j]
         j+=1
      i+=1

   count_room = 0
   i = 0
   print("Indices the rooms with maximum number of chairs ")
   while(i<len(building)):
      j = 0
      while(j<len(building[i])):
         if(building[i][j]==max_chair_room):
            print("Index of room :",j," | ","Index of floor :",i)
         j+=1
      i+=1
   print("-----------------------------------------------------------------")
 
setup()

Lab6 Parallel array

def setup():
   st_name = ["Tar","Tape","Bas","Top","Karn"]
   st_id = [10012,10032,10075,10091,10130]
   st_age = [21,21,19,21,18]
   st_weight = [72,93,69,91,68]
   st_height = [179,178,174,172,184]
 
   stData(st_name, st_id, st_age, st_weight, st_height)
   stBmi(st_name, st_id, st_age, st_weight, st_height)
   stAge(st_name, st_id, st_age, st_weight, st_height)
   stWeight(st_name, st_id, st_age, st_weight, st_height)
 
def stData(name,id,age,w,h):
   i = 0
   while(i<len(name)):
      print("Name :",name[i])
      print("ID :",id[i])
      print("Age :",age[i])
      print("Weight :",w[i])
      print("Height :",h[i])
      print("------------------------")
      i+=1

def stBmi(name,id,age,w,h):
   i = 0
   count = 1
   bmi = [0]*5
   while(i<len(w)):
      #bmi[i]=float("{0:.2f}".format(w[i]/((h[i]/100)*(h[i]/100))))
      bmi[i]=float(format((w[i]/((h[i]/100)*(h[i]/100))),'.2f'))
      if(bmi[i]>25):
         print("No.",count,sep="")
         print("Name :",name[i])
         print("ID :",id[i])
         print("Age :",age[i])
         print("Weight :",w[i])
         print("Height :",h[i])
         print("Bmi :",bmi[i])
         print("------------------------")
         count+=1
      i+=1
   print ("BMI :",bmi)
   print("------------------------")

def stAge(name,id,age,w,h):
   i = 0
   sum_age = 0
   while(i<len(age)):
      sum_age += age[i]
      i+=1
   
   avg_age = sum_age/len(name)
   print("Average age :",avg_age)
   print("------------------------")

def sortRecord(name,id,age,weight,height):
    i = 0
    while (i<len(age)):
        if(i!=len(age)-1 and age[i]>age[i+1]):
            j = i
            while (j >= 0):
                if(age[j]>age[j+1]):
                    copy_age = age[j]
                    copy_name = name[j]
                    copy_id = id[j]
                    copy_weight = weight[j]
                    copy_height = height[i]
                    age[j] = age[j+1]
                    age[j+1] = copy_age
                    name[j] = name[j+1]
                    name[j+1] = copy_name
                    id[j] = id[j+1]
                    id[j+1] = copy_id
                    weight[j] = weight[j+1]
                    weight[j+1] = copy_weight
                    height[j] = height[j+1]
                    height[j+1] = copy_height
                    j-=1
                else :
                    break
        i+=1
    print("Sort record by age")
    print("------------------------")
    stData(name,id,age,weight,height)

def stWeight(name,id,age,w,h):
   i = 0
   min_weight = w[0]
   count = 1
   while(i<len(w)):
      if(w[i]<min_weight):
         min_weight = w[i]
      if(w[i]<70):
         print("No.",count,sep="")
         print("Name :",name[i])
         print("ID :",id[i])
         print("Age :",age[i])
         print("Weight :",w[i])
         print("Height :",h[i])
         print("------------------------")
         count+=1
      i+=1
   print("Minimum weight of students :",min_weight)
   print("------------------------")

setup()

5 ตุลาคม 2558

Lab5 Final

def setup():
   text1 = "Thailand  "
   print(text1)
   assert my_count(text1) ==2
   print("my_count",my_count(text1))
   assert my_find(text1) ==3
   print("my_find",my_find(text1))
   assert my_replace(text1) == "Thaithai"
   print("my_replace",my_replace2(text1))
   print("my_strip",my_strip(text1))
   print("my_startswith",my_startswith(text1))
   print("my_endswith",my_endswith(text1))

def my_count(text):
   find = "a"
   countFind = 0
   i = 0
   while(i<len(text)):
      if(find == text[i]):
         countFind +=1
      i+=1
   return countFind

def my_find(text):
   find = "i"
   indexFind = 0
   i = 0
   while(i<len(text)):
      if(find == text[i]):
         indexFind = i
      i+=1
   return indexFind

def my_replace(text):
   find = "land"
   replace = "thai"
   newText = ""
   i = 0
   i_find = 0
   while(i<len(text)):
      if(text[i]==find[i_find]):
         checkAll = 0
         while (checkAll < len(find)):
            if(find[i_find]==text[i+i_find]):
               i_find+=1
               checkAll+=1
            else:
               newText = newText+text[i]
               i_find = 0
               checkAll = len(find)
            if(i_find==len(find)):
               n = 0
               m = len(replace)
               i_replace = 0
               while(n < m):
                  newText = newText+replace[i_replace]
                  i_replace +=1
                  n+=1
               i +=len(find)-1
               i_find = 0
      else :
         if(text[i] != " "):
             newText = newText+ text[i]
      i+=1
   return newText

def my_strip(text):
   newText = ""
   i=0
   while(i < len(text)):
      if(text[i] != " "):
            newText = newText+ text[i]
      i+=1
   return newText

def my_startswith(text):
   find = "Sun"
   i = 0
   while(i < len(text)):
      if(find[i] == text[i]):
         j =0
         while(j < len(find)):
            if(text[i+j] == find[j]):
               j +=1
               if (j == len(find)):
                  return True
            else :
               return False
      else :
          return False

def my_endswith(text):
   find = "and"
   i = len(text)-1
   i_find = len(find)-1
   while(i > 0):
      if(find[i_find] == text[i]):
         checkAll = 0
         while(checkAll < len(find)):
            if(text[i-checkAll] == find[i_find-checkAll]):
               checkAll +=1
               if (checkAll == len(find)):
                  return True
            else :
               return False
      else :
          return False

setup()
       

Lab5 Draft

def setup():
   val_array = [8,-2,20,-12,-9,20,2,-6,20,4]
   display_value_array_and_index(val_array)
   print("")
   find_max_val_array(val_array)
   print("")
   find_first_last_index_max_val(val_array)
   print("")
   find_sum_array(val_array)
   print("")
   find_sum_of_positive_val_in_array(val_array)
   print("")
   find_count_num_of_positive_val_in_array(val_array)
   print("")
   find_avg_values_array(val_array)
   print("")
   inc_dec_val_array(val_array)

def display_value_array_and_index(a):
   i = 0
   while(i < len(a)):
      val = a[i]
      print ("index",i," ",end="")
      print ("value =",val)
      i+=1
############################################################################
def find_max_val_array(a):
   i = 0
   max_val = 0
   index_max = 0
   while(i < len(a)):
      value_in_array = a[i]
      if (max_val < value_in_array):
         max_val = value_in_array
         index_max = i
      i+=1
   print ("Maximum value in array =",max_val)
############################################################################
def find_first_last_index_max_val(a):
#Find index of (the first) maximum value in array
   max_val = 0
   first_max_i = 0
   i = 0
   while(i < len(a)):
      if(max_val<a[i]):
         max_val = a[i]
         first_max_i = i
      i+=1
#Find index of (the last) maximum value in array
   max_val = 0
   last_max_i = 0
   i  = (len(a)-1)
   while(i > (-1)):
      if(max_val<a[i]):
         max_val = a[i]
         last_max_i = i
      i-=1
   
   print("The First maximum value","Index :","%2.0d"%first_max_i," Value :",max_val)
   #i=0
   #while(i < len(a)):
      #if(i >first_max_i and i < last_max_i and a[i]==max_val):
         #print("Index :","%2.0d"%i,"   ","Value :",a[i])
      #i+=1
   print("The Last maximum value"," Index :","%2.0d"%last_max_i," Value :",max_val)
############################################################################
def find_sum_array(a):
   i = 0
   sum_array = 0
   print ("Sum of values in array = ",end="")
   while(i < len(a)):
      sum_array += a[i]
      if(a[i]>0):
         print(a[i],end="")
      else :
         print("(",a[i],")",end="",sep="")
      if(i==(len(a)-1)):
         print(" = ",end="")
      else:
         print("+",end="")
      i+=1
   print (sum_array)
############################################################################
def find_sum_of_positive_val_in_array(a):
   i = 0
   sum_array = 0
   print ("Sum of positive values in array = ",end="")
   while(i < len(a)):
      if(a[i]>=0):
         sum_array += a[i]
         print(a[i],end="")
         if(i==(len(a)-1)):
            print(" = ",end="")
         else:
            print("+",end="")
      i+=1
   print (sum_array)
############################################################################
def find_count_num_of_positive_val_in_array(a):
   i = 0
   sum_array = 0
   num_positive_array = 0
   print ("Count number of positive values in array = ",end="")
   while(i < len(a)):
      if(a[i]>=0):
         num_positive_array+=1
      i+=1
   print (num_positive_array,"number (",end="")
   i=0
   while(i < len(a)):
      if(a[i]>=0):
         print(a[i],end="")
         if(i==(len(a)-1)):
            print(")")
         else:
            print(",",end="")
      i+=1
############################################################################
def find_avg_values_array(a):
   i = 0
   sum_array = 0
   print ("Average of values in array = (",end="")
   while(i < len(a)):
      sum_array += a[i]
      if(a[i]>0):
         print(a[i],end="")
      else :
         print("(",a[i],")",end="",sep="")
      if(i==(len(a)-1)):
         print(")/",len(a)," = ",end="",sep="")
      else:
         print("+",end="")
      i+=1
   avg_array = sum_array/len(a)
   print (avg_array)
############################################################################
def inc_dec_val_array(a):
   print("Increase/decrease values in array")
   mode_inc_dec = input("Increase Press 'i'     Decrease Press 'd'")
   print()
   mode_fixed_percent = input("by Fixed value Press 'f'    by Percentage Press 'p'")
   print()
   val = int(input("Please type value"))
   print()
   i = 0
   if (mode_inc_dec == 'i' or mode_inc_dec == 'I'):
      if(mode_fixed_percent == 'f' or mode_fixed_percent == 'F'):
         while(i < len(a)):
            a[i]+=val
            i+=1
      elif(mode_fixed_percent == 'p' or mode_fixed_percent == 'P'):
         while(i < len(a)):
            if(a[i]<0):
               a[i]-=((val/100)*a[i])
            else : a[i]+=((val/100)*a[i])
            i+=1
   if (mode_inc_dec == 'd' or mode_inc_dec == 'D'):
      if(mode_fixed_percent == 'f' or mode_fixed_percent == 'F'):
         while(i < len(a)):
            a[i]+=-val
            i+=1
      elif(mode_fixed_percent == 'p' or mode_fixed_percent == 'P'):
         while(i < len(a)):
            if(a[i]<0):
               a[i]+=((val/100)*a[i])
            else : a[i]-=((val/100)*a[i])
            i+=1
   print("Value in array =",a)
 
setup()