วันศุกร์ที่ 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()