Create a list of numbers consisting of your student number
and find any s
pecific number inside the list
,
you can use listsearch or binary
search
this list. Then do a sorting to sort the numbers in this list from
least to greatest.
Keep the
repetitive
numbers as it is.
def find(L, n):
"""Find n in list L
"""
for x in L:
if x == n:
return True
return False
def sort(L):
"""Sort list L in accecing order
use insertion sort
"""
n = len(L)
for i in range(1,n):
j = i
while j > 0 and L[j-1] > L[j]:
L[j-1], L[j] = L[j], L[j-1]
j -= 1
def main():
L = [1, 5, 8, 4, 3, 2, 1, 0]
n = 8
if find(L, n):
print(n, ' is in the list')
else:
print(n, ' is not in the list')
sort(L)
print('Soreted list:', L)
main()
Comments
Leave a comment