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

Lab 6 - Student Data (Age)(While Version)

def setup():
    student_name = ["ant","bird","cat","dog","eagle","frog"]
    student_id = [58001,58010,58011,58100,58101,58110]
    student_age = [39,25,19,30,22,18]
    student_weight = [55,75,44,72,70,100]
    student_height = [165,170,155,169,178,175]
   
    display(student_name,student_id,student_age,student_weight,student_height)
   
 
def display(name,ID,age,weight,height):
    print("Number of students with age < 30 =",find_numAge_lessthan30(age))
    i = 0
    insertionSort_byAge(name,ID,age,weight,height)
    while(i < len(name)):
        print("\nName :",name[i])
        print("ID :",ID[i])
        print("Age :",age[i])
        print("Weight :",weight[i])
        print("Height :",height[i])
        i += 1

def find_numAge_lessthan30(age):
    i = 0
    count = 0
    while(i < len(age)):
        if(age[i] < 30):
            count += 1
        i += 1
    return count

def insertionSort_byAge(name,ID,age,weight,height):
    i = 1
    while(i < len(age)):
        currentName = name[i]
        currentID = ID[i]
        currentAge = age[i]
        currentWeight = weight[i]
        currentHeight = height[i]

        position = i
        while(position > 0 and age[position-1] > currentAge):
            name[position] = name[position-1]
            ID[position] = ID[position-1]
            age[position] = age[position-1]
            weight[position] = weight[position-1]
            height[position] = height[position-1]
            position = position-1
           
        name[position] = currentName
        ID[position] = currentID
        age[position] = currentAge
        weight[position] = currentWeight
        height[position] = currentHeight
        i += 1

setup()

Lab 6 - Transpose Matrix (While Version)

def setup():
    a = [1,1,2]
    b = [5,5,5]
    c = [9,1,1]
    matrix = [a,b,c]
    display(matrix)
    print()
    display(transpose(matrix))
def display(matrix):
    i = 0
    while(i < len(matrix)):
        j = 0
        while(j < len(matrix[i])):
            print(matrix[i][j],end = ' ')
            j += 1
        print()
        i += 1

def transpose(matrix):
    i = 0
    while(i < len(matrix)):
        j = i+1
        while(j < len(matrix[i])):
            temp = matrix[i][j]
            matrix[i][j] = matrix[j][i]
            matrix[j][i] = temp
            j += 1
        i += 1
    return matrix

setup()

Lab 6 - Transpose Matrix

def setup():
    a = [1,1,2]
    b = [5,5,5]
    c = [9,1,1]
    matrix = [a,b,c]
    display(matrix)
    print()
    display(transpose(matrix))
def display(matrix):
    i = 0
    while(i < len(matrix)):
        j = 0
        while(j < len(matrix[i])):
            print(matrix[i][j],end = ' ')
            j += 1
        print()
        i += 1

def transpose(matrix):
    for i in range (0,len(matrix)):
        for j in range (i+1,len(matrix[i])):
            temp = matrix[i][j]
            matrix[i][j] = matrix[j][i]
            matrix[j][i] = temp
    return matrix

setup()

Lab 6 - Matrix

def setup():
    matrix1 = [[15,50],[4,16]]
    matrix2 = [[15,10],[10,12]]
    print("Matrix One")
    display(matrix1)
    print("\nMatrix Two")
    display(matrix2)
    print("\nAdd Two Matrices")
    display(addMatrix(matrix1,matrix2))
    print("\nSubtract Two Matrices")
    display(subtractMatrix(matrix1,matrix2))
    print("\nMultiply Two Matrices")
    display(multiplyMatrix(matrix1,matrix2))

   
def display(matrix):
    i = 0
    while(i < len(matrix)):
        j = 0
        while(j < len(matrix[i])):
            print(matrix[i][j],end = ' ')
            j += 1
        print()
        i += 1

def addMatrix(matrix1,matrix2):
    answer = [[0,0],[0,0]]
    i = 0
    while(i < len(matrix1)):
        j = 0
        while(j < len(matrix1[i])):
            answer[i][j] = matrix1[i][j]+matrix2[i][j]
            j += 1
        i += 1
    return answer

def subtractMatrix(matrix1,matrix2):
    answer = [[0,0],[0,0]]
    i = 0
    while(i < len(matrix1)):
        j = 0
        while(j < len(matrix1[i])):
            answer[i][j] = matrix1[i][j]-matrix2[i][j]
            j += 1
        i += 1
    return answer

def multiplyMatrix(matrix1,matrix2):
    answer = [[0,0],[0,0]]
    i = 0
    while(i < len(matrix1)):
        j = 0
        k = 0
        while(j < len(matrix1[i])):
            answer[i][j] = (matrix1[i][k]*matrix2[k][j])+(matrix1[i][k+1]*matrix2[k+1][j])
            j += 1
        i += 1
    return answer

setup()

Lab 6 - Chair in Building

def setup():
    floor1 = [20,15,15,25,30,20]
    floor2 = [20,40,35,20,35,35]
    floor3 = [25,40,10,40,30,25]
    floor4 = [20,15,15,40,15,15]
    building = [floor1,floor2,floor3,floor4]

    print("Total number of chairs =",sumChair(building))
    print("\nThe floor with maximum number of chairs :\n - floor "+str(findFloor_maxChair(building)))
    print("\nThe Room with maximum number of chairs : "+findRoom_maxChair(building))

def sumChair(building):
    i = 0
    sum = 0
    while(i < len(building)):
        j = 0
        while(j < len(building[i])):
            sum += building[i][j]
            j += 1
        i += 1
    return sum

def findFloor_maxChair(building):
    i = 0
    floor = 0
    sumChair = 0
    totalChair = 0
    while(i < len(building)):
        j = 0
        while(j < len(building[i])):
            sumChair += building[i][j]
            j += 1
        if(totalChair < sumChair):
            totalChair = sumChair
            floor = i
        sumChair = 0
        i += 1
    return floor+1

def findRoom_maxChair(building):
    floor = 0
    maxChair = 0
    maxChair_room = ""
    while(floor < len(building)):
        room = 0
        while(room < len(building[floor])):
            if(maxChair < building[floor][room]):
                maxChair = building[floor][room]
            room += 1
        floor += 1
       
    i = 0
    while(i <  len(building)):
        j = 0
        while(j < len(building[i])):
            if(maxChair == building[i][j]):
                maxChair_room += "\n - Room "+str(j+1)+" Floor "+str(i+1)
            j += 1
        i += 1
    return maxChair_room
setup()

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

Lab 6 - Student Data (Weight)

def setup():
    student_name = ["ant","bird","cat","dog","eagle","frog"]
    student_id = [58001,58010,58011,58100,58101,58110]
    student_age = [39,25,19,30,22,18]
    student_weight = [55,75,44,72,70,100]
    student_height = [165,170,155,169,178,175]
   
    display(student_name,student_id,student_age,student_weight,student_height)
   
 
def display(name,ID,age,weight,height):
    print("Number of students with weight < 50 =",find_numWeight_lessthan50(weight))
    i = 0
    while(i < len(name)):
        if(weight[i] < 50):
            print("\nName :",name[i])
            print("ID :",ID[i])
            print("Age :",age[i])
            print("Weight :",weight[i])
            print("Height :",height[i])
         
        i += 1

def find_numWeight_lessthan50(weight):
    i = 0
    count = 0
    while(i < len(weight)):
        if(weight[i] < 50):
            count += 1
        i += 1
    return count


setup()

Lab 6 - Student Data (Age)

def setup():
    student_name = ["ant","bird","cat","dog","eagle","frog"]
    student_id = [58001,58010,58011,58100,58101,58110]
    student_age = [39,25,19,30,22,18]
    student_weight = [55,75,44,72,70,100]
    student_height = [165,170,155,169,178,175]
   
    display(student_name,student_id,student_age,student_weight,student_height)
   
 
def display(name,ID,age,weight,height):
    print("Number of students with age < 30 =",find_numAge_lessthan30(age))
    i = 0
    insertionSort_byAge(name,ID,age,weight,height)
    while(i < len(name)):
        print("\nName :",name[i])
        print("ID :",ID[i])
        print("Age :",age[i])
        print("Weight :",weight[i])
        print("Height :",height[i])
        i += 1

def find_numAge_lessthan30(age):
    i = 0
    count = 0
    while(i < len(age)):
        if(age[i] < 30):
            count += 1
        i += 1
    return count

def insertionSort_byAge(name,ID,age,weight,height):
    for i in range(1,len(age)):
        currentName = name[i]
        currentID = ID[i]
        currentAge = age[i]
        currentWeight = weight[i]
        currentHeight = height[i]

        position = i
        while(position > 0 and age[position-1] > currentAge):
            name[position] = name[position-1]
            ID[position] = ID[position-1]
            age[position] = age[position-1]
            weight[position] = weight[position-1]
            height[position] = height[position-1]
            position = position-1
           
        name[position] = currentName
        ID[position] = currentID
        age[position] = currentAge
        weight[position] = currentWeight
        height[position] = currentHeight


setup()

Lab 6 - Student Data (BMI)

def setup():
    student_name = ["ant","bird","cat","dog","eagle","frog"]
    student_id = [58001,58010,58011,58100,58101,58110]
    student_age = [39,25,19,30,22,18]
    student_weight = [55,75,44,72,70,100]
    student_height = [165,170,155,169,178,175]
   
    display(student_name,student_id,student_weight,student_height)
   
 
def display(name,ID,weight,height):
    print("Number of students with BMI > 25 =",find_numBMI_greaterthan25(weight,height))
    i = 0
    while(i < len(name)):
        if(calBMI(weight[i],height[i]) > 25):
            print("\nName :",name[i])
            print("ID :",ID[i])
            print("Weight :",weight[i])
            print("Height :",height[i])
            print("BMI : %.2f"%calBMI(weight[i],height[i]))
        i += 1

def calBMI(weight,height):
    highM = height/100
    bmi = weight/(highM*highM)
    return bmi

def find_numBMI_greaterthan25(weight,height):
    i = 0
    count = 0
    while(i < len(weight)):
        if(calBMI(weight[i],height[i]) > 25):
            count += 1
        i += 1
    return count


setup()

Lab 6 - Student Data (Display)

def setup():
   student_name = ["ant","bird","cat","dog","eagle","frog"]
   student_id = [58001,58010,58011,58100,58101,58110]
   student_age = [39,25,19,30,22,18]
   student_weight = [55,75,44,72,70,100]
   student_height = [165,170,155,169,178,175]
 
   display(student_name,student_id,student_age,student_weight,student_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],"\n")
      i += 1
 
setup()

Lab 5 - Array

def setup():
    n = [ 16,48,64,-15,64,64,-69,30,-13,29]
    showIndex(n)
    print("\nMaximum =",find_maxNum(n))
    print("First Index Maximum =",find_firstMaxNum(n))
    print("Last Index Maximum =",find_lastMaxNum(n))
    print("Sum of Value =",sumValue(n))
    print("Sum of Positive Value =",sumPositiveValue(n))
    print("Count Number of Positive Value =",countPositiveNumber(n))
    print("Average of Value =",averageValue(n),"\n\n")
    showValue_afterChange(n)

   
def showIndex(n):
    i = 0
    while(i < len(n)):
        print("Index",i,"=",n[i])
        i += 1
   
def find_maxNum(n):
    i = 0
    maxNum = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
        i += 1
    return maxNum

def find_firstMaxNum(n):
    i = 0
    maxNum = 0
    indexMax = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
            indexMax = i
        i += 1
    return indexMax

def find_lastMaxNum(n):
    i = 0
    maxNum = 0
    indexMax = 0
    while(i < len(n)):
        if(n[i]> maxNum):
            maxNum = n[i]
        elif(n[i]== maxNum):
            indexMax = i
        i += 1
    return indexMax

def sumValue(n):
    i = 0
    sum_value = 0
    while(i < len(n)):
        sum_value += n[i]
        i += 1
    return sum_value

def sumPositiveValue(n):
    i = 0
    sum_positiveValue = 0
    while(i < len(n)):
        if(n[i] >= 0):
            sum_positiveValue += n[i]
        i += 1
    return sum_positiveValue

def countPositiveNumber(n):
    i = 0
    count_positive = 0
    while(i < len(n)):
        if(n[i] >= 0):
            count_positive += 1
        i += 1
    return count_positive

def averageValue(n):
    i = 0
    sum_value = 0
    while(i < len(n)):
        sum_value += n[i]
        i += 1
    average = sum_value/len(n)
    return average

def showValue_afterChange(n):
    change = -50
    i = 0
    if(change >= 0):
        print("Increase =",change)
    else:
        print("Decrease =",change)
    while(i < len(n)):
        n[i] += change
        print("Index",i,"=",n[i])
        i += 1

   
setup()

Lab 5 - Base 10 to Base 2

def setup():
    print(changeBase_ten_to_two(8))
    print(changeBase_ten_to_two(9))
    print(changeBase_ten_to_two(17))
   
def changeBase_ten_to_two(baseTen):
    baseTwo = ""
    while(baseTen > 0):
        baseTwo = str(baseTen%2)+baseTwo
        baseTen = int(baseTen/2)
    return baseTwo
       
setup()

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

Lab 5 - String

def setup():
   print(myCount("peejalovelove","e"))
   print(myFind("Thailand", "i"))
   print(myReplace("Thailand", "Thai", "Eng"))
   print(myStrip("The Land of Smile"))
   assert(startWith("peejalovelove","pee")) == True
   assert(endWith("peejalovelove","ja")) == False
 
def myCount(fullword,char):
   count = 0
   i = 0
   while(i < len(fullword)):
      if(fullword[i] == char):
         count += 1
      i += 1
   return count

def myFind(fullword,char):
   index = 0
   i = 0
   while(i < len(fullword)):
      if(fullword[i] == char):
         index = i
      i += 1
   return index

def myReplace(fullword,oldword,replace):
   newWord = ""
   i = 0
   while(i < len(fullword)):
      if(fullword[i] != oldword[0]):
         newWord += fullword[i]
         i += 1
      else:
         num = 0
         while(num < len(oldword) and oldword[num] == fullword[i+num]):
            num += 1
         if(num == len(oldword)):
            newWord += replace
            i += len(oldword)
   return newWord

def myStrip(word):
   newWord = ""
   i = 0
   while(i < len(word)):
      if(word[i] != " "):
         newWord += word[i]
      i += 1
   return newWord

def startWith(fullword,startword):
   wordCheck = list(fullword)
   startCheck = list(startword)
   i = 0
   while(i < len(startCheck)):
      if(startCheck[i] != wordCheck[i]):
         return False
      i += 1
   return True

def endWith(fullword,endword):
   wordCheck = list(fullword)
   endCheck = list(endword)
   i = 0
   while(i < len(endCheck)):
      if(endCheck[i+(len(endCheck)-1)] != wordCheck[i+(len(wordCheck)-1)]):
         return False
      i += 1
   return True

setup()