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()
Building Software Systems I & II
&
Computer Fundamentals
By Thakdanai Khunsaen
OS : Ubuntu GNOME 17.04 64-bit
วันศุกร์ที่ 23 ตุลาคม พ.ศ. 2558
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()
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()
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()
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()
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()
วันอาทิตย์ที่ 20 กันยายน พ.ศ. 2558
Lab4x - Summation of Prime Number
def setup():
print("Sum of Prime Number\n")
sum_prime(0,20)
sum_prime(0,100)
def sum_prime(minNumber,maxNumber):
print(minNumber,"-",maxNumber)
sumPrime = 0
while (minNumber <= maxNumber):
if (prime(minNumber)):
sumPrime += minNumber
minNumber += 1
print("Sum = ",sumPrime)
def prime(value):
num = 2
while (num <= value):
if (value%num == 0):
if (value == num):
return True
else:
return False
num += 1
return False
setup()
print("Sum of Prime Number\n")
sum_prime(0,20)
sum_prime(0,100)
def sum_prime(minNumber,maxNumber):
print(minNumber,"-",maxNumber)
sumPrime = 0
while (minNumber <= maxNumber):
if (prime(minNumber)):
sumPrime += minNumber
minNumber += 1
print("Sum = ",sumPrime)
def prime(value):
num = 2
while (num <= value):
if (value%num == 0):
if (value == num):
return True
else:
return False
num += 1
return False
setup()
Lab4x - Summation of Integers
def setup():
print("Sum of Integers\n")
sumIntegers(1,5)
sumIntegers(1,20)
def sumIntegers(minNumber,maxNumber):
print(minNumber," + ",(minNumber+1)," + ... + ",maxNumber)
sumIntegers = 0
while (minNumber <= maxNumber):
sumIntegers = sumIntegers + minNumber
minNumber = minNumber+1
print("Sum = ",sumIntegers)
setup()
print("Sum of Integers\n")
sumIntegers(1,5)
sumIntegers(1,20)
def sumIntegers(minNumber,maxNumber):
print(minNumber," + ",(minNumber+1)," + ... + ",maxNumber)
sumIntegers = 0
while (minNumber <= maxNumber):
sumIntegers = sumIntegers + minNumber
minNumber = minNumber+1
print("Sum = ",sumIntegers)
setup()
สมัครสมาชิก:
บทความ (Atom)