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

Lab 5 - Convert a number from base 10 to base 2

def convert_10_to_2():
    num = 29
    keep = ""
    r = 0
    while (num>0):
        r = num % 2
        num = num // 2
        keep = str(r) + keep
    print (keep)

convert_10_to_2()    
    

วันพุธที่ 23 กันยายน พ.ศ. 2558

Lab 5 - Decrease values in array (by percentage)

def decrease_percent(a):
    n = a
    i = 0
    dec_per = 0.5 # 50/100
    print("Number increase",dec_per*100,"%")
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] - n[i]*dec_per
        print("Decrease Array n[",i,"] =",n[i])
        print()
        i = i + 1

decrease_percent([5, 6, 7, 8])




Lab 5 - Increase values in array (by percentage)

def increase_percent(a):
    n = a
    i = 0
    inc_per = 0.5
    print("Number increase",inc_per*100,"%")
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] + n[i]*inc_per
        print("Increase Array n[",i,"] =",n[i])
        print()
        i = i + 1

increase_percent([5, 6, 7, 8])


Lab 5 - Decrease values in array (by fixed value)

def decrease_array(a):
    n = a
    i = 0
    dec = 5
    print("Decrease Number is",dec)
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] - dec
        print("Decrease Array n[",i,"] =",n[i])
        print()
        i = i + 1

decrease_array([5, 6, 7, 8])



Lab 5 - Increase values in array (by fixed value)

def increase_array(a):
    n = a
    i = 0
    inc = 5
    print("Increase Number is",inc)
    while(i < len(n)):
        print("Original Array n[",i,"] =",n[i])
        n[i] = n[i] + inc
        print("Increase Array n[",i,"] =",n[i])
        print()
        i = i + 1

increase_array([5, 6, 7, 8])


Lab 5 - Find/count number of positive values in array

def find_count_pos(a):
    n = a
    i = 0
    suma = 0
    x = 0     #amount of positve numer
    while(i < len(n)):
        if(n[i] > 0):
            suma = suma + n[ i ]
            print("Index of Positive number = n[", i ,"]")
            x = x + 1
        i = i + 1        
    print("Summary of Positive number is =", suma)
    print("Amount of Positive number is =", x)
   
find_count_pos([-5, 4, 3, 2, 1, -6, -3])


Lab 5 - Find sum of positive values in array

def pos_sum(a):
    n = a
    i = 0
    suma = 0
    while(i < len(n)):
        if(n[i] > 0):
            suma = suma + n[ i ]
        i = i + 1        
    print("Summary of Positive number is =", suma)
   
pos_sum([-5, 4, 3, 2, 1, -6, -3])


Lab 5 - Find average of values in array

def average_sum(a):
    n = a
    i = 0
    suma = 0
    while(i < len(n)):
         suma = suma + n[ i ]
         i = i + 1
    print("Summary =", suma)
    print("Amount of array =", len(n))
    suma = suma/len(n)
    print("Average of summary =", suma)

average_sum([5, 4, 3, 2, 1, 0, 6, 3])


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

Lab 5 - Find index of (the last) minimum value in array

def lastmin_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,21,900,21]
   i = 0
   maxnum = A[0]

   while(i<len(A)):
        if(maxnum >= A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")

lastmin_array()



Lab 5 - Find index of (the first) minimum value in array

def firstmin_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900,900,899]
   i = 0
   maxnum = A[0]
 
   while(i<len(A)):
        if(maxnum > A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
firstmin_array()


Lab 5 - Find index of (the last) maximum value in array

def lastmax_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900,900,899]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum <= A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
lastmax_array()


Lab 5 - Find index of (the first) maximum value in array

def firstmax_array():
   A = [23,22,89,24,45,26,72,81,89,55,900,889,225,900]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum < A[i]):
           maxnum = A[i]
           x = i
        i = i+1
   print(maxnum,"A[",x,"]")
 
firstmax_array()

Lab 5 - The minimum value in array

def min_array():
   A = [23,22,53,24,45,26,72,81,89,55]
   i = 0
   minnum = A[0]
 
   while(i<len(A)):
        if(minnum > A[i]):
           minnum = A[i]
        i = i+1
   print(minnum)
   
         
min_array()



Lab 5 - The maximum value in array

def max_array():
   A = [1,2,3,4,5,6,7,8,9,10]
   i = 0
   maxnum = 0
 
   while(i<len(A)):
        if(maxnum < A[i]):
           maxnum = A[i]
        i = i+1
   print(maxnum)
         
max_array()


Lab 5 - Display elements of array and its index

def run_array():
   A = [1,2,3,4,5,6,7,8,9,10]
   i = 0
 
   while(i<len(A)):
      print("A[",i,"] is",A[i])
      i = i + 1
   print ("Amount of Index's Array is ",len(A))
   
run_array()


Lab 5 - Find sum of values in array

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

setup()

Lab 4x - (Python) Summary of Integers

>>> def setup():
count = 1
n = 10
nsum = 0
while (count <= n):
nsum = nsum + count
count = count + 1
print("summation=",nsum)


>>> setup()
summation= 1
summation= 3
summation= 6
summation= 10
summation= 15
summation= 21
summation= 28
summation= 36
summation= 45
summation= 55
>>>

Lab 4x - (Python) Power of Ten

>>> def power_ten(num):
      if(num==6):
         print ("10 ^",num,"is Million")
      if(num==9):
         print ("10 ^",num,"is Billion")
      if(num==12):
         print ("10 ^",num,"is Trillion")
      if(num==15):
         print ("10 ^",num,"is Quadrillion")
      if(num==18):
         print ("10 ^",num,"is Quintillion")
      if(num==21):
         print ("10 ^",num,"is Sextillion")
      if(num==30):
         print ("10 ^",num,"is Nonillion")
      if(num==100):
         print ("10 ^",num,"is Googol")

       
>>> power_ten (9)
10 ^ 9 is Billion
>>>


Lab 4x - (Python) BMI Calculator

>>> def cal_bmi(weight,height):
   bmi = weight/((height/100)**2)
   print("Youe BMI is ",bmi)

 
>>> cal_bmi(62,173)
Youe BMI is  20.715693808680545


Lab 4x - (Python) Grade

>>> def setup():
  score = 78
  print("Your score was =",score)
  if score<101and score>=80:
    print("A")
  if score<80and score>=70:
    print("B")
  if score<70and score>=60:
    print("C")
  if score<60and score>=50:
    print("D")
  if score<50:
    print("F")

 
>>> setup()
Your score was = 78
B
>>>


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

Lab 4x - (Python) Mutiple Table

def setup():
    print (multi(7, 12))


def multi(n , row):
    i=0
    while (i <= row):
        result =  n * i
        print( n,"*",i,"=",result)
    i = i + 1

setup()

Lab 4x - (Python) Summary of Integers

def setup():
print (cal_sum(10))
print (cal_sum(15))


def cal_sum (i):
a = 0
x = 1
while (x<=i):
a = a+x
x = x+1
print("Sum 1 to",i,"=",a)


setup()


Lab 4x - (Python) Test

def setup():
  num = 0
  numl = 8
while (num<=numl):
print(num)
num = num + 1

setup()


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

Lab 4 - Prime

void setup() {
  size(250, 150);
  background(50);
  int minNumber = 0;
  int maxNumber = 20;
  int sum = 0;

  int space = 30;
  textAlign(CENTER);
  text("Sum of Prime Number", width/2, height/2-space);
  text(minNumber+" - "+maxNumber, width/2, height/2);
  while (minNumber <= maxNumber) {
    if (prime(minNumber)) {
      sum += minNumber;
    }
    minNumber++;
  }
  text("Sum = "+sum, width/2, height/2+space);
}
boolean prime(int value) {
  int num = 2;
  while (num <= value) {
    if (value%num == 0) {
      if (value == num) {
        return true;
      } else {
        return false;
      }
    }
    num++;
  }
  return false;
}

Lab 4 - Loan Payment

void setup() {
  size(450, 300);
  background(0);
  float loanAmount = 10000;
  float interestPercent = 12;
  int loanTerm = 12;
  loan(loanAmount, interestPercent, loanTerm);
}

void loan(float loanAmount, float interestPercent, int loanTerm) {
  int space = 15;
  text("Loan Amount = $"+nf(loanAmount, 1, 2), 10, space);
  text("Loan Term = "+loanTerm+" months", 10, space*3);
  text("Interest Rate = "+interestPercent+"%", 10, space*5);
  head(30, space*7);

  float interestRate = interestPercent/100;
  float effectiveInterest = interestRate/12;
  float monthlyPayment = loanAmount*(effectiveInterest/(1-pow(1+effectiveInterest, -loanTerm)));

  float interest = 0;
  float totalInterest = 0;
  float principal = 0;
  float balance = loanAmount;
  int term = 1;
  while (term <= loanTerm) {
    interest = balance * effectiveInterest;
    principal = monthlyPayment-interest;
    totalInterest += interest;
    balance -= principal;
    if (balance <= 0) {
      balance = 0;
    }
    showInfo(term, balance, principal, interest, totalInterest, 30, space*(7+term));
    term++;
  }
}

void showInfo(int term, float balance, float principal, float interest, float totalInterest, int x, int y) {
  int space = 80;
  fill(#FFFF00);
  text(term, x, y);
  text("$"+nf(principal, 1, 2), x+space, y);
  text("$"+nf(interest, 1, 2), x+space*2, y);
  text("$"+nf(balance, 1, 2), x+space*3, y);  text("$"+nf(totalInterest, 1, 2), x+space*4, y);
}

void head(int x, int y) {
  int space = 80;
  fill(#FF0000);
  text("Payment No.", x, y);
  text("Principal", x+space, y);
  text("Interest", x+space*2, y);
  text("Balance", x+space*3, y);  text("Total Interest", x+space*4, y);
}

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

Lab 4 - Balloon

void setup() {
  size (400, 600);
}
void draw() {
  background(255);
  int x = 200;
  int y = mouseY;

  if (y < 100) {
      y = 100;
      fill(#00AA00);
  } else {
      fill(255);
    if (y>500) {
      y=500;
      fill(#0000AA);
    }
  }
 
   draw_balloon(x, y);
   draw_balloon(x+100, y);
   draw_balloon(x-100, y);
}
void draw_balloon(int x, int y) {
  int sized = 100;
  int string_length = 150;

  line(x, y, x, y + string_length);
  ellipse(x, y, sized, sized);

}

Lab 4 - MultipleTable

void setup() {
  multi(7, 12);
}
  int i=0;
void multi(int n, int row) {
  while (i<=row) {
    int result =  n * i ;
    println( n,"*",i,"=",result) ;
    i += 1 ;
  }
}

Lab 4 - (Exercise) Countdown

void setup(){
  cal_num(10);
}
void cal_num (int i){
 
    while(i>0){
      int x=i;
      while (x<=i && x!=0){
        print(x,"");
      x--;
    }
      i--;
      println();
  }
}

Lab 4 - Summary of Integers

void setup(){
  cal_sum(10);
  cal_sum(15);
}
void cal_sum (int i){
  int a=0;
  int x=1;

    while (x<=i){
      a=a+x;
      x++;
    }println("Sum 1 to",i,"=",a);
}

** ERROR **

เมื่อพิมพ์ println("Sum 1 to",i,"=",a); ใน Processing
โปรแกรมจะแสดงผลอกมาเป็น Sum 1 to (i) = (a)
แต่เมื่อนำมาลงบล็อกแล้ว ต้องเปลี่ยนเครื่องหมายจาก
" , " เป็น " + " แทน
เพราะในบล็อกจะแสดงผลแค่ Sum 1 to
 

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

Lab 3 - Service Charge

int space = 50;
void setup() {
  size(650, 300);
  background(#000000);
  int x =50, y =50;

  int packageType = 2;
  int serviceType = 2; 
  float weight = 7.99;

  fill(#00FFFF);
  textSize(40);
  text("Expressimo Delivery Service", x, y);
  charge(x+space, y+space, packageType, serviceType, weight);
}

void charge(int x, int y, int package_t, int service_t, float weight) {
  float cost = 0;
  fill(#00FF00);
  textSize(25);
  if (package_t == 1) {
    text("Package Type : LETTER", x, y);
  } else if (package_t == 2) {
    text("Package Type  : BOX", x, y);
  } else {
    text("Package Type  : ERROR", x, y);
  }

  if (service_t == 1) {
    text("Service Type  : Next day Priority", x, y+space);
  } else if (service_t == 2) {
    text("Service Type  : Next day Standard", x, y+space);
  } else if (service_t == 3) {
    text("Service Type  : Two-days", x, y+space);
  } else {
    text("Service : ERROR", x, y+space);
  }

  if (package_t == 1) {
    text("Weight : "+weight+" Oz", x, y+space*2);
    if (service_t == 1 && weight <= 8) {
      cost = 12;
    }
    if (service_t == 2 && weight <= 8) {
      cost = 10.5;
    }
    if (service_t >= 3 || weight > 8) {
      cost = 0;
      text("Charge : $"+cost+" (No Services)", x, y+space*3);
    }
  }
  if (package_t == 2) {
    text("Weight : "+weight+" Pound", x, y+space*2);
    if (service_t == 1) {
      if (weight <= 1) cost = 15.75;
      else if (weight > 1) {
        cost = cal_service1(weight);
      }
    } else if (service_t == 2) {
      if (weight <= 1) cost = 13.75;
      else if (weight > 1) {
        cost = cal_service2(weight);
      }
    } else if (service_t == 3) {
      if (weight <= 1) cost = 7.00;
      else if (weight > 1) {
        cost = cal_service3(weight);
      }
    }
  }
  text("Charge : $"+cost, x, y+space*3);
}

float cal_service1(float weight) {
  float priority_charge;
  float firstCharge = 15.75;
  priority_charge = firstCharge + ((weight-1)*1.25);
  return priority_charge;
}

float cal_service2(float weight) {
  float standard_charge;
  float firstCharge = 13.75;
  standard_charge = firstCharge + ((weight-1)*1);
  return standard_charge;
}

float cal_service3(float weight) {
  float twodays_charge;
  float firstCharge = 7.00;
  twodays_charge = firstCharge + ((weight-1)*0.5);
  return twodays_charge;
}

Lab 3 - Syntax Error

Syntax Error
    ปีกนกยืดตามเมาส์เนื่องจากไม่ได้กำหนดค่าตัวแปรให้


Lab 3 - Book

int value = 0;

void setup(){
size (800,600);
background(value);

}
void draw() {
  //Center Alphabet
  fill(value,value,0);
  stroke(value,value,0);
  rect(360,200,80,160);
  rect(310,240,180,80);
  quad(280,440,310,360,360,360,360,440);
  arc(360,360,160,160,0,HALF_PI);

  // Left Alphabet

  fill(value,0,value);
  stroke(value,0,value);
  ellipse(580,280,160,160);
  rect(660,240,80,120);
  quad(500,440,530,360,660,360,660,440);
  arc(660,360,160,160,0,HALF_PI);

  // Right Alphabet
  fill(0,value,value);
  stroke(0,value,value);
  arc(220,320,160,160, PI+PI/2, TWO_PI);
  arc(220,360,160,160,0,HALF_PI);
  rect(220,320,80,40);
  rect(110,240,110,80);
  quad(80,440,110,360,220,360,220,440);

  // Text
   textSize(40);
  text("Detective Conan",30,220);
}
void mousePressed() {
  if (value == 0) {
    value = 255;
  } else {
    value = 0;
  }
}

Lab 3 - Power of Ten


void setup() {
  size (400, 400);
  background (255);
  fill(255,0,0);
  textSize(30);
  textAlign(CENTER);
  text("Power of Ten",width/2,50);
  
}

void draw() {
  power_ten(6);
  power_ten(5);
  power_ten(12);
  power_ten(15);
  power_ten(18);
  power_ten(21);
  power_ten(30);
  power_ten(100);
  
  
}

void power_ten(int x){
  
  fill(0);
  textSize(15);
  if(x==6||x==9||x==12||x==15||x==18||x==21||x==30||x==100){
    if(x==6){
    text("10^"+x+"= Million",width/2,height/2-70);
    }
    if(x==9){
    text("10^"+x+"= Billion",width/2,height/2-50);
    }
    if(x==12){
    text("10^"+x+"= Trillion",width/2,height/2-30);
    } 
    if(x==15){
    text("10^"+x+"= Quadrillion",width/2,height/2-10);
    }
    if(x==18){
    text("10^"+x+"= Quintillion",width/2,height/2+10);
    }
    if(x==21){
    text("10^"+x+"= Sextillion",width/2,height/2+30);
    }
    if(x==30){
    text("10^"+x+"= Nonillion",width/2,height/2+50);
    }
    if(x==100){
    text("10^"+x+"= Googol",width/2,height/2+70);
    }
  } else {
    
    textAlign(CENTER);
    textSize(50);
    
    text("ERROR!!!",width/2,height/2);
  }
  
}