Lab 5 Generic 1 ( Value of array and its index )
def setup():
value = [19,18,17,16,15,14]
ValueAndIndex(value)
def ValueAndIndex(value):
index = 0
while(index < len(value)):
print("Value = ",value[index]," Index = ",index)
index = index + 1
setup()
Lab 5 Generic 2 ( Maximum value )
def setup():
value = [19,18,17,21,15,32]
max_value(value)
def max_value(value):
index = 0
max_num = value[0]
while(index < len(value)):
if(value[index] > max_num):
max_num = value[index]
index = index + 1
print("Max value = ",max_num)
setup()
Lab 5 Generic 3 ( Index of the first maximum)
def setup():
value = [19,17,21,21,15,14]
first_max_value_index(value)
def first_max_value_index(value):
index = 0
max_num = value[0]
while(index < len(value)):
if(value[index] > max_num):
max_num = value[index]
first_index = index
index = index + 1
print(" First Maximum =",max_num)
print(" First Maximum index =",first_index)
setup()
Lab 5 Generic 4 ( Index of the last maximum)
def setup():
value = [19,17,21,21,15,14,21]
last_max_value_index(value)
def last_max_value_index(value):
index = 0
max_num = value[0]
while(index < len(value)):
current = value[index]
if(current >= max_num):
max_num = current
last_index = index
index = index + 1
print(" Last Maximum index =",last_index)
setup()
Lab 5 Generic 5 ( Sum of values in array )
def setup():
value = [19,17,21,21,15,14,21]
sum_array(value)
def sum_array(value):
index = 0
sum_num = 0
while(index < len(value)):
sum_num = sum_num + value[index]
index = index + 1
print(" Sum of array value = ",sum_num)
setup()
Lab 5 Generic 6 ( Sum of positive values )
def setup():
value = [19,-17,21,-21,15,14,-21]
sum_array(value)
def sum_array(value):
index = 0
sum_num = 0
while(index < len(value)):
if(value[index] > 0):
sum_num = sum_num + value[index]
index = index + 1
print(" Sum of array value = ",sum_num)
setup()
Lab 5 Generic 7 ( Find/count number of positive values )
def setup():
value = [19,-17,21,-21,15,14,-21]
sum_array(value)
def sum_array(value):
index = 0
count = 0
while(index < len(value)):
if(value[index] > 0):
count = count + 1
index = index + 1
print(" Number of positive values = ",count)
setup()
Lab 5 Generic 8 ( average of values )
def setup():
value = [19,17,21,21,15,14]
sum_array(value)
def sum_array(value):
index = 0
sum_num = 0
while(index < len(value)):
sum_num = sum_num + value[index]
index = index + 1
avg_value = sum_num/len(value)
print("Sum of values = ",sum_num)
print("Average = ",avg_value)
setup()
Lab 5 Generic 9 ( Increase/decrease values )
def setup():
value = [19,17,21,21,15,14]
show_array(value)
value = change_array_value(value)
show_array(value)
def change_array_value(value):
index = 0
Change = int(input("Value Change = "))
while(index < len(value)):
value[index] = value[index] + Change
index = index + 1
return value
def show_array(value):
index = 0
print("Value = [",end ="")
while(index < len(value)):
if(index < (len(value)-1)):
print(value[index],",",end = "")
else:
print(value[index],"]")
index = index + 1
setup()
Lab 5 Generic 10 ( convert a number from base 10 to base 2 )
def setup():
num = 2
print("Number base 10 = ",num)
new_num = convert_10_to_2(num)
print("Number base 2 = ",new_num)
def convert_10_to_2(num):
temp = 0
while(num > 0):
count = 0
while(num/(pow(2,count)) >= 1):
count = count + 1
count = count - 1
num = num - pow(2,count)
temp = temp + pow(10,count)
if(num == 1):
num = 0
temp = temp + 1
assert (count >= 0)
return temp
setup()
Lab 5 Generic 11 ( my_count() function )
def setup():
string1 = "Thailand"
ref_alphabet = "a"
print("String : ",string1)
print("Reference alphabet : ",ref_alphabet,)
num_alphabet = my_count(string1,ref_alphabet)
print("Alphabet number(s) = ",num_alphabet)
def my_count(string,alphabet):
num_alphabet = 0
index = 0
while(index < len(string)):
if(string[index] == alphabet):
num_alphabet = num_alphabet + 1
index = index + 1
return num_alphabet
assert num_alphabet == count(string,alphabet)
setup()
Lab 5 Generic 12 ( my_find() function )
def setup():
string1 = "Thailand"
alphabet = "a"
print("String : ",string1)
print("Alphabet : ",alphabet)
index = my_find(string1,alphabet)
print("Alphabet index : ",index)
assert string1.find(alphabet) == my_find(string1,alphabet)
def my_find(string,alphabet):
place_alphabet = 0
index = 0
finsh = False
while(index < len(string) and (finsh == False)):
if(string[index] == alphabet):
finsh = True
return index
index = index + 1
setup()
value = [19,17,21,21,15,14]
show_array(value)
value = change_array_value(value)
show_array(value)
def change_array_value(value):
index = 0
Change = int(input("Value Change = "))
while(index < len(value)):
value[index] = value[index] + Change
index = index + 1
return value
def show_array(value):
index = 0
print("Value = [",end ="")
while(index < len(value)):
if(index < (len(value)-1)):
print(value[index],",",end = "")
else:
print(value[index],"]")
index = index + 1
setup()
Lab 5 Generic 10 ( convert a number from base 10 to base 2 )
def setup():
num = 2
print("Number base 10 = ",num)
new_num = convert_10_to_2(num)
print("Number base 2 = ",new_num)
def convert_10_to_2(num):
temp = 0
while(num > 0):
count = 0
while(num/(pow(2,count)) >= 1):
count = count + 1
count = count - 1
num = num - pow(2,count)
temp = temp + pow(10,count)
if(num == 1):
num = 0
temp = temp + 1
assert (count >= 0)
return temp
setup()
Lab 5 Generic 11 ( my_count() function )
def setup():
string1 = "Thailand"
ref_alphabet = "a"
print("String : ",string1)
print("Reference alphabet : ",ref_alphabet,)
num_alphabet = my_count(string1,ref_alphabet)
print("Alphabet number(s) = ",num_alphabet)
def my_count(string,alphabet):
num_alphabet = 0
index = 0
while(index < len(string)):
if(string[index] == alphabet):
num_alphabet = num_alphabet + 1
index = index + 1
return num_alphabet
assert num_alphabet == count(string,alphabet)
setup()
Lab 5 Generic 12 ( my_find() function )
def setup():
string1 = "Thailand"
alphabet = "a"
print("String : ",string1)
print("Alphabet : ",alphabet)
index = my_find(string1,alphabet)
print("Alphabet index : ",index)
assert string1.find(alphabet) == my_find(string1,alphabet)
def my_find(string,alphabet):
place_alphabet = 0
index = 0
finsh = False
while(index < len(string) and (finsh == False)):
if(string[index] == alphabet):
finsh = True
return index
index = index + 1
setup()
Lab 5 Generic 13 ( my_replace() function )
def setup():
string1 = "Thailand"
string2 = "land"
string3 = "thai"
print("string1 : ",string1)
print("Referent String : ",string2)
print("Replacement : ",string3)
string1 = my_replace(string1,string2,string3)
assert my_replace(string1,string2,string3) == string1.replace(string2,string3)
print("Result : ",string1)
def my_replace(string,refStr,replacementStr):
strIndex = 0
refIndex = 0
replaceIndex = 0
stringTemp = ""
stringTemp2 = ""
replace = False
while(strIndex < len(string)):
stringTemp = stringTemp + string[strIndex]
if(string[strIndex] == refStr[refIndex]):
while(refIndex < len(refStr)):
if(string[strIndex] == refStr[refIndex]):
replace = True
else:
replace = False
strIndex += 1
refIndex += 1
if(replace):
temp = 0
while(temp < strIndex - len(refStr)):
stringTemp2 = stringTemp2 + stringTemp[temp]
temp += 1
stringTemp = stringTemp2
stringTemp = stringTemp + replacementStr
stringTemp2 = ""
else:
strIndex = strIndex - len(refStr)
refIndex = 0
strIndex += 1
return stringTemp
setup()
Lab 5 Generic 14 ( my_strip() function )
def setup():
string1 = " Thailand "
print("string1 {",string1,"}")
string1 = my_strip(string1)
assert my_strip(string1) == string1.rstrip("")
print("Result {",string1,"}")
def my_strip(string):
strIndex = 0
Refstring = " "
stringTemp = ""
replace = True
while(strIndex < len(string) and replace):
if(string[strIndex] == Refstring):
strIndex += 1
replace = True
else:
replace = False
while(strIndex < len(string)):
stringTemp = stringTemp + string[strIndex]
strIndex +=1
string = stringTemp
strIndex = len(string) - 1
replace = True
stringTemp = ""
while(strIndex > 0 and replace):
if(string[strIndex] == Refstring):
strIndex -= 1
replace = True
else:
replace = False
temp = strIndex
strIndex = 0
while(strIndex <= temp):
stringTemp = stringTemp + string[strIndex]
strIndex +=1
string = stringTemp
return string
setup()
Lab 5 Generic 15 ( my_startswith() function )
def setup():
string1 = "Thailand"
string2 = "Sun"
print("String : ",string1)
print("Referent String : ",string2)
assert my_startswith(string1,string2) == string1.startswith(string2)
print("String starts with referent string :",my_startswith(string1,string2))
def my_startswith(string,referentString):
Index = 0
startsWithRef = True
while(Index < len(referentString) and startsWithRef):
if(string[Index] == referentString[Index]):
Index += 1
startsWithRef = True
else:
startsWithRef = False
return startsWithRef
setup()
Lab 5 Generic 16 ( my_endswith() function )
def setup():
string1 = "Thailand"
string2 = "land"
print("String : ",string1)
print("Referent String : ",string2)
assert my_endswith(string1,string2) == string1.endswith(string2)
print("String ends with referent string :",my_endswith(string1,string2))
def my_endswith(string,referentString):
strIndex = len(string) - 1
refIndex = len(referentString) - 1
endsWithRef = True
while(strIndex >= len(string) - len(referentString) and endsWithRef):
if(string[strIndex] == referentString[refIndex]):
strIndex -= 1
refIndex -= 1
endsWithRef = True
else:
endsWithRef = False
return endsWithRef
setup()
string1 = " Thailand "
print("string1 {",string1,"}")
string1 = my_strip(string1)
assert my_strip(string1) == string1.rstrip("")
print("Result {",string1,"}")
def my_strip(string):
strIndex = 0
Refstring = " "
stringTemp = ""
replace = True
while(strIndex < len(string) and replace):
if(string[strIndex] == Refstring):
strIndex += 1
replace = True
else:
replace = False
while(strIndex < len(string)):
stringTemp = stringTemp + string[strIndex]
strIndex +=1
string = stringTemp
strIndex = len(string) - 1
replace = True
stringTemp = ""
while(strIndex > 0 and replace):
if(string[strIndex] == Refstring):
strIndex -= 1
replace = True
else:
replace = False
temp = strIndex
strIndex = 0
while(strIndex <= temp):
stringTemp = stringTemp + string[strIndex]
strIndex +=1
string = stringTemp
return string
setup()
Lab 5 Generic 15 ( my_startswith() function )
def setup():
string1 = "Thailand"
string2 = "Sun"
print("String : ",string1)
print("Referent String : ",string2)
assert my_startswith(string1,string2) == string1.startswith(string2)
print("String starts with referent string :",my_startswith(string1,string2))
def my_startswith(string,referentString):
Index = 0
startsWithRef = True
while(Index < len(referentString) and startsWithRef):
if(string[Index] == referentString[Index]):
Index += 1
startsWithRef = True
else:
startsWithRef = False
return startsWithRef
setup()
Lab 5 Generic 16 ( my_endswith() function )
def setup():
string1 = "Thailand"
string2 = "land"
print("String : ",string1)
print("Referent String : ",string2)
assert my_endswith(string1,string2) == string1.endswith(string2)
print("String ends with referent string :",my_endswith(string1,string2))
def my_endswith(string,referentString):
strIndex = len(string) - 1
refIndex = len(referentString) - 1
endsWithRef = True
while(strIndex >= len(string) - len(referentString) and endsWithRef):
if(string[strIndex] == referentString[refIndex]):
strIndex -= 1
refIndex -= 1
endsWithRef = True
else:
endsWithRef = False
return endsWithRef
setup()
ปัญหาและวิธีแก้ :
1) ฟังก์ชั่น print error เพราะ ไม่สามารถหาตัวค่าตัวแปร array ที่ตำแหน่งที่ฟังก์ชั่น len() วัดค่าได้
(เปลี่ยนเงื่อนไขจาก while(index <= len(value)) เป็น while(index < len(value)) เพราะ array ที่มีจำนวนค่า 6 ค่า จะมีเลขประจำตำแหน่งเป็น 0-5)
2) ฟังก์ชั่น input() error เพราะไม่ได้ใส่เครื่องหมาย " " ในวงเล็บ(ใส่เครื่องหมาย" "ล้อมข้อความที่ต้องการให้แสดงในฟังก์ชั่น input() )
3) ไม่สามารถประกาศตัวแปรได้ (กำหนดค่าให้ตัวแปรเวลาประกาศตัวแปรเลย เช่น sum_num = 0 แทน การประกาศ sum_num โดดๆ)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น