Algorithms Exercise - Solution
Algorithms Exercise - Solution
following:
Sequences and summation
Determine if a given number is prime or not.
def isPrime(number):
for i in range(2,number):
if(number % i == 0):
print('not prime')
return
print('Yes it is prime')
#Call the function
isPrime(49)
isPrime(67)
def AvgNumbers(nlist):
total = 0
for n in nlist:
total = total + n
return total
n! = 1 * 2 * 3* 4… n
def factorial(n):
f = 1
for i in range(2,n+1):
f = f * i
print f
#Call the function
factorial(5)
Lists
Find the maximum number in list
def FindMax(lstNumbers):
maxn = numbers[0]
for n in lstNumbers:
if n > maxn:
maxn = n
print('Maximum number is ', maxn)
#call function
lst = [3,10,34,11,67,2]
FindMax(lst)
OR New
Find a number in a list using Binary Search and print its position
def binarySearch(lstNumbers, searchValue):
low = 0
high = len(lstNumbers) - 1
while (low <=high):
mid = (low + high)//2 #integer division
if (searchValue == lstNumbers[mid]):
print('Number found at ', mid)
return
elif (searchValue < lstNumbers[mid]):
high = mid -1
else:
low = mid + 1
print('not found')
#call function
lst = [12,23,33,45,49,62,74,77,89]
binarySearch(lst,56)
binarySearch(lst,74)
binarySearch(lst,33)
'''
def Swap(x,y):
if (x > y):
z = x
x = y
y = z
print (x, y)
Swap(8,3)
Swap(12,19) #Because 12 is less than 19 it wont swap
'''
'''
def printList1(numbers):
for x in numbers :
print (x)
printList1([10,3,5,16,4])
'''
'''
def PrintList2(numbers):
for i in range (len(numbers)):
print (numbers [i])
PrintList2([4,18,12,64,29,11])
'''
'''
#write a function that print the sum of numbers in a list
def sumList(numbers):
total = 0
for x in numbers :
total = total + x
print(total)
sumList([2,5,1,10])
'''
'''
#write a function that print the average of numbers in a list
def avgList(numbers):
total = 0
for x in numbers :
total = total + x
avg = total / len(numbers)
print(avg)
avgList([2,5,1,8])
'''
'''
#write a function to search a number in a list
def SearchNumber(numbers, x):
found = False
for n in numbers:
if n == x:
found = True
break # to exit the loop
if(found):
print("Number is Found")
else:
print("Not Found")
SearchNumber([3,5,12,9], 8)
'''
'''
#modify the function to find a number and also print the location where
the number was found
if(found):
print("Number is Found")
else:
print("Not Found")
lst = [3,10,34,11,67,2]
SearchNumber(lst,11)
SearchNumber(lst,2)
SearchNumber(lst,13)
'''
'''
def FindMax(numbers):
maxn = 0
for n in numbers:
if n > maxn :
maxn = n
print("Your Maximum number is ", maxn)
'''
def PrintEven(lstNumbers):
count = 0
for n in lstNumbers:
if (n%2 == 0): # check if n is divisable by 2, no remainders
count = count + 1 #increase counter by 1
print(count)
lst = [2,4,6,8,13,11]
PrintEven(lst)
def PrintEven2(lstNumbers):
count = 0
for i in range (len(lstNumbers)):
if ( lstNumbers[i] % 2 == 0):
count = count + 1
print(count)
#test function
lst = [30,7,2,99,12,11]
PrintEven2(lst)
'''
'''
# Prime number
def IsPrime(num):
for n in range (2,num):
if (num%n == 0):
print(num, "Is Not a Prime number")
return #exit the function / break -> get out from loop
#test function
num = int(input("Enter the number : "))
IsPrime(num)
'''
'''
#Linear Search ~Sorted~ ﻣﺮﺗﺐ
def LinearSearchSorted(lstNumbers, target):
for n in lstNumbers:
if(n > target):
print ("Not Found")
return
elif (n == target):
print("Found")
return
lst = [2,4,8,10,45,54,66]
LinearSearchSorted(lst, 55)
LinearSearchSorted(lst, 66)
'''
'''
#Linear Search not sorted
def LinearSearch(lstNumbers, target):
for n in lstNumbers:
if(n == target):
print ("Found")
return
print("Not Found")
lst = [2,4,8,10,45,54,66]
LinearSearch(lst, 55)
LinearSearch(lst, 66)
'''
'''
#Linear Search not sorted find the ~index~
def LinearSearchIndex(lstNumbers, target):
for i in range(len(lstNumbers)):
if(lstNumbers[i] == target):
print ("Found")
return i
print("Not Found")
return -1
'''
def binarySearch (lstN, sv):
low = 0
high = len(lstN) - 1
while (low <= high):
mid = (low + high) / 2
if (sv == lstN[mid]):
print("Number found at", mid)
return
elif (sv > lstN[mid]):
low = mid + 1
else:
high = mid - 1
print("Not Found")
'''
def printList(lstNumbers):
for i in range (len(lstNumbers)):
print (lstNumbers[i])
#test function
lst = [2,4,8,10,45,54,66]
printList(lst)
'''
'''
def printList(lstNumbers):
i=6
while (i < len(lstNumbers)):
print (lstNumbers[i])
lst = [2,4,8,10,45,54,66]
printList(lst)
'''