วันเสาร์ที่ 26 กันยายน พ.ศ. 2558

Lab05 : Find index of (the first,the last) maximum value in array

def setup():
    a = [8,8,3,5,8,2,7,-5,2]
    display_list(a)
    maxval_position(a)

def maxval(a):
    max=a[0]
    i=0
    while i<len(a):
        if a[i]>max:
            max=a[i]
        i+=1
    return max

def maxval_position(a):
    max=maxval(a)
    i=0
    keep=[]
    while i < len(a):
        if a[i]==max:
            keep.append(i)
        i+=1
    if len(keep)==1:
        print("First & Last:",keep[0])
    else :
        print("First:",keep[0])
        print("Last:",keep[len(keep)-1])

def display_list(a):
    i=0
    while i<len(a):
        print(i,":",a[i])
        i+=1
   
setup() # keep=[] -> list # keep.append("what to append")
======================================================================
def setup():
    a = [8,8,3,5,8,2,7,-5,2]
    display_list(a)
    m=maxval(a)
    print(max_first_position(a,m))
    print(max_last_position(a,m))

def maxval(a):
    max=a[0]
    i=0
    while i<len(a):
        if a[i]>max:
            max=a[i]
        i+=1
    return max

def max_first_position(a,max):
    i=0
    while i < len(a):
        if a[i]==max:
            return i
            break
        i+=1
def max_last_position(a,max):
    i=len(a)-1
    while i > 0:
        if a[i]==max:
            return i
            break
        i-=1
     
def display_list(a):
    i=0
    while i<len(a):
        print(i,":",a[i])
        i+=1

setup()

ไม่มีความคิดเห็น:

แสดงความคิดเห็น

commit เพิ่มเติม : เพื่ม userstory, bookmark

>>  add userstory ทำการเพิ่ม userstory ใน functional tests >>  add bookmark for user เพิ่มระบบ Bookmark โดยการสร้าง model ขึ...