วันอาทิตย์ที่ 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()