วันอาทิตย์ที่ 6 ธันวาคม พ.ศ. 2558

Lab8-Weight

public class student{

      private String name;
      private int ID;
      private int age;
      private int weight;
      private int height;
      private float BMI;

      public student(String name, int ID, int age, int weight, int height){
       this.name = name;
        this.ID = ID;
        this.age = age;
        this.weight = weight;
        this.height = height;
        this.BMI = weight/((height/100)^2);

      }

   public float get_weight(){
      return this.weight;
   }

   public void display(){
      System.out.println("name:  "+this.name );
      System.out.println("ID:    "+this.ID);
      System.out.println("age:   "+this.age);
      System.out.println("weight:"+this.weight);
      System.out.println( "height:"+this.height );
      System.out.println("BMI:"+this.BMI);
   }
   public static void main(String[] args){
      student S[] ={ new student("jinx",58102,18,44,164),
                            new student("Vi",58201,19,80,165),
                            new student("Kat",58101,20,70,160)};
     
      System.out.println("The number of student who got weight less than 50 :"+count_weight_less_than_50(S));
      display_weight_less_than_50(S);
 
   }
   public static int count_weight_less_than_50(student[] S){
      int count = 0;
      int i = 0;
      while(i<S.length){
         if(S[i].get_weight()<50){
            count+=1;
         }
         i+=1;
      }
      return count;
   }

   public static void display_weight_less_than_50(student[] S){
      int i = 0;
      while(i<S.length){
         if(S[i].get_weight()<50){
            S[i].display();
         }
         i+=1;
      }


   }

}

Lab8-BMI

public class student{

      private String name;
      private int ID;
      private int age;
      private int weight;
      private int height;
      private float BMI;
   
      public student(String name, int ID, int age, int weight, int height){
       this.name = name;
        this.ID = ID;
        this.age = age;
        this.weight = weight;
        this.height = height;
        this.BMI = weight/((height/100)^2);
 
      }

   public float get_BMI(){
      return this.BMI;
   }
   
   public int get_height(){
      return this.height;
   }

   public void display(){
      System.out.println("name:  "+this.name );
      System.out.println("ID:    "+this.ID);
      System.out.println("age:   "+this.age);
      System.out.println("weight:"+this.weight);
      System.out.println( "height:"+this.height );
      System.out.println("BMI:"+this.BMI);
   }

   public static void main(String[] args){
      student S[] ={ new student("jinx",58102,18,44,164),
                            new student("Vi",58201,19,80,165),
                            new student("Kat",58101,20,70,160)};
      int i = 0;
      while(i<S.length){
         if(S[i].get_BMI()>25){
            S[i].display();
            System.out.println();
         }
         i+=1;
      }
   }
   

}

วันเสาร์ที่ 5 ธันวาคม พ.ศ. 2558

วันอังคารที่ 1 ธันวาคม พ.ศ. 2558

Lab8-banner

public class banner{
   String word,character;
   public banner(String word){
      this.word = word;
      //this.character = character;
   }

   public void show(){
      String F1="# # # # #";
      String F2="#";
      String F3="# # # #";
      String F4="#";
      String F5="#";
      String F6="#";
      String[] F = {F1,F2,F3,F4,F5,F6};
      String O1=" # # # # ";
      String O2="#        #";
      String O3="#        #";
      String O4="#        #";
      String O5="#        #";
      String O6=" # # # # ";
      String[] O={O1,O2,O3,O4,O5,O6};
      String X1="#       #";
      String X2="  #   #  ";
      String X3="    #    ";
      String X4="    #    ";
      String X5="  #   #  ";
      String X6="#       #";
      String[] X ={X1,X2,X3,X4,X5,X6};
   
   
      int word_i = 0;
      while(word_i<this.word.length()){
         int alphabet_i = 0;
         while(alphabet_i<F.length){
            if(word.charAt(word_i) == 'F'){
               System.out.println(F[alphabet_i]);
            }else if(word.charAt(word_i) == 'O'){
               System.out.println(O[alphabet_i]);
            }else if(word.charAt(word_i) == 'X'){
               System.out.println(X[alphabet_i]);
            }
            alphabet_i+=1;
         }
        System.out.println();
        word_i+=1;
      }
   }
   public static void main(String[] arg){
      String word = "FOX";
      banner b = new banner(word);
      b.show();
   }


}

วันอังคารที่ 17 พฤศจิกายน พ.ศ. 2558

Report

A1
   -can't run on website
   -history have a problem
   -coin have a problem

Raspberry pi

def setup():
    S = [Student('Jinx',12,56),
         Student('Vi',13,25),
         Student('Garen',14,80),
         Student('Missfor',15,64),
         Student('Zed',16,85)]

    show_allGrade(S)
    print(count_grade('A',S))

class Student:
    def __init__(self,name,ID,score):
        self.name = name
        self.ID = ID
        self.score = score

    def get_score(self):
        return self.score

    def set_ID(self,score):
        self.score = score

    def display(self):
        print('name:',self.name)
        print('ID:',self.ID)
        print('score:',self.score)
       
   
def find_grade(score):
    if(score<50):
        return 'F'
    elif(score>=50 and score<60):
        return 'D'
    elif(score>=60 and score<70):
        return 'C'
    elif(score>=70 and score<80):
        return 'B'
    elif(score>=80):
        return 'A'

def count_grade(grade,S):
    i = 0
    count = 0
    while(i<len(S)):
        if(grade == find_grade(S[i].get_score())):
            count+=1
        i+=1  
    return count          

def show_allGrade(S):
    i = 0
    while(i<len(S)):
        S[i].display()
        print(find_grade(S[i].get_score()))
        print()
        i+=1
   
   
setup()

วันเสาร์ที่ 14 พฤศจิกายน พ.ศ. 2558

Complex

class Complex:
   def __init__(self,real_num,imagine_num=0):
      self.real_num = real_num
      self.imagine_num = imagine_num
 
   def get_real_num(self):
      return self.real_num

   def get_imagine_num(self):
      return self.imagine_num

   def add(self,b):
      real_num = self.real_num + b.get_real_num()
      imagine_num = self.imagine_num + b.get_imagine_num()
      c = Complex(real_num,imagine_num)
      return c

   def display(self):
      i = self.imagine_num
      if i<0 :
         print(self.real_num,end=" ")
      else:
         print(self.real_num,end="+")
      print(i,end="i")
      print()
 
def setup():
   a = Complex(5,-1)
   b = Complex(2,-1)
   c = Complex(0,0)
   c = a.add(b)
   a.display()
   b.display()
   c.display()
setup()

Fraction

class Fraction:
   def __init__(self,numerator,denominator):
      self.numerator = numerator
      self.denominator = denominator
   
   def get_numerator(self):
      return self.numerator
 
   def get_denominator(self):
      return self.denominator

   def add(self,b):    
      c_numerator = self.numerator*b.get_denominator() + b.get_numerator()*self.denominator
      c_denominator = self.denominator*b.get_denominator()
      c = Fraction(c_numerator,c_denominator)
      return c
       
   def display(self):
      print(self.numerator,'/',self.denominator)
   
def setup():
   a = Fraction(5,3)
   b = Fraction(4,7)
   c = a.add(b)
   a.display()
   print('+')
   b.display()
   print('=')
   c.display()
setup()

vector

class Vector:
   def __init__(self,i,j):
      self.i = i
      self.j = j
 
   def get_i(self):
      return self.i

   def get_j(self):
      return self.j

   def add(self,b):
      c_i = self.i + b.get_i()
      c_j = self.j + b.get_j()
      c = Vector(c_i,c_j)
      return c
   
   def display(self):
        #print('(')
        print('(',self.i,end='i')
        if(self.j>0):
          print('+',self.j,end='j')
        print(')')
 
def setup():
   a = Vector(5,3)
   b = Vector(4,7)
   c = a.add(b)
   a.display()
   print('+')
   b.display()
   print('=')
   c.display()
setup()

วันอาทิตย์ที่ 8 พฤศจิกายน พ.ศ. 2558

Lab6-banner

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(alphabet_i<6):
      alphabet_i = 0
      while(word_i<len(word)):
         if(word[alphabet_i] == 'F'):
            print(F[word_i])
         elif(word[alphabet_i] == 'O'):
            print(O[word_i])
         elif(word[alphabet_i] == 'X'):
            print(X[word_i])
         alphabet_i+=1
      print()
      word_i+=1
setup()

Lab7-banner

class banner:
   def __init__(self,word,char):
      self.word = word
      self.char = char
 
   def set_word(self):
      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]


      my_replace(F,'#',self.char)
      my_replace(O,'#',self.char)
      my_replace(X,'#',self.char)
 
      word_i = 0
      alphabet_i = 0
      while(word_i<len(self.word)):
         while(alphabet_i<6):
            if(self.word[word_i] == 'F'):
               print(F[alphabet_i])
            elif(self.word[word_i] == 'O'):
               print(O[alphabet_i])
            elif(self.word[word_i] == 'X'):
               print(X[alphabet_i])
            alphabet_i+=1
         alphabet_i = 0
         print()
         word_i+=1

   
def my_replace(word,old,new):
   newWord = ""
   i = 0
   j = 0
   while(i < len(word)):
      while(j< len(word[i])):      
         if(word[i][j] != old[0]):
            newWord += word[i][j]
            j += 1
         else:
            num = 0
            while(num < len(old) and old[num] == word[i][j+num]):
               num += 1
            if(num == len(old)):
               newWord += new
               j += len(old)
      word[i] = newWord
      newWord = ""
      j = 0    
      i+=1              


def setup():
   b = banner('FOX','&')
   b.set_word()

setup()

วันพุธที่ 4 พฤศจิกายน พ.ศ. 2558

Lab8-display student by java

public class student{

      private String name;
      private int ID;
      private int age;
      private int weight;
      private int height;
     
      public student(String name, int ID, int age, int weight, int height){
       this.name = name;
        this.ID = ID;
        this.age = age;
        this.weight = weight;
        this.height = height;
   
      }

   public int get_weight(){
      return this.weight;
      }
     
   public int get_height(){
      return this.height;
   }

   public void display(){
      System.out.println("name:  "+this.name );
      System.out.println("ID:    "+this.ID);
      System.out.println("age:   "+this.age);
      System.out.println("weight:"+this.weight);
      System.out.println( "height:"+this.height );
   }

   public static void main(String[] args){
      student S[] ={ new student("jinx",58102,18,44,164),
                            new student("Vi",58201,19,65,169),
                            new student("Kat",58101,20,70,160)};
      int i = 0;
      while(i<S.length){
         S[i].display();
         System.out.println();
         i+=1;
      }
   }
     

}

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

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

name:  Kat
ID:    58101
age:   20
weight:70
height:160


วันเสาร์ที่ 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

วันอังคารที่ 22 กันยายน พ.ศ. 2558

Lab5-- Find average of values in array

def findAverage():
   ListNum=[1,2,3,4,5,6,7,8]
   i=0
   sum=0
   while(i<len(ListNum)):
      sum=sum+ListNum[i]
      i=i+1
   average=sum/len(ListNum)  
   print("Average of Array =",average)
 
findAverage()

Result
Average of Array = 4.5

Lab5-Find Sum of value in array

def findSum():
   ListNum=[1,2,3,4,5,6,7,8]
   i=0
   sum=0
   #lengthListNum=len(ListNum)-1
   while(i<len(ListNum)):
      sum=sum+ListNum[i]    
      print("+",ListNum[i])  
      i=i+1
   print("Sum=",sum)
findSum()


Result
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
Sum= 36

วันจันทร์ที่ 21 กันยายน พ.ศ. 2558

Lab5-Element of Array

def eleArray():
   n=[1,2,3,4,5,6,7,8]
   i=0
   while(i<len(n)):
      print("n","[",i,"]","=",n[i])
      i=i+1
     
eleArray()  

ผลลัพธ์
n [ 0 ] = 1
n [ 1 ] = 2
n [ 2 ] = 3
n [ 3 ] = 4
n [ 4 ] = 5
n [ 5 ] = 6
n [ 6 ] = 7
n [ 7 ] = 8

วันอาทิตย์ที่ 20 กันยายน พ.ศ. 2558

Lab4x-Circle

import math
def calCircle(radius):
  circumferance=2*math.pi*radius
  area=math.pi*radius**2
  print("Radius=",radius)
  print("Circumferance=",circumferance)
  print("Area=",area)

calCircle(10)

Lab4x-Sum of Prime Number

def setup():
   print(sumOfPrimeNum(10))
def sumOfPrimeNum(lastNum):
   x=2
   sum=0
   while(x<=lastNum):
      if(primeNum(x)):
        sum=sum+x
      x=x+1
   return sum
 
def primeNum(p):
   x = 2
   while(x<p):
      if(p%x == 0 or p==1 ):
         return False
   
      x=x+1
   return True
setup()

Lab4x-Grade

def BMI(weight,heightM):
  heightCm=heightM/100
  BMI = weight/(heightCm**2);
  print("height=",heightM,"m")
  print("weight=",weight,"kg")
  print("BMI=",BMI)

BMI(71,167)   

#grade
def grade(score):
     text="\"your grade is"
     if(score<50):
        print(text,'F')
        print("You should have more practice")
     elif(score>=50 | score<=59):
        print(text,'D')
     elif(score>=60 | score<=69):
        print(text,'C')
     elif(score>=70 | score<=79):
        print(text,'B')
     elif(score>=80 | score<=100):
        print(text,'A')

grade(70)

Lab4x-BMI

def BMI(weight,heightM):
  heightCm=heightM/100
  BMI = weight/(heightCm**2);
  print("height=",heightM,"m")
  print("weight=",weight,"kg")
  print("BMI=",BMI)

BMI(71,167)  

Lab4x-Multiple Table

def setup(table):
  firstNum=1
  x=12
  while(firstNum<=x):
     result=table*firstNum
     print(table,"X",firstNum,"=",result)
     firstNum=firstNum+1
setup(25)

Lab4x-Sum of Integer

def setup(firstNum,lastNum):
  sum=0
  while(firstNum<=lastNum):
     sum=sum+firstNum
     firstNum=firstNum+1
  print("sum =",sum)
setup(1,10)

Lab4x-Service Charge

def serviceCharge(typeOfPackage, typeOfService, weightOfPackage):
   cost=0
   textTypePac="type of package is"
   textTypeSer="type of service is"
   textWeight="Weight of package is"
   if(typeOfPackage==1):
      print(textTypePac,"Letter")
      print(textWeight,weightOfPackage,"oz")
   elif(typeOfPackage==2):
      print(textTypePac,"Box")
      print(textWeight,weightOfPackage,"pound")
   else:
      print("Not Found")
   
   if(typeOfService==1):
      print(textTypeSer,"Next Day Priority")
   elif(typeOfService==2):
      print(textTypeSer,"Next Day Standard")
   elif(typeOfService==3):
      print(textTypeSer,"Two-Day")
   else:
      print(textTypeSer,"Not Found")
   
   if(typeOfPackage==1):
      if(typeOfService==1 and weightOfPackage<=8):
         cost=12
      elif(typeOfService==2 and weightOfPackage<=8):
         cost=10
      else:
         print("No Service")
       
   if(typeOfPackage==2):
      if(typeOfService==1):
         if(weightOfPackage<=1):
             cost=15.75
         elif(weightOfPackage>1):
             cost=15.75+((weightOfPackage-1)*1.25)
      elif(typeOfService==2):
         if(weightOsPackage<=1):
             cost=13.75
         elif(weightOfPackage>1):
             cost=13.75+((weightOfPackage-1)*1.00)
      elif(typeOfService==3):
         if(weightOfPackage<=1):
             cost=7
         elif(weightOfPacksge>1):
             cost=7+ ((weightOfPackage-1)*0.50)
      print("Charge: $",cost)
   
serviceCharge(2,1,20)

วันอังคารที่ 15 กันยายน พ.ศ. 2558

Lab4-plant as zombie

int shoot;
int colour;
void setup(){
  size(500,500);
  }
void draw(){
  int n = 0;
  int count = 3;
  int another = 0;
  int x = 100;
  int y = 100;
  size(500,500);
  background(#FFFFFF);
  while(n<=count){
  zombie(x+another,y);
  another+=100;
  n+=1;
  }
  plant(mouseX,mouseY);
  pea(shoot,colour);
  advice(10,400);
  if( mousePressed == true){
    colour=#99ff00;
    shoot+=10;
  }
  while(shoot>350){
    shoot=0;
  }

}
void plant(int x, int y){
  stroke(#339900);
  strokeWeight(5);
  fill(#99FF00);
  arc(x-100,y,100,150,PI,TWO_PI);
  arc(x-100,y,100,100,PI,TWO_PI);
  fill(#FFFFFF);
  arc(x-100,y,100,70,PI,TWO_PI);
  fill(#99FF00);
  arc(x+20,y+100,100,200,HALF_PI,PI+HALF_PI);
  fill(#FFFFFF);
  arc(x+20,y+100,70,200,HALF_PI,PI+HALF_PI);
  //head
  stroke(#339900);
  strokeWeight(5);
  fill(#99FF00);
  ellipse(x-20,y+100,50,25);
  ellipse(x,y,200,200);//face
  ellipse(x+120,y+20,100,140);//mouth

  fill(#000000);
  ellipse(x+130,y+20,50,100);//hole

  //eye
  stroke(#000000);
  strokeWeight(1);
  ellipse(x+10,y-30,40,70);//Reye
  ellipse(x+50,y-40,30,60);//Leye
  fill(#FFFFFF);
  ellipse(x+5,y-40,15,30);
  ellipse(x+45,y-50,10,20);

  stroke(#339900);
  strokeWeight(5);
  fill(#00CC00);
  ellipse(x+70,y+200,100,50);
  ellipse(x-30,y+200,100,50);
  }
void pea(int shoot, int Gcolor){
  int r = 50;
  fill(colour);
  noStroke();
  ellipse(mouseX+130+shoot,mouseY+20,r,r);
}
void advice(int x, int y){
  textSize(30);
  fill(0);
  text("Press left mouse to shoot",x,y);
}
void zombie(int x, int y){
  fill(#999999);
  rect(x-30,y+30,60,30,20);
  ellipse(x,y,100,100);
  fill(#FFFFFF);
  ellipse(x-20,y-10,30,30);
  ellipse(x+20,y-10,30,30);
  rect(x-20,y+30,40,20,20);
  fill(0);
  ellipse(x-25,y-15,5,5);
  ellipse(x+20,y-5,5,5);

}

วันอาทิตย์ที่ 13 กันยายน พ.ศ. 2558

Lab4-Lone

void setup() {
  size(850, 600);
  background(0);
  int year = 1;
  calLone(5000,0.12,year);
  draw_text(year);
}
void calLone(float beginBalance, float interestRate, int year) {
  int x = 430;
  int y = 400;

  int month = year*12;
  float IRPM = interestRate/month;//interest rate per month
  float paypermonth = beginBalance*(IRPM/(1-pow(1+IRPM, -month)));
  float interest = 0;
  float principal = 0;
  float endingBalance = 0;
  int n = 1;

  while (n<=month) {
    interest = IRPM*beginBalance;
    principal = paypermonth - interest;
    endingBalance = beginBalance - principal;
 
    textSize(30);
    text(nf(+beginBalance,3,1),x-300,y-300);
    text(nf(+interest,2,1),x-100,y-300);
    text(nf(+principal,3,1),x+50,y-300);
    text(nf(+endingBalance,3,1),x+220,y-300);
    beginBalance = endingBalance;
    y+=40;
    n+=1;
  }
}

void draw_text(int year){
  int x = 430;
  int y = 400;
  int n = 1;
  int month = year*12;

  text("Begin Balance",x-350,y-350);
  text("interest",x-130,y-350);
  text("principal",x+20,y-350);
  text("Ending balance",x+160,y-350);

  while(n<=month){
    text(+n,x-370,y-300);
    y+=40;
    n++;
  }

}

Lab4-Sum of integers

void setup(){
  size(300,300);
  background(#CCCC00);
  int x = 100;
  int y = 100;
  int firstNum = 0;
  int lastNum = 10;
  int sum = 0 ;
  textSize(30);
  text("Sum of "+firstNum,x-50,y);
  text("to "+lastNum,x+90,y);
  text("is",x+50,y+40);
  while(firstNum<=lastNum){
    sum+=firstNum;
    firstNum++;
  }
 text(+sum,x+35,y+80);
}

Lab4-Sum of prime number

void setup(){
  size(500,400);
  background(#FFCC33);
  int x = 250;
  int y = 250;
  int firstNum = 0;
  int lastNum = 10;
  textSize(30);
  text("Sum of Prime Number from "+firstNum,x-200,y-100);
  text("to  "+lastNum,x-25,y-60);
  text("is",x,y-20);
  text(+sumOfPrime(firstNum,lastNum),x-10,y+20);
}

int sumOfPrime(int firstNum, int lastNum){
  int x = 2;
  int sum = 0;
  while(x<=lastNum){
    if(primeNum(x)){
      sum+=x;
    }
    x++;  
  }
  return sum;
}

boolean primeNum(int p){
  boolean result = true;
  int x = 2;
  while(x<p){
    if(p%x == 0 || p==1 ){
      result = false;  
    }
    x++;
  }
  return result;
}

Lab4-flock of bird

int x=250;
int y=250;
int fly=0;
int Rtree1mov;
int Rtree2mov;
int Ltree1mov;
int Ltree2mov;
void setup(){
  size(700,700);
  }
void draw(){
  size(700,700);
  background(#33FFFF);
  int posX = mouseX;
  int posY = mouseY;
  int n =0;
  int count = 3;
  int another = 0;
  frameRate(50);
    view_background(Rtree1mov,Rtree2mov,Ltree1mov,Ltree2mov);
  Rtree1mov+=1;
  Rtree2mov+=1;
  Ltree1mov+=1;
  Ltree2mov+=1;
  if(Rtree1mov>180){
    Rtree1mov=0;
  }
  if(Rtree2mov>80){
    Rtree2mov=-100;
  }
  if(Ltree1mov>180){
    Ltree1mov=0;
  }
  if(Ltree2mov>80){
    Ltree2mov=-100;
  }
  while(n<=count){
      bird(posX+another,posY+another,80,fly);
      bird(posX-another,posY+another,80,fly);
      if(frameCount%20>15){
      fly=-120;
      } else {
      fly=60;
      }
      n+=1;
      another+=100;
  }
}
void bird(int x, int y, int r, int fly){
  strokeWeight(5);
  line(x+r/2,y,x+80+fly/5,y+40+fly);
  line(x-r/2,y,x-80-fly/5,y+40+fly);
  fill(#666666);
  arc(x,y,100,150,PI+QUARTER_PI,TWO_PI-QUARTER_PI);
  ellipse(x,y,r,r);//body
  ellipse(x-15,y-10,r-60,r-60);//Leye
  ellipse(x+15,y-10,r-60,r-60);//Reye
  fill(#FFFF00);
  triangle(x-15,y+10,x,y+50,x+15,y+10);
}
void view_background(int Rtree1mov, int Rtree2mov, int Ltree1mov, int Ltree2move){
  int x = 350;
  int y = 350;
  int sizeX = 50;
  int sizeY = 90;
  strokeWeight(0);
  fill(#999999);
  quad(x+50,y,x-50,y,0,y+350,x+350,y+350);//fill colour lane
  fill(#99FF00);
  triangle(0,y,x-50,y,0,y+350);//fill colour left lane
  triangle(x+350,y,x+50,y,x+350,y+350);//fill colour right lane
  fill(#336600);
  rect(x+130+Rtree1mov,y-20+Rtree1mov,sizeX,sizeY);//trunk Rtree1
  rect(x-180-Ltree1mov,y-20+Ltree1mov,sizeX,sizeY);//trunk Ltree1
  rect(x+230+Rtree2mov,y+100+Rtree2mov,sizeX,sizeY);//trunk Rtree2
  rect(x-280-Ltree2mov,y+100+Ltree2mov,sizeX,sizeY);//trunk Ltree2
  fill(#669900);
  triangle(x+110+Rtree1mov,y-20+Rtree1mov,x+155+Rtree1mov,y-160+Rtree1mov,x+200+Rtree1mov,y-20+Rtree1mov);//leave Rtree1
  triangle(x-110-Ltree1mov,y-20+Ltree1mov,x-155-Ltree1mov,y-160+Ltree1mov,x-200-Ltree1mov,y-20+Ltree1mov);//leave Ltree1
  triangle(x+210+Rtree2mov,y+100+Rtree2mov,x+255+Rtree2mov,y-60+Rtree2mov,x+300+Rtree2mov,y+100+Rtree2mov);//leave  Rtree2
  triangle(x-210-Ltree2mov,y+100+Ltree2mov,x-255-Ltree2mov,y-60+Ltree2mov,x-300-Ltree2mov,y+100+Ltree2mov);//leave Ltree2
}