def setup():
n = [18 ,5 ,4 ,6 ,9]
print("Elements and Indexs of array n is")
find(n)
def find(array):
index = 0
while(index < len(array)):
element = array[index]
print("At index",index ,"Element is" ,element)
index+=1
setup()
------------------------------------------------------------------------------
Lab5 - Find the maximum value in array
def setup():
n = [18 ,5 ,9 ,7 ,8]
find_max(n)
def find_max(array):
index = 0
Max = array[index]
Maxindex = index
while(index
Maxindex = index
index +=1
assert Max == 18
assert Maxindex == 0
print("Maximum is " ,Max ,"in index" ,Maxindex)
setup()
-----------------------------------------------------------------------------
Lab5 - Find index of (the first) maximum value in array
def setup():
n = [18 ,5 ,18 ,7 ,8]
find_max(n)
def find_max(array):
index = 0
Max = array[index]
Maxindex = index
while(index
Maxindex = index
index +=1
assert Max == 18
assert Maxindex == 0
print("Maximum is " ,Max ,"in index" ,Maxindex)
setup()
---------------------------------------------------------------------------
Lab5 - Find index of (the last) maximum value in array
def setup():
n = [18 ,5 ,18 ,7 ,8]
find_max(n)
def find_max(array):
index = 0
Max = array[index]
Maxindex = index
while(index
Maxindex = index
if(Max==array[index]):
Maxindex = index
index +=1
assert Max == 18
assert Maxindex == 2
print("Maximum is " ,Max ,"in index" ,Maxindex)
setup()
------------------------------------------------------------------------------
Lab5 - Find sum of values in array
def setup():
n = [18 ,5 ,18 ,7 ,8]
find_sum(n)
def find_sum(array):
index = 0
sum = 0
while(index
index +=1
assert sum == 56
print("Sum of array is" ,sum)
setup()
--------------------------------------------------------------------------------
Lab5 - Find sum of positive values in array
def setup():
n = [18 ,5 ,18 ,-9 ,-8]
find_sum(n)
def find_sum(array):
index = 0
sum = 0
while(index
sum += array[index]
index +=1
assert sum == 56-15
print("Sum of array is" ,sum)
setup()
-----------------------------------------------------------------------------------
Lab5 - Find/count number of positive values in array
def setup():
n = [18 ,5 ,18 ,-9 ,-8]
find_positive(n)
def find_positive(array):
index = 0
positive = 0
while(index
positive += 1
print(array[index],"is a positive numbers in index" ,index)
index +=1
assert positive ==3
print("Numbers of Positive is" ,positive)
setup()
---------------------------------------------------------------------------------
Lab5 - Find average of values in array
def setup():
n = [18 ,5 ,18 ,15 ,7]
find_average(n)
def find_average(array):
index = 0
average =0
while(index
index +=1
average = average/len(array)
assert average == 63/5
print("Average of array is" ,average)
setup()
---------------------------------------------------------------------------------------
Lab5 - Increase/decrease values in array (by fixed value)
def setup():
n = [12,3,5,8,10]
change_value(n)
def change_value(array):
index = 0
while(index
add = int(input("Please input value you want to increase/decrease :"))
array[index] = array[index]+add
print("Now value of this index is " ,array[index])
index +=1
setup()
------------------------------------------------------------------------------------------
Lab5 - convert a number from base 10 to base 2
def setup():
base10 = int(input("Please input value :"))
convert(base10)
def convert(base10):
base2 = ""
while(base10>0):
while(base10 %2 ==0 and base10 > 0):
base2 = "0"+base2
base10 = base10/2
while(base10%2!=0 and base10>0):
base2 = "1"+base2
base10 = (base10/2)-0.5
print(base2)
setup()
---------------------------------------------------------------------------------------------
Lab5 - my_find
def setup():
n = input("Please Input Word :")
find = input("Please Input Word You want to find :")
my_find(n,find)
def my_find(n ,find):
index = 0
while(index
print(find,"is in index",index)
index = index+1
setup()
--------------------------------------------------------------------------------------------
Lab5 - my_count
def setup():
n = input("Please Input Word :")
count = input("Please Input Word You want to Count :")
print("Count is " ,my_count(n,count))
def my_count(n,find):
index = 0
count =0
while(index
count +=1
index = index+1
return count
setup()
---------------------------------------------------------------------------------------------
ไม่มีความคิดเห็น:
แสดงความคิดเห็น