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()