Saturday, December 5, 2015

Tuesday, November 24, 2015

Lab x RaspberryPi - GradeCalculate

class Student:
    def __init__(self,name,id,score):
        self.name = name
        self.id = id
        self.score = score
    def display(self):
        print('Name :',self.name)
        print('ID :',self.id)
        print('Score :',self.score)
    def get_score(self):
        return self.score

def setup():
    a = Student('Ant',1,80)
    b = Student('Bird',2,75)
    c = Student('Cat',3,59)
    d = Student('Dog',4,15)
    e = Student('Elephant',5,62)
    data = [a,b,c,d,e]
    show_allgrade(data)

def find_grade(data):
 
    if(80 <= data.get_score()):
        return 'A'
    elif(70 <= data.get_score()):
        return 'B'
    elif(60 <= data.get_score()):
        return 'C'
    elif(50 <= data.get_score()):
        return 'D'
    else:
        return 'F'

def count_grade(c,data):
    i=0
    count=0
    while(i < len(data)):
        if(find_grade(data[i]) == c):
            count+=1
        i+=1
    return count

def show_allgrade(data):
    i=0
    while(i < len(data)):
        data[i].display()
        print('Grade :',find_grade(data[i]))
        print()
        i+=1
 
setup()
         
     
     
     

Sunday, November 1, 2015

Lab8 - Display student records with weight < 50

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int i=0;
    while (i<s.length) {
      if(50 > s[i].display_weight()) {
        System.out.println( "Name : "+s[i].display_name() );
      System.out.println( "StudentNumber : " +s[i].display_studentNumber() );
      System.out.println( "Age : "+s[i].display_age() );
      System.out.println( "Weight : "+s[i].display_weight() );
      System.out.println( "Height : "+s[i].display_height() );
      System.out.println( "" );
      }
     i=i+1;
    }
  }
}

Lab8 - Find/count number of students with weight < 50

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int count=0;
    int i=0;
    while (i<s.length) {
      if(50 > s[i].display_weight()) {
        count = count+1;
      }
     i=i+1;
    }
  System.out.println( "Number of students with weight < 50 is "+count );
  }
}

Lab8 - Find minimum weight of students

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int i=0;
    float reference=s[i].display_weight();
    while (i<s.length) {
      if( reference>s[i].display_weight() ) {
        reference=s[i].display_weight();
      }
      i=i+1;
    }
  System.out.println( "Minimum weight of students is "+reference );
  }
}

Lab8 - Find/count number of students with age < 30

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int count=0;
    int i=0;
    while (i<s.length) {
      if(s[i].display_age()<30) {
        count = count+1;
      }
      i=i+1;
    }
  System.out.println( "Number of students with age < 30 is "+count );
  }
}

Lab8 - Find average age of students

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    float sum=0;
    int i=0;
    while (i<s.length) {
     sum = sum+s[i].display_age();
     i=i+1;
    }
  sum = sum/s.length;
  System.out.println( "Average age of students is "+sum );
  }
}

Lab8 - Display student records with BMI > 25

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int count=0;
    float bmi;
    int i=0;
    while (i<s.length) {
      bmi=s[i].display_weight()/((s[i].display_height()/100)*(s[i].display_height()/100));
      if(bmi>25) {
      System.out.println( "Name : "+s[i].display_name() );
      System.out.println( "StudentNumber : " +s[i].display_studentNumber() );
      System.out.println( "Age : "+s[i].display_age() );
      System.out.println( "Weight : "+s[i].display_weight() );
      System.out.println( "Height : "+s[i].display_height() );
      System.out.println("Your BMI is "+bmi);
      System.out.println( "" );
      }
      i=i+1;
    }
  }
}

Lab8 - Find/count number of students with BMI > 25

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 72, 165 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 69, 166 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int count=0;
    float bmi;
    int i=0;
    while (i<s.length) {
      bmi=s[i].display_weight()/((s[i].display_height()/100)*(s[i].display_height()/100));
      if(bmi > 25) {
        count=count+1;
      }
      i=i+1;
    }
  System.out.println( "Number of students with BMI > 25 is "+count );
  }
}

Lab8 - Find student BMI

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public float display_weight() {
    return this.weight;
  }
 
  public float display_height() {
    return this.height;
  }
 
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 65, 170 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 57, 167 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    float bmi;
    int i=0;
    while (i<s.length) {
      System.out.println( "Name : "+s[i].display_name() );
      System.out.println( "StudentNumber : " +s[i].display_studentNumber() );
      System.out.println( "Age : "+s[i].display_age() );
      System.out.println( "Weight : "+s[i].display_weight() );
      System.out.println( "Height : "+s[i].display_height() );
      bmi=s[i].display_weight()/((s[i].display_height()/100)*(s[i].display_height()/100));
      System.out.println("Your BMI is "+bmi);
      System.out.println( "" );
      i=i+1;
    }
  }
}

Lab8 - Display each student record

public class Student {
  private String name;
  private int studentNumber;
  private int age;
  private int weight;
  private int height;
 
  public Student(String name, int studentNumber, int age, int weight, int height) {
    this.name = name;
    this.studentNumber = studentNumber;
    this.age = age;
    this.weight = weight;
    this.height = height;
  }
 
  public String display_name() {
    return this.name;
  }
 
  public int display_studentNumber() {
  return this.studentNumber ;
  }
 
  public int display_age(){
    return this.age;
  }
 
  public int display_weight() {
    return this.weight;
  }
 
  public int display_height() {
    return this.height;
  }
 
  public static void main(String[] args) {
    Student[] stu =  { new Student("Bird", 33, 22, 65, 170 ),
    new Student("Bryant", 24, 21, 58, 167 ),
    new Student("Paul", 3, 17, 57, 167 ),
    new Student("James", 23, 19, 62, 172 ),
        new Student("Nowitzki", 34, 21, 66, 177 ) };
    System.out.println ( " ##### Student Record #####" );
    display_data(stu) ;
  }
 
  public static void display_data(Student[] s) {
    int i=0;
    while (i<s.length) {
      System.out.println( "Name : "+s[i].display_name() );
      System.out.println( "StudentNumber : " +s[i].display_studentNumber() );
      System.out.println( "Age : "+s[i].display_age() );
      System.out.println( "Weight : "+s[i].display_weight() );
      System.out.println( "Height : "+s[i].display_height() );
      System.out.println( "" );
      i=i+1;
    }
  }
}

Lab7 - Display student records with weight < 50

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 display_name(self):
        return self.name
       
    def display_id(self):
        return self.id 
       
    def display_age(self):
        return self.age
       
    def display_weight(self):
        return self.weight
       
    def display_height(self): 
        return self.height

      

def setup():

    a = Student( "Antony" , 11 , 22 , 58 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 48 , 162 )

    d = Student( "Bank" , 30 , 22 , 57 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    find_minimum(data)


def find_minimum(data):
   
    i=0
   
    while(i < len(data)):
       
        if(50 > data[i].display_weight()):
       
            print("Name : ",data[i].display_name())
       
            print("ID : ",data[i].display_id())
       
            print("Age : ",data[i].display_age())
       
            print("Weight : ",data[i].display_weight())
       
            print("Height : ",data[i].display_height())
           
        i+=1  

setup()

Lab7 - Find/count number of students with weight < 50

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 display_name(self):
        return self.name
       
    def display_id(self):
        return self.id 
       
    def display_age(self):
        return self.age
       
    def display_weight(self):
        return self.weight
       
    def display_height(self): 
        return self.height

      

def setup():

    a = Student( "Antony" , 11 , 22 , 58 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 71 , 162 )

    d = Student( "Bank" , 30 , 22 , 57 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    find_minimum(data)


def find_minimum(data):
   
    i=0
   
    count=0
   
    while(i < len(data)):
       
        if(50 > data[i].display_weight()):
           
            count+=1
           
        i+=1
       
    print(" Number of students with weight < 50 is",count)   


setup()

Lab7 - Find minimum weight of students

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 display_name(self):
        return self.name
       
    def display_id(self):
        return self.id 
       
    def display_age(self):
        return self.age
       
    def display_weight(self):
        return self.weight
       
    def display_height(self): 
        return self.height

      

def setup():

    a = Student( "Antony" , 11 , 22 , 58 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 71 , 162 )

    d = Student( "Bank" , 30 , 22 , 57 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    find_minimum(data)


def find_minimum(data):
   
    i=0
   
    reference=data[i].display_weight()
   
    while(i < len(data)):
       
        if(reference>data[i].display_weight()):
           
            reference=data[i].display_weight()
           
        i+=1
       
    print("Minimum weight of students is ",reference)   


setup()

Lab7 - Find/count number of students with age < 30

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 display_name(self):
        return self.name
       
    def display_id(self):
        return self.id 
       
    def display_age(self):
        return self.age
       
    def display_weight(self):
        return self.weight
       
    def display_height(self): 
        return self.height

      

def setup():

    a = Student( "Antony" , 11 , 22 , 71 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 58 , 162 )

    d = Student( "Bank" , 30 , 22 , 60 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    average_age(data)


def average_age(data):
   
    i = 0
   
    count = 0
   
    while(i < len(data)):
       
        if(data[i].display_age() < 30):
           
            count+=1
           
        i+=1
       
    print("Number of students with age < 30 is ",count)    


setup()

Lab7 - Find average age of students

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 display_name(self):
        return self.name
       
    def display_id(self):
        return self.id 
       
    def display_age(self):
        return self.age
       
    def display_weight(self):
        return self.weight
       
    def display_height(self): 
        return self.height

      

def setup():

    a = Student( "Antony" , 11 , 22 , 71 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 58 , 162 )

    d = Student( "Bank" , 30 , 22 , 60 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    average_age(data)


def average_age(data):
   
    i = 0
   
    avg = 0
   
    while(i < len(data)):
       
        avg+=data[i].display_age()
       
        i+=1
       
    avg/=len(data)
   
    avg*=100
   
    avg=round(avg)
   
    avg/=100
   
    print("Average age of students is ",avg)    


setup()

Lab7 - Display student records with BMI > 25

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 bmi(self):
       
       
        bmi=self.weight/((self.height/100)*(self.height/100))
       
        if(bmi>25):
           
            print("Name : ",self.name)

            print("Weight : ",self.weight)

            print("Height : ",self.height)
           
            print("Your BMI is" ,bmi)

      

def setup():

    a = Student( "Antony" , 11 , 22 , 71 , 165 )

    b = Student( "Mike" , 12 , 23 , 66 , 171 )

    c = Student( "Jacky" , 23 , 21 , 58 , 162 )

    d = Student( "Bank" , 30 , 22 , 60 , 177 )

    e = Student( "Billy" , 1 , 20 , 62 , 180 )

    f = Student( "Biccy" , 3 , 24 , 62 , 181 )

    data = [a,b,c,d,e,f]

    bmi(data)


def bmi(data):

    i=0

    print("#### BMI ####")

    while(i < len(data)):

        data[i].bmi()
       
        print("")

        i+=1


setup()

Lab7 - Find student BMI

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 display(self):
        print("Name : ",self.name)
        print("Weight : ",self.weight)
        print("Height : ",self.height)
     
    def bmi(self):
        bmi=self.weight/((self.height/100)*(self.height/100))
        print("Your BMI is" ,bmi)
     
def setup():
    a = Student( "Antony" , 11 , 22 , 65 , 165 )
    b = Student( "Mike" , 12 , 23 , 66 , 171 )
    c = Student( "Jacky" , 23 , 21 , 58 , 162 )
    d = Student( "Bank" , 30 , 22 , 60 , 177 )
    e = Student( "Billy" , 1 , 20 , 62 , 180 )
    f = Student( "Biccy" , 3 , 24 , 62 , 181 )
    data = [a,b,c,d,e,f]
    bmi(data)

def bmi(data):
    i=0
    print("#### BMI ####")
    while(i < len(data)):
        data[i].display()
        data[i].bmi()
        print("")
        i+=1

setup()

Sunday, October 25, 2015

Lab7 - Display each student record

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 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 = Student( "Antony" , 11 , 22 , 65 , 165 )
    b = Student( "Mike" , 12 , 23 , 66 , 171 )
    c = Student( "Jacky" , 23 , 21 , 58 , 162 )
    d = Student( "Bank" , 30 , 22 , 60 , 177 )
    e = Student( "Billy" , 1 , 20 , 62 , 180 )
    f = Student( "Biccy" , 3 , 24 , 62 , 181 )
    call = [a,b,c,d,e,f]
    record(call)

def record(call):
    i=0
    print("#### Student record ####")
    while(i < len(call)):
        call[i].display()
        print("")
        i+=1

setup()

Lab6 - Multiply two matrices

def setup():
    row1=[1,2,3]
    row2=[4,5,6]
    row3=[7,8,9]
    matrix1=[row1,row2,row3]
    row4=[9,8,7]
    row5=[6,5,4]
    row6=[3,2,1]
    matrix2=[row4,row5,row6]
    multiply_tow_matrix(matrix1,matrix2)
   
def multiply_tow_matrix(matrix1,matrix2):
    iRow=0
    iResultNow=0
    const1=0
    while(iResultNow < len(matrix1)):
        i=0
        iResult=0
        const2=0
        print("|",end = "")
        while(iResult < len(matrix2[iRow])):
            t1=matrix1[iRow+const1][i]*matrix2[iRow][i+const2]
            t2=matrix1[iRow+const1][i+1]*matrix2[iRow+1][i+const2]
            t3=matrix1[iRow+const1][i+2]*matrix2[iRow+2][i+const2]
            print(" ",t1+t2+t3,end = " ")
            const2+=1
            iResult+=1
        print("|")
        const1+=1
        iResultNow+=1
       
setup()

Lab6 - Subtract two matrices

def setup():
    row1=[1,2,3]
    row2=[4,5,6]
    row3=[7,8,9]
    matrix1=[row1,row2,row3]
    row4=[9,8,7]
    row5=[6,5,4]
    row6=[3,2,1]
    matrix2=[row4,row5,row6]
    subtract_tow_matrix(matrix1,matrix2)
   
def subtract_tow_matrix(matrix1,matrix2):
    iRow=0
    while(iRow < len(matrix1)):
        i=0
        print("|",end = "")
        while(i < len(matrix1[iRow])):
            print(" ",matrix1[iRow][i]-matrix2[iRow][i]," ",end  = "")
            i+=1
        print("|")
        iRow+=1
       
setup()

Lab6 - Add two matrices

def setup():
    row1=[1,2,3]
    row2=[4,5,6]
    row3=[7,8,9]
    matrix1=[row1,row2,row3]
    row4=[9,8,7]
    row5=[6,5,4]
    row6=[3,2,1]
    matrix2=[row4,row5,row6]
    plus_tow_matrix(matrix1,matrix2)
   
def plus_tow_matrix(matrix1,matrix2):
    iRow=0
    while(iRow < len(matrix1)):
        i=0
        print("|",end = "")
        while(i < len(matrix1[iRow])):
            print(" ",matrix1[iRow][i]+matrix2[iRow][i]," ",end  = "")
            i+=1
        print("|")
        iRow+=1
       
setup()

Lab6 - Display the matrix

def setup():
    row1=[1,2,3]
    row2=[4,5,6]
    row3=[7,8,9]
    matrix1=[row1,row2,row3]
    row4=[9,8,7]
    row5=[6,5,4]
    row6=[3,2,1]
    matrix2=[row4,row5,row6]
    display_matrix(matrix1)
    print()
    display_matrix(matrix2)
   
def display_matrix(matrix1):
    iRow=0
    while(iRow < len(matrix1)):
        i=0
        print("|",end = "")
        while(i < len(matrix1[iRow])):
            print(" ",matrix1[iRow][i]," ",end  = "")
            i+=1
        print("|")
        iRow+=1
       
setup()

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

def setup():
    floor1=[50,49,48,49,46,52,52]
    floor2=[49,51,50,52,47,48,47]
    floor3=[46,47,48,49,50,65,72]
    building=[floor1,floor2,floor3]
    find_chairs(building)

def find_chairs(building):
    iFloor=0
    total=0
    ref=building[0][0]
    while(iFloor < len(building)):
        iRoom=0
        while(iRoom < len(building[iFloor])):
            if(building[iFloor][iRoom]>ref):
                ref=building[iFloor][iRoom]
            iRoom+=1
        iFloor+=1
    iFloor=0
    print("Indices of rooms with maximum  chairs :",end = "")
    while(iFloor < len(building)):
        iRoom=0
        while(iRoom<len(building[iFloor])):
            if(ref==building[iFloor][iRoom]):
                print(" [",iFloor,"][",iRoom,"] ",end = "")
            iRoom+=1
        iFloor+=1 

setup()

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

def setup():
    floor1=[50,49,48,49,46,52,52]
    floor2=[49,51,50,52,47,48,47]
    floor3=[46,47,48,49,50,65,72]
    building=[floor1,floor2,floor3]
    find_chairs(building)

def find_chairs(building):
    iFloor=0
    total=0
    ref=0
    while(iFloor < len(building)):
        iRoom=0
        while(iRoom < len(building[iFloor])):
            total+=building[iFloor][iRoom]
            iRoom+=1
        if(total > ref):
            ref = total
            iMax = iFloor
        iFloor+=1
    print("Index of floor with maximum chairs is ",iMax)

setup()

Lab6 - Find total number of chairs (in the building)

def setup():
    floor1=[50,49,48,49,46,52,52]
    floor2=[49,51,50,52,47,48,47]
    floor3=[46,47,48,49,50,65,72]
    building=[floor1,floor2,floor3]
    find_chairs(building)

def find_chairs(building):
    i=0
    total=0
    while(i < len(building)):
        iRoom=0
        while(iRoom < len(building[i])):
            total+=building[i][iRoom]
            iRoom+=1
        i+=1
    print("Total number of chairs (in the building) : ",total)
    
setup()

Lab6 - Find minimum weight of students

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,49]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    find_weight(name,id,age,weight,height)

def find_weight(name,id,age,weight,height):
    i=0
    ref=weight[0]
    while(i < len(weight)):
        if(ref > weight[i]):
            ref = weight[i]
        i+=1
    print("Minimum weight of students : ",ref)
    
setup()

Lab6 - Display student records with weight < 50

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,49]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    find_weight(name,id,age,weight,height)

def find_weight(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("")
        i+=1
      
setup()

Lab6 - Find/count number of students with weight < 50

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,49]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    print("Number of students with weight < 50 : ",find_weight(weight))

def find_weight(weight):
    i=0
    j=0
    while(i < len(weight)):
        if(weight[i] < 50):
            j+=1
        i+=1
    return j
      
setup()

Lab6 - Display student records, sorted by age, use insertion sort

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [58, 62 ,60 ,72 ,55 ,55 ,50]
    height = [169, 170 ,172 ,188 ,165 ,158 ,160]
    display(name,id,age,height,weight)
   
def display(name,id,age,height,weight):
    index=0
    while(index<len(age)):
        index1=index+1
        while(index1<len(age)):
            if(age[index]>age[index1]):
                temp=age[index]
                age[index]=age[index1]
                age[index1]=temp
            index1+=1
        index+=1
    index=0
    print("-sorted by age-")
    print("")
    while(index<len(age)):
        index1=0
        stop = False
        while(index1<len(age) and (not stop)):
            if(age[index1]==age[index]):
                print("Name ",name[index])
                print("ID ",id[index])
                print("Age ",age[index])
                print("Weight ",weight[index])
                print("Height ",height[index])
                print("")
                stop=True
            index1+=1
        index+=1
       
setup()
           

Monday, October 19, 2015

Lab 6 - Find/count number of students with age < 30

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,50]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    print("Number of students with age < 30 : ",find_age(age))

def find_age(age):
    i=0
    j=0
    while(i < len(age)):
        if(age[i] < 30):
            j+=1
        i+=1
    return j
       
setup()

Lab 6 - Find average age of students

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,50]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    cal_average_age(age)

def cal_average_age(age):
    i=0
    summ=0
    while(i < len(age)):
        summ=summ+age[i]
        i+=1
    summ=summ/len(age)
    print("Average age of students : ",summ)
     
setup()

Lab 6 - Display student records with BMI > 25

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,50]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    (cal_bmi(name, id, age, weight, height))

def cal_bmi(name, id, age, weight, height):
    i=0
    while(i < len(weight)):
        bmi=weight[i]/((height[i]/100)*(height[i]/100))
        if(bmi > 25):
            print("Name ",name[i])
            print("ID ",id[i])
            print("Age ",age[i])
            print("Weight ",weight[i])
            print("Height ",height[i])
            print("")
        i+=1

setup()

Lab 6 - Find/count number of students with BMI > 25

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [69, 76 ,60 ,80 ,55 ,55 ,50]
    height = [165, 170 ,172 ,188 ,166 ,158 ,160]
    print("---Student records (data)---")
    print("")
    print("Number of students with BMI > 25 : ",cal_bmi(height,weight))

def cal_bmi(height,weight):
    i=0
    j=0
    while(i < len(weight)):
        bmi=weight[i]/((height[i]/100)*(height[i]/100))
        if(bmi > 25):
            j+=1
        i+=1
    return j

setup()

Lab 6 - Find student BMI

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [58, 62 ,60 ,72 ,55 ,55 ,50]
    height = [169, 170 ,172 ,188 ,165 ,158 ,160]
    print("--- Student student BMI ---")
    print("")
    cal_bmi(name,height,weight)

def cal_bmi(name,height,weight):
    i=0
    while(i < len(weight)):
        bmi=weight[i]/((height[i]/100)*(height[i]/100))
        print("Name ",name[i])
        print("Your Weight is" ,weight[i])
        print("Your Height is" ,height[i])
        print("Your BMI is" ,bmi)
        print("")
        i+=1

setup()

Lab 6 - Display student records (data)

def setup():
    name = ["Ant", "Bird" ,"Cat" ,"Dog" ,"Elephant" ,"Frog" ,"Giraffe"]
    id = [19, 23 ,10 ,13 ,22 ,25 ,17]
    age = [18 ,17 ,16 ,19 ,18 ,18 ,17]
    weight = [58, 62 ,60 ,72 ,55 ,55 ,50]
    height = [169, 170 ,172 ,188 ,165 ,158 ,160]
    print("---Student records (data)---")
    print("")
    display(name, id, age, weight, height)
 
def display(name, id, age, weight, height):
    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("")
        i+=1
 
setup()

Lab 4x Hello Python - Calculate body mass index (BMI) (function)

def setup():
    cal_bmi(61,165)

def cal_bmi(wit,hit):
        bmi=wit/((hit/100)*(hit/100))
        print("Your Weight is" ,wit)
        print("Your Height is" ,hit)
        print("Your BMI is" ,bmi)
        return bmi
 
setup()

Monday, October 5, 2015

Lab5 - my_strip() e.g. my_strip("Thailand ") -> return "Thailand"

def setup():
   string = "   Thailand"
   print(my_strip(string))
 
def my_strip(string):
   newString = ""
   i = 0
   while(i<len(string)):
        if(string[i] != " "):
            newString = newString + string[i]
        i+=1
       
   return newString
 
setup()

Lab5 - my_replace() e.g. my_replace("Thailand", "land", "thai") -> return "Thaithai"

def setup():
   string = "Thailand"
   reference = "land"
   replace = "thai"
   print(my_replace(string,reference,replace))
 
def my_replace(string,reference,replace):
   newString = ""
   i = 0
   iRef = 0
   iRep = 0
   while(i<len(string)):
      if(string[i]==reference[iRef]):
         a = 0
         while(a < len(reference)):        
            if(reference[iRef]==string[i+iRef]):
               iRef+=1
               a+=1
            else:
               newString = newString+string[i]

               a = len(reference)
            if(iRef==len(reference)):
               n = 0
               iRep = 0
               while(n < len(replace)):
                  newString = newString+replace[iRep]
                  iRep +=1
                  n+=1
               i +=len(reference)-1
               iRef = 0
      else :
         if(string[i] != " "):
             newString = newString + string[i]
      i+=1
   return newString
 
setup()

Sunday, October 4, 2015

Lab5 - my_endswith() e.g. my_endswith("Thailand", "and") -> return True

def setup():

    input_string = "Thailand"

    reference = "and"

    assert my_endswith(input_string,reference) == input_string.endswith(reference)

    print(my_endswith(input_string,reference))


def my_endswith(string,reference):

    index = len(string)-1
   
    referenceindex = len(reference)-1

    endswith = True
   
    while(index >= len(string) - len(reference) and endswith):

        if(string[index] == reference[referenceindex]):

            index-=1
           
            referenceindex-=1
       
            endswith = True
       
        else:
           
            endswith = False
           
    return endswith

setup()

Lab5 - my_startswith() e.g. my_startswith("Thailand", "Sun") -> return False

def setup():

    input_string = "Thailand"

    reference = "Sun"

    assert my_startswith(input_string,reference) ==input_string.startswith(reference)

    print(my_startswith(input_string,reference))

 

def my_startswith(string,reference):

    index = 0

    startswith = True
   
    while(index < len(string)and startswith):

        if(string[index] == reference[index]):

            index+=1
       
            startswith = True
       
        else:
           
            startswith = False
           
    return startswith

setup()

Lab5 - my_find() e.g. my_find("Thailand", "i") -> return 3

def setup():
    input_string = "Thailand"
    reference = "i"
    find_string = my_find(input_string,reference)
  
def my_find(string,reference):
    index = 0
    while(index < len(string)):
        if(string[index] == reference):
            print(index)
        index+=1

setup()

Monday, September 28, 2015

Lab5 - my_count() e.g. my_count("Thailand", "a") -> return 2

def setup():
    input_string = "Thailand"
    reference = "a"
    count_string = my_count(input_string,reference)
    print(count_string)
   
def my_count(string,reference):
    index = 0
    count = 0
    while(index < len(string)):
        if(string[index] == reference):
            count+=1
        index+=1
    return count

setup()

Lab5 - convert a number from base 10 to base 2

def setup():
    baseTwo=""
    baseTen=int(input())
    while(baseTen>0):
        module=baseTen%2
        baseTen=baseTen//2
        baseTwo = str(module)+baseTwo
   
    print("Base 10 to Base 2 =",baseTwo)
   
setup()

Sunday, September 27, 2015

Lab5 - Increase/decrease values in array (by fixed value or percentage)

def setup():

    n=[5,8,9,7,5,3,2,9]

    i=0

    percent=int(input())

    while(i<=len(n)):

        n[i]=n[i]+((percent*n[i])/100)
       
        print("Value of n[",i,"] is ",n[i])

        i+=1


setup()

Saturday, September 26, 2015

Lab5 - Find average of values in array

def find_avg(n):
    i=0
    sum=0
    count=0
    while(i<len(n)):
        sum=sum+n[i]
        i+=1
        count+=1
    sum=sum/count
   
    return sum
   
def setup():
     n=[5,8,9,7,5,3,2,9]
     print("Average of values in array",find_avg(n))
    
setup()

Lab5 - Find/count number of positive values in array

def count_pos(n):
    i=0
    count=0
    while(i<len(n)):
        if(n[i]>0):
            count+=1
            print("n[",i,"]","is",n[i])
        i+=1
    return count
   
def setup():
     n=[5,8,-9,7,5,3,-2,9]
     print("Number of positive values in array",count_pos(n))
    
setup()

Lab5 - Find sum of positive values in array

def find_sum(n):
    i=0
    sum=0
    while(i<len(n)):
        if(n[i]>0):
            sum=sum+n[i]
        i+=1
    return sum
   
def setup():
     n=[5,8,-9,7,5,3,-2,9]
     print("Sum of values in array",find_sum(n))
    
setup()

Lab5 - Find sum of values in array

def find_sum(n):
    i=0
    sum=0
    while(i<len(n)):
        sum=sum+n[i]
        i+=1
    return sum
   
def setup():
     n=[5,8,9,7,5,3,2,9]
     print("Sum of values in array",find_sum(n))
    
setup()

Lab5 - Find the maximum value in array

def find_max(n):
    i=0
    maximum=n[i]
    while(i<len(n)):
        if(maximum<n[i]):
            maximum=n[i]
        i+=1
     
    return maximum

def setup():
    n=[5,8,9,7,5,3,2,9]
    print("Maximum is ",find_max(n))
 
setup()

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

def find_max(n):
    i=0
    maximum=n[i]
    while(i<len(n)):
        if(maximum<n[i]):
            maximum=n[i]
        i+=1
     
    return maximum

def find_indexLast(n):

    i=len(n)-1
    maximum=find_max(n)
    while(i>=0):
        if(maximum==n[i]):
            index=i
            break
        i-=1
    return index
 
def setup():
    n=[5,8,9,7,5,3,2,9]
    print("Maximum is ",find_max(n))
    print("index of (the last) maximum value in array is ",find_indexLast(n))
 
setup()

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

def find_max(n):
    i=0
    maximum=n[i]
    while(i<len(n)):
        if(maximum<n[i]):
            maximum=n[i]
        i+=1
     
    return maximum

def find_indexf(n):
    i=0
    maximum=find_max(n)
    while(i<len(n)):
        if(maximum==n[i]):
            index=i
            i=len(n)
        i+=1
    return index
 
def setup():
    n=[5,8,9,7,5,3,2,9]
    print("Maximum is ",find_max(n))
    print("index of (the first) maximum value in array is ",find_indexf(n))
 
setup()

Lab5 - Display elements (value) of array and its index

def setup():
    n=[5,8,9,7,5,3,2,9]
    i=0
    print("Index is ",n[0])
    while(i<=len(n)):
        print("Value of n[",i,"] is ",n[i])
        i+=1

setup()

Saturday, September 19, 2015

Lab 4x Hello Python - Loan payment, see question 30 on page 370 show payment No. , interest, principal, unpaid balance, total interest to date (do not worry about the output format too much, e.g. how to change 1.234 to 1.23)

def setup():
    loan_amount=5000
    interestRate=12
    months=12
    cal_loan(5000,12,12)
   
def cal_loan(loan_amount, interestRate, months):
    n=1
    term=1
    total_interest=0
   
    effectiveInterest=interestRate/(100*12)
   
    monthlyPayment=loan_amount*(effectiveInterest/(1-pow(1+effectiveInterest,-months)))
   
    balance=loan_amount
    print("Loan Amount $ = ",loan_amount)
    print("Loan Term = ",months," month(s)")
    print("Interest Rate = ",interestRate,"%")
    print("Payment per months = ",monthlyPayment)
    print("Payment No. |  Interset  |  Principal  |  Unpaid Balance  |  Total Interest to Date")
   
    while(term<=months):
        interest = effectiveInterest*balance
        principal=monthlyPayment-interest
        balance=balance-principal
        total_interest=total_interest+interest
        print("     ",term,"        $",interest,"        $",principal,"    $",balance,"           $",total_interest)
        term=term+1
  
setup()

Lab 4x Hello Python - Calculate sum of prime numbers from 1 to N, (create boolean function?)

def setup():
    sum_prime_number(10)
   
def sum_prime_number(numbers_end):
    numbers=2
    sum=0
    n=1
    print("Prime number is ",end="")
    while(numbers<=numbers_end):
        reference=2
        prime_numbers=True
        while(reference<numbers and prime_numbers):
            if(numbers%reference==0):
                prime_numbers=False
            reference=reference+1
        if(prime_numbers):
            sum=sum+numbers
            print(numbers,"",end="")
            n=n+1
        numbers=numbers+1
    print("")
    print("Sum of prime number is ",sum)
   
setup()

Lab 4x Hello Python - Calculate and display multiplication table

def setup():
    multiplication_table(12)
 
def multiplication_table(numbers):
    n=1
    print("** Multiplication table of " ,numbers, "**")
    while(n<=12):
        result=numbers*n
        print(numbers, "X" ,n, "=" ,result)
        n=n+1

setup()

Lab 4x Hello Python - Calculate sum of integers from 1 to N (1+2+3+...+N) using loop (not recursive function)

def setup():
    numbers=10
    print("N = " ,numbers)
    cal_sum_1_to_N(numbers)
 
def cal_sum_1_to_N(n):
    sum=0
    while(n>0):
        if(n>1):
            print(n, "+",end="")
         
        else:
            print("1 = ",end="")
         
        sum=sum+n
        n=n-1
 
    print(sum)
    print("Sum of 1 to N = " ,sum)

setup()

Lab 4x Hello Python - Delivery charge, see question 20 on page 299

def setup():
  cal_charge(0, 1, 8);


def cal_charge(packaging, services, weight):

    print("Type of package (Letter=0/Box=1) is " ,packaging)
    print("Type of service (Next Day Priority=0/Next Day Standard=1/Two-Day=2) is " ,services)
    print("Weight is " ,weight ," oz")
    print();
 
    if(packaging == 0 and services == 0 and weight <= 8):
    
       print("The total is $12.00")
    
    elif(packaging == 0 and services == 1 and weight <= 8):
    
       print("The total is $10.50")
    
    elif(packaging == 0 and services == 2):
    
       print("Not avaliable")
   
    elif(packaging == 1 and services == 0):
        if (weight <= 16):
      
            print("The total is $15.75")
      
        else:
          
         weight = weight/16;
         add = 15.75,((ceil(weight)-1)*1.25);
         print("The total is $" ,add)
      
    
    elif(packaging == 1 and services == 1):
      
        if (weight <= 16):
      
         print("The total is $13.75")
      
        else:
      
         weight = weight/16
         add = 13.75,((ceil(weight)-1)*1.00)
         print("The total is $" ,add)
      
  
    elif(packaging == 1 and services == 2):
      
        if (weight <= 16):
      
         print("The total is $7.00")
      
        else:
      
         weight = weight/16
         add = 7.00,((ceil(weight)-1)*0.50)
         print("The total is $" ,add)

setup()

Lab 4x Hello Python - Leap Year, see question 12 on page 297

def setup():
    cal_leapyear(1796)
 
def cal_leapyear(years):
    print("That year is" ,years)
 
    if(((years%4)==0) and ((years%100)!=0) or ((years%400)==0)):
        print("This is leap year.")
     
    else:
        print("This isn't leap year!")
     
    return years
 
setup()

Lab 4x Hello Python - Power of 10, see question 9 on page 296

def setup():
    cal_power(100)
 
def cal_power(number):
    print("Power of ten is" ,number)
 
    if(number==6):
        print("Number is Million")
     
    elif(number==9):
        print("Number is Billion")
     
    elif(number==12):
        print("Number is Trillion")
     
    elif(number==15):
        print("Number is Quadrillion")
     
    elif(number==18):
        print("Number is Quintillion")
     
    elif(number==21):
        print("Number is Sextillion")
     
    elif(number==30):
        print("Number is Nonillion")
     
    elif(number==100):
        print("Number is Googol")
     
    else:
        print("None number of power ten!")
     
    return number
 
setup()

Lab 4x Hello Python - Calculate grade from score (A for score 80-100, B for 70-79, C for ...)

def setup():
    cal_grade(49)
 
def cal_grade(grade):
    print("Your Score is" ,grade)
 
    if(grade>=80):
        print("You got grade A")
     
    elif(grade>=70):
        print("You got grade B")
     
    elif(grade>=60):
        print("You got grade C")
     
    elif(grade>=50):
        print("You got grade D")
     
    elif(grade<50):
        print("You got grade F!")
     
    return grade
 
setup()

Lab 4x Hello Python - Calculate circumference and area of a circle from its diameter (function)

PI=3.14

def setup():
    cal_area(7)
    cal_circumference(7)

def cal_area(diameter):
    area_of_a_circle=PI*(diameter/2)*(diameter/2)
    print("Diameter is" ,diameter)
    print("Area of a circle is" ,area_of_a_circle)
    return area_of_a_circle
 
def cal_circumference(diameter):
    circumference=PI*diameter
    print("Circumference is" ,circumference)
    return circumference
 
setup()

Sunday, September 13, 2015

Lab4 - Loan payment, see question 30 on page 370 show payment No. , interest, principal, unpaid balance, total interest to date (do not worry about the output format too much, e.g. how to change 1.234 to 1.23)

void setup(){
  float loan_amount=5000;
  float interestRate=12;
  int months=12;
  cal_loan(5000,12,12);
}

void cal_loan(float loan_amount,float interestRate,int months){
  float balance;
  int n=1;
  int term=1;
  float monthlyPayment;
  float effectiveInterest;
  float interest;
  float principal;
  float total_interest=0;
  //Calculate your effective interest J.
  effectiveInterest=interestRate/(100*12);
  //Calculate (1+J)-N
  monthlyPayment=loan_amount*(effectiveInterest/(1-pow(1+effectiveInterest,-months)));
  //Display
  balance=loan_amount;
  println("Loan Amount $ = "+loan_amount);
  println("Loan Term = "+months+" month(s)");
  println("Interest Rate = "+interestRate+"%");
  println("Payment per months = "+monthlyPayment);
  println("Payment No. |  Interset  |  Principal  |  Unpaid Balance  |  Total Interest to Date");
  while(term<=months){
   interest = effectiveInterest*balance;
   principal=monthlyPayment-interest;
   balance=balance-principal;
   total_interest=total_interest+interest;
   println("     "+term+"        $"+interest+"        $"+principal+"    $"+balance+"           $"+total_interest);
   term=term+1;
  }
}





Lab4 - Calculate sum of prime numbers from 1 to N, (create boolean function?)

void setup(){
  sum_prime_number(10);
}

void sum_prime_number(int numbers_end){
 int numbers=2;
 int sum=0;
 int n=1;
 print("Prime number is ");
 while(numbers<=numbers_end){
  int reference=2;
  boolean prime_numbers=true;
   while(reference<numbers && prime_numbers){
    if(numbers%reference==0) prime_numbers=false;
    reference=reference+1;
   }
   if(prime_numbers){
    sum=sum+numbers;
    print(numbers+",");
    n=n+1;
   }
   numbers=numbers+1;
 }
 println("");
 println("Sum of prime number is "+sum);
}


Lab4 - Calculate and display multiplication table

void setup(){
  multiplication_table(12);
}

void multiplication_table(int numbers){
  int n=1;
  int result;
  println("**Multiplication Table of "+numbers+"**");
  while(n<=12){
    result = numbers*n;
    println(numbers+"x"+n+" = "+result);
    n=n+1;
  }
}


Lab4 - Calculate sum of integers from 1 to N (1+2+3+...+N) using loop (not recursive function)

void setup(){
  int numbers=10;
  println("N = "+numbers);
  sum_1_to_N(numbers);
}

void sum_1_to_N(int n){
  int sum=0;
  while(n>0){
   if(n>1){
    println(n+"+");
   }else{
     println("1 = ");
  }
  sum=sum+n;
  n--;
  }
  println(sum);
  println("Sum of 1 to N = "+sum);
}



Lab4 - Flocks of birds

int wingMove = 0;
int wingDelay;

void setup() {
  size(500, 500);
  frameRate(10);
}

void draw() {
  int posX = mouseX;
  int posY = mouseY;
  int count=0;
  int numbers=3;
  background(#87CEFA);
  if (posY < 80){
    wingDelay = 10;
  }
  else if (posY >= 80 && posY < 190){
    wingDelay = 6;
  }
  else{
    wingDelay = 3;
  }
  if (frameCount%wingDelay == 2){
    wingMove = 30;
  }
  else{
    wingMove = 0;
  }
  while(count < numbers){
   posX = (count*150)+100;
   draw_angrybird(posX, posY, 80);
   count = count+1;
  }
}

void draw_angrybird(int posX, int posY, int size) {
  //Body
  strokeWeight(4);
  stroke(#000000);
  fill(#C60000);
  ellipse(posX, posY, size, size);
  //Eyes
  strokeWeight(4);
  stroke(#000000);
  fill(#FFFFFF);
  ellipse(posX-(size/10), posY-(size/10), size/5, size/5);
  ellipse(posX+(size/10), posY-(size/10), size/5, size/5);
  stroke(#000000);
  fill(#000000);
  ellipse(posX-(size/15), posY-(size/10), size/15, size/15);
  ellipse(posX+(size/15), posY-(size/10), size/15, size/15);
  //Eyebrow
  strokeWeight(4);
  stroke(#000000);
  rect(posX-(size/3.8), posY-(size/4.2), size/4, size/10);
  rect(posX+(size/60), posY-(size/4.2), size/4, size/10);
  //Mouth
  strokeWeight(3);
  stroke(#000000);
  fill(#FFB90F);
  triangle(posX-(size/7.5), posY+(size/10), posX+(size/6), posY+(size/6), posX, posY);
  triangle(posX-(size/7.5), posY+(size/10), posX+(size/6), posY+(size/6), posX, posY+(size/5));
  //Wings
  strokeWeight(3);
  stroke(#000000);
  fill(#C60000);
  triangle(posX+(size/2), posY+(size/15), posX+(size/1.15), posY+wingMove, posX+(size/2), posY-(size/15));
  triangle(posX-(size/2), posY+(size/15), posX-(size/1.15), posY+wingMove, posX-(size/2), posY-(size/15));
}