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

Report Rasberry Pi.

1.วิธีเขียนโค้ดเหมือนไพท้อนทุกประการ แต่คอมช้ามาก เนตก็ล่มบ่อยมากๆ
2.AS1 รันบนเว็บได้ แต่เข้าหน้าแอดมินไม่ได้
3.Animation เป็นเรื่องเกี่ยวกับ เสือมาเจอเด็กผู้หญิงยืนเต้นอยู่บนแม่น้ำ แล้วก็ตนใจว่าตัวเองกำลังเดินอยู่บนน้ำ จึงรีบวิ่งออกไป

Rasberry Pi.

class student : def __init__(self,name,id,score): self.name = name self.id = id self.score = score def display(self) : print "Name =>" , self.name , " ID => " , self.id , " Score => %3s" % self.score def get_sc(self): return self.score def set_sc(self,va): self.score = va def getGrade(score): if(score >=80): return "A" elif(score >=70): return "B" elif(score >=60): return "C" elif(score >=50): return "D" else: return "F" def count_G(score): gr = [0,0,0,0,0] for i in range (0,len(score),1): if getGrade(score[i].get_sc()) == "A" : gr[0] += 1 elif getGrade(score[i].get_sc()) == "B" : gr[1] += 1 elif getGrade(score[i].get_sc()) == "C" : gr[2] += 1 elif getGrade(score[i].get_sc()) == "D" : gr[3] += 1 elif getGrade(score[i].get_sc()) == "F" : gr[4] += 1 return gr; def C_grade (grade,a): gr = count_G(a) if grade == 'A' : print "Grade " , grade , " = ",gr[0] , "Person" elif grade == 'B' : print "Grade " , grade , " = ",gr[1] , "Person" elif grade == 'C' : print "Grade " , grade , " = ",gr[2] , "Person" elif grade == 'D' : print "Grade " , grade , " = ",gr[3] , "Person" elif grade == 'F' : print "Grade " , grade , " = ",gr[4] , "Person" def s_a_g (a,i): print" Grade = " ,getGrade(a[i].get_sc()) def main(): a = [student("eiei", 01, 100 ), student("kiki", 02, 79 ), student("kuku", 03, 65 ), student("mimi", 04, 51 ), student("mumu", 05, 40 ), student("jiji", 11, 100 ), student("juju", 12, 95 ), student("nini", 13, 98 ), student("nunu", 14, 97 ), student("ahah", 15, 99 )] for ei in range (0,len(a),1): a[ei].display() s_a_g (a,ei) count_G(a) C_grade ('A', a) C_grade ('F', a) main()

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

Inheritance.

def setup():
   a = Students("Gun", 26, 70, 1.74, 30024)
   b = Students("Rit", 25, 60, 1.70, 30076)
   c = Students("Frame", 19, 58.2, 1.73, 30058)
   d = Students("Boat", 16, 65, 1.68, 30020)
   BMI(a)

class Person:
   def __init__(self,name,age,widht,height):
      self.name = name
      self.age = age
      self.widht = widht
      self.height = height
     
   def get_name(self):
      return self.name
 
   def get_age(self):
      return self.age
 
   def get_widht(self):
      return self.widht
 
   def get_height(self):
      return self.height
 
class Students(Person):
   def __init__(self,name,age,widht,height,ID):
      super().__init__(name,age,widht,height)
      self.ID = ID
     
   def get_ID(self):
      return self.ID

def BMI(data_per):
   BMI = data_per.get_widht()/(data_per.get_height()*data_per.get_height())
   print(BMI)
 
setup()

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

Vector.

class Vector:
   def __init__(self, i, j, k):
      self.i = i
      self.j = j
      self.k = k
   
   def print(self):
      print(self.i,"i","+",self.j,"j","+",self.k,"k")
         
   def add(self,b):
      x = self.i + b.i
      y = self.j + b.j
      z = self.k + b.k
      add = Vector(x, y, z)
      return add
   

def setup():
   a = Vector(5,3,4)
   b = Vector(2,1,6)
   c = a.add(b)

   a.print()
   b.print()
   c.print()
 
   assert c.x == 7 and c.y == 4 and c.z == 10
setup()

Fraction.

class Fraction:
   def __init__(self,numer,deno):
      self.numer = numer
      self.deno = deno
   
   def print(self):
      print(self.numer,end="/")
      print(self.deno,end="")
      print()
         
   def add(self,b):
      o = (self.numer*b.deno)+(b.numer*self.deno)
      u = (self.deno*b.deno)
      add = Fraction(o,u)
      return add
   

def setup():
   a = Fraction(5,3)
   b = Fraction(2,-1)
   c = a.add(b)

   a.print()
   b.print()
   c.print()

   assert c.numer == 1 and c.deno == -3
setup()

Complex.

class Complex:
   def __init__(self,real,im):
      self.real = real
      self.im = im
 
   def print(self):
      i = self.im
      if i > 0:
         print(self.real,end="+")
      else:
         print(self.real,end=" ")
      print(self.im,end="i")
      print()
       
   def add(self,b):
      r = self.real + b.real
      ic = self.im + b.im
      add = Complex(r,ic)
      return add
 

def setup():
   a = Complex(5,3)
   b = Complex(2,-1)
   c = a.add(b)

   a.print()
   b.print()
   c.print()

   assert c.real == 7 and c.im == 2
setup()

Create class Banner for printing a banner.

public class Banner{
    private String word;
    private char font;
 
    public Banner(String word, char font){
      this.word = word;
      this.font = font;
   }
   
    public void display(){
       String B1 = " ##### ";
       String B2 = " #     #   ";
       String B3 = " ##### ";
       String B4 = " #     #   ";
       String B5 = " ##### ";
       String [] B = {B1,B2,B3,B4,B5};
       String L1 = " #          ";
       String L2 = " #          ";
       String L3 = " #          ";
       String L4 = " #          ";
       String L5 = " ##### ";
       String [] L = {L1,L2,L3,L4,L5};
       String U1 = " #        #  ";
       String U2 = " #        #  ";
       String U3 = " #        #  ";
       String U4 = " #        #  ";
       String U5 = "  #### ";
       String [] U = {U1,U2,U3,U4,U5};
       String E1 = " ######  ";
       String E2 = " #              ";
       String E3 = " ######  ";
       String E4 = " #              ";
       String E5 = " ######  ";
       String [] E = {E1,E2,E3,E4,E5};
   
       int iw = 0;
         B = apply_font(B,'#',this.font);
         L = apply_font(L,'#',this.font);
         U = apply_font(U,'#',this.font);
         E = apply_font(E,'#',this.font);
       while(iw < 5){
          int ia = 0 ;        
          while(ia < this.word.length()){
             if(word.charAt(ia) == 'B'){
                System.out.print(B[iw]);
             }else if(word.charAt(ia) == 'L'){
                System.out.print(L[iw]);
             }else if(word.charAt(ia) == 'U'){
                System.out.print(U[iw]);
             }else if(word.charAt(ia) == 'E'){
                System.out.print(E[iw]);
             }ia = ia + 1;
          }System.out.println();
           iw = iw + 1;
       }
    }

      public String[] apply_font(String [] w,char f,char r){
      int i = 0;
      String[] font = new String[w.length]; //String that has length as  w
      while(i < w.length){
        int j = 0;
        font[i]="";
           while(j < w[i].length()){
             if (w[i].charAt(j) == f){
                font[i] = font[i] + r;
            }else{
                font[i] = font[i] + ' ';
            }j += 1;
           }
            i += 1;
       }
        return font;
     }
           
public static void main(String[] args){
    String word = "BLUE";
   Banner B = new Banner(word,'+');
    B.display();

    }
}

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

Display student records, sorted by age, use insertion sort.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

  public int get_height(){
        return this.height;
  }
 
   public void set_name(String name){
      this.name = name;
   }
 
   public void set_ID(int ID){
      this.ID = ID;
   }
 
   public void set_age(int age){
      this.age = age;
   }
 
   public void set_weight(int weight){
      this.weight = weight;
   }
 
   public void set_height(int height){
      this.height = 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();
  }

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};
    sort(data);
   
    a.display();
    b.display();
    c.display();
    d.display();
    e.display();
   
  }

   public static void sort(Student [] data){
      int i = 1;
      while(i < data.length){
      int value_a = data[i].get_age();
      String value_n = data[i].get_name();
      int value_I = data[i].get_ID();
      int value_w = data[i].get_weight();
      int value_h = data[i].get_height();
      int j = i;
         while(j > 0 && data[j-1].get_age() > value_a){
         data[j].set_age(data[j-1].get_age());
         data[j].set_name(data[j-1].get_name());
         data[j].set_ID(data[j-1].get_ID());
         data[j].set_weight(data[j-1].get_weight());
         data[j].set_height(data[j-1].get_height());
            j = j - 1;
      }
      data[j].set_age(value_a);
      data[j].set_name(value_n);
      data[j].set_ID(value_I);
      data[j].set_weight(value_w);
      data[j].set_height(value_h);
      i = i + 1;
   }
}
}

Find/count number of students with weight < 50.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};  
    find(data);  
  }

   public static void find(Student [] data){
      int i = 0;
      int count = 0;
      while(i < data.length){
         if(data[i].get_weight() < 50){
           count = count + 1;
      }
     i = i + 1;
   }
      System.out.println("Have " + count + " students");
}
}

Find minimum weight of students.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};  
    find(data);  
  }

   public static void find(Student [] data){
      int i = 0;
      int min = data[i].get_weight();
      int index = 0;
      while(i < data.length){
         if(min > data[i].get_weight()){
            min = data[i].get_weight();
            index = i;
      }
     i = i + 1;
   }
      System.out.println("Min is " + data[index].get_name() + " weight = " + min);
}
}

Find/count number of students with age < 30.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};
    count(data);
  }

   public static void count(Student [] data){
      int i = 0;
      int count = 0;
      while(i < data.length){
         if(data[i].get_age() < 30){
         count = count + 1;
         
      }
          i = i + 1;
   
   }
       System.out.println("Have " + count + " students");
}
}

Find average age of students.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};  
    average(data);  
  }

   public static void average(Student [] data){
      int i = 0;
      int total = 0;
      int n = 0;
      float average = 0;
      while(i < data.length){
        total = total + data[i].get_age();
        n = n + 1;
        average = total/n;
        i += 1 ;      
      }
     System.out.println("average age = " + average);
   }
}

Find/count&display number of students with BMI > 25.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};  
    BMI(data);  
  }

   public static void BMI(Student [] data){
      int i = 0;
      float BMI = 0;
      int count = 0;
      while(i < data.length){
        BMI = data[i].get_weight()*100*100/((data[i].get_height()*(data[i].get_height())));
         if(BMI > 25){
            System.out.println(data[i].get_name() +" BMI value = " +BMI);
            count = count + 1;
         }
       
         i += 1 ;
       
      }
     
        System.out.println("Have" + count + "Students");
     
   }
}

Find student BMI.

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 String get_name(){
        return this.name;
  }

  public int get_ID(){
        return this.ID;
  }

  public int get_age(){
        return this.age;
  }

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

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

  public static void main(String[] args){
    Student a = new Student("Gun" , 32075, 26, 80, 174);
    Student b = new Student("Rit" , 32078, 25, 70, 170);
    Student c = new Student("Frame" , 32080, 19, 58, 173);
    Student d = new Student("Coffee"  , 32057, 18, 50, 168);
    Student e = new Student("Boat" , 32087, 16, 60, 165);
 
    Student [] data = {a,b,c,d,e};  
    BMI(data);  
  }

   public static void BMI(Student [] data){
      int i = 0;
      float BMI = 0;
      while(i < data.length){
        BMI = data[i].get_weight()*100*100/((data[i].get_height()*(data[i].get_height())));
        System.out.println(data[i].get_name() +" BMI value = " +BMI);
        i += 1 ;
      }
   }
}

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

Display each student record.

public class Students {
   private String name;
   private int ID;
   private int age;
   private int weight;
   private int height;
 
   public Students(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 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();
   }
 
   public static void main(String[] args) {
      Students a = new Students ("Gun", 32075, 26, 70, 174);
      Students b = new Students ("Rit", 32078, 25, 60, 170);
      Students c = new Students ("Frame", 32080, 19, 58, 173);
      Students d = new Students ("Boat", 32057, 16, 65, 168);
     
      Students [] data = {a, b, c, d};
      display(data);
   }
 
   public static void display(Students [] data){
      int i = 0;
      while(i < data.length){
        data[i].display();
        i = i + 1 ;
      }
   }
}

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

Create class Banner for printing a banner.

class Alphabet:
    def __init__(self,line1,line2,line3,line4,line5):
        self.line1 = line1
        self.line2 = line2
        self.line3 = line3
        self.line4 = line4
        self.line5 = line5
       
    def display_alphabet_L1(self):
        print(self.line1,end = "")
    def display_alphabet_L2(self):
        print(self.line2,end = "")
    def display_alphabet_L3(self):
        print(self.line3,end = "")
    def display_alphabet_L4(self):
        print(self.line4,end = "")
    def display_alphabet_L5(self):
        print(self.line5,end = "")
                       
    def get_alphabet_L1(self):
        return self.line1
    def get_alphabet_L2(self):
        return self.line2  
    def get_alphabet_L3(self):
        return self.line3
    def get_alphabet_L4(self):
        return self.line4
    def get_alphabet_L5(self):
        return self.line5
                       
    def print_star_L1(self):
        hastag_to_star(self.line1)
    def print_star_L2(self):
        hastag_to_star(self.line2)
    def print_star_L3(self):
        hastag_to_star(self.line3)
    def print_star_L4(self):
        hastag_to_star(self.line4)
    def print_star_L5(self):  
        hastag_to_star(self.line5)
     
def hastag_to_star(temp):
    i = 0
    while(i < len(temp)):
        if(temp[i]  == "#"):
            print("*",end = "")
        else:
            print(" ",end = "")
        i += 1

def draw_alphabet(alphabet_group,word):    
    ip = 0
    while(ip < 5):
        iw = 0
        while(iw < len(word)):
            if(word[iw] == "B"):
                print_func(alphabet_group,ip,0)
            elif(word[iw] == "L"):
                print_func(alphabet_group,ip,1)
            elif(word[iw] == "U"):
                print_func(alphabet_group,ip,2)
            elif(word[iw] == "E"):
                print_func(alphabet_group,ip,3)
            iw += 1
        print()
        ip += 1

def print_func(alphabet_group,ip,alphabet_index):
    if(ip == 0):
        alphabet_group[alphabet_index].print_star_L1()
    elif(ip == 1):
        alphabet_group[alphabet_index].print_star_L2()
    elif(ip == 2):
        alphabet_group[alphabet_index].print_star_L3()
    elif(ip == 3):
        alphabet_group[alphabet_index].print_star_L4()
    elif(ip == 4):
        alphabet_group[alphabet_index].print_star_L5()

def setup():
    B = Alphabet(" #####  ",
                 " #    # ",
                 " #####  ",
                 " #    # ",
                 " #####  " )
    L = Alphabet(" #      ",
                 " #      ",
                 " #      ",
                 " #      ",
                 " ###### " )
    U = Alphabet(" #    # ",
                 " #    # ",
                 " #    #  ",
                 " #    # ",
                 "  ####  " )
    E = Alphabet(" ###### ",
                 " #      ",
                 "###### ",
                 " #      ",
                 " ###### " )
    alphabet_group = [B,L,U,E]
    word = input("")
    draw_alphabet(alphabet_group,word)
                                 


setup()

Display student records, sorted by age, use insertion sort.

class Students:
   def __init__(self,name,ID,age,weight,height):
      self.name = name
      self.ID = ID
      self.age = age
      self.weight = weight
      self.height = height
   
   def display(self):
      print("Name:",self.name)
      print("ID:",self.ID)
      print("Age:",self.age)
      print("Weight:",self.weight)
      print("Height:",self.height)
      print()
     
   def get_name(self):
      return self.name
   def get_ID(self):
      return self.ID
   def get_age(self):
      return self.age
   def get_weight(self):
      return self.weight
   def get_height(self):
      return self.height
 
   def set_name(self,name):
      self.name = name
   def set_ID(self,ID):
      self.ID = ID
   def set_age(self,age):
      self.age = age
   def set_weight(self,weight):
      self.weight = weight
   def set_height(self,height):
      self.height = height
   
def setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 48, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   sort(nukrean)
   a.display()
   b.display()
   c.display()
   d.display()

def sort(nukrean):
   i = 1
   while(i < len(nukrean)):
      value_a = nukrean[i].get_age()
      value_n = nukrean[i].get_name()
      value_I = nukrean[i].get_ID()
      value_w = nukrean[i].get_weight()
      value_h = nukrean[i].get_height()
      j = i
      while(j>0 and nukrean[j-1].get_age() > value_a):
         nukrean[j].set_age(nukrean[j-1].get_age())
         nukrean[j].set_name(nukrean[j-1].get_name())
         nukrean[j].set_ID(nukrean[j-1].get_ID())
         nukrean[j].set_weight(nukrean[j-1].get_weight())
         nukrean[j].set_height(nukrean[j-1].get_height())
         j = j - 1
      nukrean[j].set_age(value_a)
      nukrean[j].set_name(value_n)
      nukrean[j].set_ID(value_I)
      nukrean[j].set_weight(value_w)
      nukrean[j].set_height(value_h)
      i += 1
 
setup()

Find/count number of students with weight < 50.

class Students:
   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_name(self):
      return self.name
   def get_weight(self):
      return self.weight
   
def setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 48, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   find(nukrean)
 
def find(nukrean):
   i = 0
   count = 0
   while(i < len(nukrean)):
      if(nukrean[i].get_weight() < 50):
         print(nukrean[i].get_name(),"weight = ",nukrean[i].get_weight())
         count = count + 1
      i = i + 1
   print("Have",count,"Students")
 
setup()

Find minimum weight of students.

class Students:
   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 setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   minimum(nukrean)
 
def minimum(nukrean):
   i = 0
   min = nukrean[i].get_weight()
   while(i < len(nukrean)):
      if(nukrean[i].get_weight() < min):
         min = nukrean[i].get_weight()
      i = i + 1
   print("Minimum is",min)
 
setup()

Find/count number of students with age < 30.

class Students:
   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_age(self):
      return self.age
   
def setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   count(nukrean)
 
def count(nukrean):
   i = 0
   count = 0
   while(i < len(nukrean)):
      if(nukrean[i].get_age() < 30):
         count = count + 1
      i = i + 1
   print("Have",count,"Students")
 
setup()

Find average age of students.

class Students:
   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_age(self):
      return self.age
     
def setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   average(nukrean)
 
def average(nukrean):
   i = 0
   count = 0
   sum = 0
   a = 0
   while(i < len(nukrean)):
      sum = sum + nukrean[i].get_age()
      count = count + 1
      a = sum/count
      i = i + 1
   print("Average = ",a)
 
setup()

Find/count&display number of students with BMI > 25.

class Students:
   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_name(self):
      return self.name
   def get_weight(self):
      return self.weight
   def get_height(self):
      return self.height
   
def setup():
   a = Students("Gun", 32075, 26, 90, 174)
   b = Students("Rit", 32078, 25, 80, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 70, 168)
   nukrean = [a, b, c, d]
   BMI(nukrean)
 
def BMI(nukrean):
   i = 0
   count = 0
   while(i < len(nukrean)):
      BMI = nukrean[i].get_weight()/pow((nukrean[i].get_height()/100),2)
      if(BMI > 25):
         print(nukrean[i].get_name()," = ","%.3f"%BMI)
         count = count + 1
      i = i + 1
   print("Have",count,"Students")
   
 
setup()

Find student BMI.

class Students:
   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_name(self):
      return self.name
   def get_weight(self):
      return self.weight
   def get_height(self):
      return self.height
     
def setup():
   a = Students("Gun", 32075, 26, 70, 174)
   b = Students("Rit", 32078, 25, 60, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 65, 168)
   nukrean = [a, b, c, d]
   BMI(nukrean)
 
def BMI(nukrean):
   i = 0
   while(i < len(nukrean)):
      BMI = nukrean[i].get_weight()/pow((nukrean[i].get_height()/100),2)
      print(nukrean[i].get_name()," = ","%.3f"%BMI)
      i = i + 1
     
 
setup()

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

Display each student record.

class Students:
   def __init__(self,name,ID,age,weight,height):
      self.name = name
      self.ID = ID
      self.age = age
      self.weight = weight
      self.height = 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():
   a = Students("Gun", 32075, 26, 70, 174)
   b = Students("Rit", 32078, 25, 60, 170)
   c = Students("Frame", 32080, 19, 58.2, 173)
   d = Students("Boat", 32057, 16, 65, 168)
     
   a.display()
   print()
   b.display()
   print()
   c.display()
   print()
   d.display()
 
setup()

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

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

def setup():
    word = input("")
    draw(word)

def draw(word):
    Bl1 = " #####  "
    Bl2 = " #    # "
    Bl3 = " #####  "
    Bl4 = " #    # "
    Bl5 = " #####  "
    B = [Bl1,Bl2,Bl3,Bl4,Bl5]
    Ll1 = " #      "
    Ll2 = " #      "
    Ll3 = " #      "
    Ll4 = " #      "
    Ll5 = " ###### "
    L = [Ll1,Ll2,Ll3,Ll4,Ll5]
    Ul1 = " #    # "
    Ul2 = " #    # "
    Ul3 = " #    # "
    Ul4 = " #    # "
    Ul5 = "  ####  "
    U = [Ul1,Ul2,Ul3,Ul4,Ul5]
    El1 = " ###### "
    El2 = " #      "
    El3 = " ###### "
    El4 = " #      "
    El5 = " ###### "
    E = [El1,El2,El3,El4,El5]
    ip = 0
    while(ip < 5):
        iw = 0
        while(iw < len(word)):
            if(word[iw] == "B"):
                print(B[ip],end = "")
            elif(word[iw] == "L"):
                print(L[ip],end = "")
            elif(word[iw] == "U"):
                print(U[ip],end = "")
            elif(word[iw] == "E"):
                print(E[ip],end = "")
            iw += 1
        print()  
        ip += 1  
                                     
setup()

Transpose a square matrix in place without creating a new matrix.

def setup():
   rowone = [41, 22, 83]
   rowtwo = [12, 62, 72]
   rowthree = [38, 15, 98]
   maxtrix= [rowone, rowtwo, rowthree]
   dis(maxtrix)
   tran(maxtrix)
   print()
   print("After")
   print()
   dis(maxtrix)

def dis(maxtrix):
   i = 0
   while(i < len(maxtrix)):
      j = 0
      print("|",end = "")
      while(j < len(maxtrix[i])):
          print(" ",maxtrix[i][j]," ",end  = "")
          j += 1
      print("|")
      i +=1
 
def tran(maxtrix):
   i = 0
   while(i < len(maxtrix)):
      j = i
      while(j < len(maxtrix[i])):
         temp = maxtrix[i][j]
         maxtrix[i][j] = maxtrix[j][i]
         maxtrix[j][i] = temp
         j = j + 1
      i = i + 1
         
setup()

Multiply two matrices.

def setup():
   rowone = [1, 2, 3]
   rowtwo = [2, 3, 4]
   rowthree = [3, 4, 5]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [4, 5, 6]
   rowfive = [5, 6, 7]
   rowsix = [6, 7, 8]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   mul(maxtrixone,maxtrixtwo)
 
def mul(maxtrixone,maxtrixtwo):
   i = 0
   ii = 0
   ki = 0
   while(ii < len(maxtrixone)):
      j = 0
      ij = 0
      kj = 0
      print("|", end = "")
      while(ij < len(maxtrixtwo[i])):
         tempone = maxtrixone[i+ki][j]*maxtrixtwo[i][j+kj]
         temptwo = maxtrixone[i+ki][j+1]*maxtrixtwo[i+1][j+kj]
         tempthree = maxtrixone[i+ki][j+2]*maxtrixtwo[i+2][j+kj]
         print(" ",tempone+temptwo+tempthree,end = " ")
         kj += 1
         ij += 1
      print(" |")  
      ki += 1  
      ii += 1      
         

setup()

Subtract two matrices.

def setup():
   rowone = [1, 2, 3]
   rowtwo = [2, 3, 4]
   rowthree = [3, 4, 5]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [4, 5, 6]
   rowfive = [5, 6, 7]
   rowsix = [6, 7, 8]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   subtwo(maxtrixone,maxtrixtwo)
 
def subtwo(maxtrixone,maxtrixtwo):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j]-maxtrixtwo[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
     
setup()

Add two matrices.

def setup():
   rowone = [1, 2, 3]
   rowtwo = [2, 3, 4]
   rowthree = [3, 4, 5]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [4, 5, 6]
   rowfive = [5, 6, 7]
   rowsix = [6, 7, 8]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   addtwo(maxtrixone,maxtrixtwo)
 
def addtwo(maxtrixone,maxtrixtwo):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j]+maxtrixtwo[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
     
setup()

Display the matrix.

def setup():
   rowone = [1, 2, 3]
   rowtwo = [2, 3, 4]
   rowthree = [3, 4, 5]
   maxtrixone = [rowone, rowtwo, rowthree]
   rowfour = [4, 5, 6]
   rowfive = [5, 6, 7]
   rowsix = [6, 7, 8]
   maxtrixtwo = [rowfour, rowfive, rowsix]
   display(maxtrixone)
   print()
   display(maxtrixtwo)
 
def display(maxtrixone):
   i = 0
   while(i < len(maxtrixone)):
      j = 0
      print("|", end = "")
      while(j < len(maxtrixone[i])):
         print("",maxtrixone[i][j],"",end = "")
         j = j + 1
      print("|")
      i = i + 1
     
setup()

Find indices of the room(s) with maximum number of chairs (in the building).

def setup():
   one = [12, 24, 26, 28, 32]
   two = [14, 27, 29, 31, 34]
   three = [25, 26, 27, 36, 39]
   four = [16, 18, 15, 14, 13]
   b = [one, two, three, four]
   find(b)

def find(b):
    i_f = 0
    value = b[0][0]
    while(i_f < len(b)):
        i_r = 0
        while(i_r < len(b[i_f])):
            if(b[i_f][i_r] > value):
                value = b[i_f][i_r]              
            i_r += 1
        i_f += 1
    i_f = 0  
    while(i_f < len(b)):
        i_r = 0
        while(i_r < len(b[i_f])):
           if( value == b[i_f][i_r]):
               print("Indices is"," [",i_f,"][",i_r,"] ",end = "")
           i_r += 1
        i_f += 1  

setup()

Display student records, sorted by age, use insertion sort.

def setup():
   name = ["Gun","Rit","Frame","Coffee","Ice","Boat"]
   ID = [32075,32078,32080,32057,32087,33653]
   age = [26,25,19,17,18,16]
   weight = [80,75,58.2,60,65,55]
   height = [174,170,173,165,168,163]
   i = 0
   sort(name,ID,age,weight,height)
   while(i<len(weight)):
      display(name,ID,age,weight,height,i)
      i += 1

def display(n,I,a,w,h,i):  
      print(n[i])
      print("ID =",I[i])
      print("age =",a[i])
      print("weight =",w[i])
      print("height =",h[i])
      print()    
   
def sort(n,I,a,w,h):
   i = 1
   while(i<len(a)):
      value_a = a[i]
      value_n = n[i]
      value_I = I[i]
      value_w = w[i]
      value_h = h[i]
      j = i
      while(j>0 and a[j-1] > value_a):
         a[j] = a[j-1]
         n[j] = n[j-1]
         I[j] = I[j-1]
         w[j] = w[j-1]
         h[j] = h[j-1]
         j = j - 1
      a[j] = value_a
      n[j] = value_n
      I[j] = value_I
      w[j] = value_w
      h[j] = value_h
      i += 1
   
setup()

Find index of the floor(s) with maximum number of chairs (in the building).

def setup():
   one = [12, 24, 26, 28, 32]
   two = [14, 27, 29, 31, 34]
   three = [25, 26, 27, 36, 39]
   four = [16, 18, 15, 14, 13]
   b = [one, two, three, four]
   find(b)
 
def find(b):
   i_f = 0
   total = 0
   value = 0
   imax = 0
   while(i_f < len(b)):
      i_r = 0
      while(i_r < len(b[i_f])):
         total = total + b[i_f][i_r]
         i_r = i_r +1
      if(total > value):
         value = total
         imax = i_f
      i_f = i_f + 1
   print("Floor is",imax)
 
setup()

Find total number of chairs (in the building).

def setup():
   one = [12, 24, 26, 28, 32]
   two = [14, 27, 29, 31, 34]
   three = [25, 26, 27, 36, 39]
   four = [16, 18, 15, 14, 13]
   b = [one, two, three, four]
   total(b)
 
def total(b):
   i_f = 0
   total = 0
   while(i_f < len(b)):
      i_r = 0
      while(i_r < len(b[i_f])):
         total = total + b[i_f][i_r]
         i_r = i_r +1
      i_f = i_f + 1
   print("Total is",total)
 
setup()

Display student records with weight < 50.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [38, 83, 19]
   weight = [80, 45, 50.2]
   height = [1.74, 1.70, 1.73]
   find(name, weight)

def find(n, w):
   i = 0
   while(i < len(w)):
      if(w[i] < 50):
         print(n[i], "weight =", w[i])
      i = i + 1

setup()

Find/count number of students with weight < 50.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [38, 83, 19]
   weight = [80, 45, 50.2]
   height = [1.74, 1.70, 1.73]
   print(count(weight))
 
def count(w):
   i = 0
   count = 0
   while(i < len(w)):
      if(w[i] < 50):
         count = count + 1
      i = i + 1
   return count

setup()

Find minimum weight of students.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [38, 83, 19]
   weight = [80, 75, 58.2]
   height = [1.74, 1.70, 1.73]
   min(name, weight)

def min(n, w):
   i = 0
   namemin = 0
   min = w[i]
   while(i < len(w)):
      if(min > w[i]):
         min = w[i]
         namemin = n[i]
      i = i + 1
   print(namemin,"is a minimum weight =",min)
 
setup()

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

Find/count number of students with age < 30.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [38, 83, 19]
   weight = [80, 75, 58.2]
   height = [1.74, 1.70, 1.73]
   print(count(age))
 
def count(age):
   i = 0
   count = 0
   while(i < len(age)):
      if(age[i] < 30):
         count = count + 1
      i = i + 1
   return count

setup()

Find average age of students.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [25, 25, 19]
   weight = [80, 75, 58.2]
   height = [1.74, 1.70, 1.73]
   print(average(age))
 
def average(age):
   i = 0
   sum = 0
   n = 0
   average = 0
   while(i < len(age)):
      sum = sum + age[i]
      n = n + 1
      average = sum/n
      i = i + 1
   return average

setup()

Display student records with BMI > 25.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [25, 25, 19]
   weight = [80, 75, 58.2]
   height = [1.74, 1.70, 1.73]
   find(name, weight, height)
 
def find(n, w, h):
   i = 0
   count = 0
   while(i < len(n)):
      BMI = w[i]/(h[i]*h[i])
      if(BMI > 25):
         print(n[i], "weight", w[i], "height", h[i], "BMI =", "%.2f"%BMI)
      i = i + 1
     
setup()

Find/count number of students with BMI > 25.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [25, 25, 19]
   weight = [80, 75, 58.2]
   height = [1.74, 1.70, 1.73]
   print(count(name, weight, height))
 
def count(n, w, h):
   i = 0
   count = 0
   while(i < len(n)):
      BMI = w[i]/(h[i]*h[i])
      if(BMI > 25):
         count = count + 1
      i = i + 1
   return count
     
setup()

Find student BMI.

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [25, 25, 19]
   weight = [62, 55, 58.2]
   height = [1.74, 1.70, 1.73]
   BMI(name, weight, height)
 
def BMI(n, w, h):
   i = 0
   while(i < len(n)):
      BMI = w[i]/(h[i]*h[i])
      print(n[i],"BMI =","%.3f"%BMI)
      i = i + 1

setup()

Display student records (data).

def setup():
   name = ["Gun", "Rit", "Frame"]
   ID = [32075, 32078, 32080]
   age = [25, 25, 19]
   weight = [62, 55, 58.2]
   height = [1.74, 1.70, 1.73]
   array(name, ID, age, weight, height)
 
def array(n, I, a, w, h):
   i = 0
   while(i < len(n)):
      print(n[i], "ID =", I[i], "age =", a[i], "weight =", w[i], "height =", h[i])
      i = i + 1
   return array

setup()

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

Syntax Error for Lab 5.

1.ใส่ชื่อตัวแปรไม่ตรงกัน เช่น

def setup():
   string = 'thailand'
   lasttring = 'and' <<<<<<<<<<<<< ผิดตรงนี้ไงครับ laststring สิ ไม่ใช่ lasttring อิอิ
   assert my_endswith(string, laststring) == True

setup()

วิธีแก้คือ แก้ไขชื่อให้ตรงกัน โปรแกรมเราก็จะสามารถรันได้ตามปกติ

2.ลืมส่งค่ากลับ เช่น

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1

โปรแกรมก็ไม่สามารถรู้ผลสิ วิธีแก้คือ ใส่ return result ไปซะ ดังนี้

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result <<<<<<<<<<<<<<<<<<<<<< นี้ไง ใส่มาแล้ว แค่นี้เราก็จะสามารถรันโปรแกรมได้ตามปกติ

3.ลืมอัพเดทค่า

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]

   return wordwithoutstrip แบบนี้เราก็จะรันโปรแกรมไม่ได้นะครับ วิธีแก้คือ

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]
      i = i+1 <<<<<<<<<<<<<<<<<<< ก็อัพเดทค่ามันซะสิ เราก็จะสามารถรันโปรแกรมได้ตามปกติ
   return wordwithoutstrip

my_endswith.

def my_endswith(string, laststring):
   result = False
   i = 0
   if (len(string) > len(laststring)):
      while (i < len(laststring)):
         if (string[i+(len(string)-len(laststring))] == laststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result

def setup():
   string = 'thailand'
   laststring = 'and'
   assert my_endswith(string, laststring) == True

setup()

my_startswith.

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result

def setup():
   string = 'thailand'
   firststring = 'sun'
   assert my_startswith(string, firststring) == False

setup()

my_strip.

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]
      i = i+1
   return wordwithoutstrip

def setup():
   string = 'thailand    '
   strip = '        '
   print("world is", my_strip(string, strip))
   assert my_strip(string, strip) == 'thailand'

setup()

my_replace.

def my_replace(string, replace):
   new = ''
   i = 0
   while (i < len(string)):
      if (i < len(replace)):
         new = new + replace[i]
      elif (i >= len(replace)):
         new = new + string[i]
      i = i+1
   return new

def setup():
   string = 'thailand'
   replace = 'blue'
   print("Old world is", string, "and New world is", my_replace(string, replace))
   assert my_replace(string, replace) == 'blueland'

setup()

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

my_find.

def setup():
      my_find = "thailand"
      i = 0
      indexi = i
      while (i < len(my_find)):
         if ("i" == my_find[i]):
            indexi = i
         i += 1
      print(indexi)
      assert (indexi == 3)
setup()

my_count.

def setup():
      my_count = "thailand"
      i = 0
      count = 0
      check = True
      while (i < len(my_count)):
         if ("a" == my_count[i]):
            check = True
            count += 1
         else:
            check = False
         i += 1
      print(count)
      assert (count == 2)
setup()
     

convert a number from base 10 to base 2.

def setup():
      baseten = 3
      basetwo = ""
      while (baseten > 0):
         if (baseten % 2 == 0):
            basetwo = "0" + basetwo
            baseten = baseten/2
         elif (baseten % 2 == 1):
            basetwo = "1" + basetwo
            baseten = (baseten/2) - 0.5
      print(basetwo)

setup()
     

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

Increase/decrease values in array.

def new(array):
   i = 0
   sum = 0
   new = 0
   while(i < len(array)):
      new = array[i] + 2
      sum += new
      i += 1
      print("new value index ", i, "is ", new)
   print("sum = ", sum)
 
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   new(array)
 
setup()

Find average of values in array.

def average(array):
   i = 0
   sum = 0
   n = 0
   average =0
   while(i < len(array)):
      sum += array[i]
      i += 1
      n += 1
   average = sum/n
   print("average of values in this array is ", average)

def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   average(array)

setup()

Find/count number of positive values in array.

def countpositive(array):
   i = 0
   positive = 0
   while(i < len(array)):
      if(array[i] > 0):
         positive += 1
      i += 1
   print("number of positive have ", positive, " number")
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   countpositive(array)
 
setup()

Find sum of positive values in array.

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

def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   print("sum positive = ",sumpositive(array))
 
setup()

Find sum of values in array.

def sumnumber(a):
   i = 0
   sum = 0
   while(i < len(a)):
      sum = sum + a[i]
      i += 1
   return sum
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   sumnumber(array)
   print("sum number = ", sumnumber(array))
 
setup()

Find index of (the last) maximum value in array.

def maximumfirst(array):
   i = 6
   maximum = array[i]
   indexmax = i
   while(i >= 0):
      if(maximum < array[i]):
         maximum = array[i]
         indexmax = i
      i -= 1
   print("Maximum = ", maximum)
   print("index of maximum (the last) = ", indexmax)    
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximumfirst(array)
 
setup()

Find index of (the first) maximum value in array.

def maximumfirst(array):
   i = 0
   maximum = array[i]
   indexmax = i
   while(i < len(array)):
      if(maximum < array[i]):
         maximum = array[i]
         indexmax = i
      i += 1
   print("Maximum = ", maximum)
   print("index of maximum (the first) = ", indexmax)    
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximumfirst(array)
 
setup()

Find the maximum value in array.

def maximum(array):
   i = 0
   maximum = array[i]
 
   while(i < len(array)):
      if(maximum < array[i]):
         maximum = array[i]
       
      i += 1
   print("Maximum = ", maximum)
     
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximum(array)
 
setup()

Display elements (value) of array and its index.

def printArray(array):
   i = 0
   while( i < len(array)):
      print("index", i, "value", array[i])
      i += 1  
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   printArray(array)
 
setup()

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

Loan money Lab4x.

def setup():
  loan_money(5000,0.12,1)
def loan_money(amount,rate,year):
  month = year*12
  ratepermonth = rate/12
  paypermonth = amount*(ratepermonth/(1-pow(1+ratepermonth,-month)))
  principal = paypermonth
  unpaid = amount
  interest_total = 0
  x = 1
  print("Num.  Interest  Principal  Unpaid  Interest total")
  while(x <= month):
    print(max(x, 2))
    print("     %.2f"%max(ratepermonth*unpaid, 2, 2), end = "")
    interest_total+=ratepermonth*unpaid
    principal=paypermonth-(ratepermonth*unpaid)
    unpaid -= principal
    if(unpaid < 0):
      unpaid = 0
    print("     %.2f"%max(principal, 3, 2), end = "")
    print("    %.2f"%max(unpaid, 4, 2), end = "")
    print("    %.2f "%max(interest_total, 3, 2), end = "")
    print()
    x += 1
 
setup()

Leap years Lab4x.

year = 2038
def setup():
   if (year%4==0):
      print(year,"          This year is leap year")
   elif (year % 4 != 0):
      print(year,"          This year isn't leap year")
   elif (year % 4 == 0 and year % 100 != 0):
      print(year,"          This year is leap year")
   elif (year % 4 == 0 and year % 100 == 0 and year % 400 != 0):
      print(year,"          This year isn't leap year")
   elif (year % 4 == 0 and year % 100 == 0 and year % 400 == 0):
      print(year,"          This year is leap year")

setup()

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

Prime number Lab4x.

def setup():
   print(sum_prime(10))

def sum_prime(n):
   x = 2
   sum = 0
   while (x <= n):
      if (prime(x)):
         sum += x
      x += 1
   
   return sum


def prime(p):
   x = 2
   if (p==1):
         return False
   while (x<p):
      if (p%x == 0):
         return False
      x += 1
   return True

setup()

Number sum Lab4x.

def setup():
  print(sum_number(10))

def sum_number(n):
   x = 0
   sum = 0
   while (x <= n):
      sum += x
      x +=1
   return sum

setup()

Link Video Presentation Resort Managemant System Project.

Video Presentation Resort Managemant System Project. จัดทำโดย พากษ์เสียง: คุณาสิน  ทองมณี  5801012620011 ลำดับภาพ: สุพิชชา  ศรีศิริ...