0% found this document useful (0 votes)
24 views

AI Lab Assignment # 2

This document contains an assignment submission for a computer science program. It includes definitions for several functions: buyFruit() to calculate fruit costs, sum() and sub() for basic math operations, ifMainFunction() to check the calling context, and insertionSort(), mergesort(), and quicksort() to implement different sorting algorithms. It also contains sample calls to these functions and sorts a sample list at the end.

Uploaded by

Maryam Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

AI Lab Assignment # 2

This document contains an assignment submission for a computer science program. It includes definitions for several functions: buyFruit() to calculate fruit costs, sum() and sub() for basic math operations, ifMainFunction() to check the calling context, and insertionSort(), mergesort(), and quicksort() to implement different sorting algorithms. It also contains sample calls to these functions and sorts a sample list at the end.

Uploaded by

Maryam Qazi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ARTIFICIAL INTELLIGENCE

ASSIGNMENT # 2
Name: Anas Qazi
Reg#: 36444
Program: BSCS

fruitPrices = {'apples':2.00, 'oranges': 1.50, 'pears': 1.75}

def buyFruit(fruit, numPounds):


cost = fruitPrices[fruit] * numPounds
d=cost
print("That'll be %f please" % (d))

def sum(a,b):
s=a+b
print (s)

def sub(a,b):
subb = a-b
print(subb)

def ifMainFunction():
if __name__=='main':
print("You Are In Main Function")
else:
print("NO You Are Not In Main Fucntion")

#Insertion Sort
def insertionSort(list):
for index in range(1,len(list)):
value = list[index]
i=index-1
while i>=0:
if list[index]>value:
list[i+1]=list[i]
list[i]=value
i=i-1
#MergeSort
def mergesort(A):
mergesort2(A,0,len(A)-1)

def mergesort2 (A, first, last):


if first<last:
middle = (first+last)//2
mergesort2(A,first,middle)
mergesort2(A,middle+1,last)
merge(A,first,middle,last)
def merge(A,first,middle,last):
L= A[fist:middle]
R = A[middle:last+1]
L.append(99999999)
R.append(99999999)
i=j=0
for k in range (first,last+1):
if L[i]<=R[j]:
A[k]=L[i]
i+=1
else:
A[k]=R[j]
j+=1

#Quick Sort
def quicksort(A):
quicksort2(A,0,len(A)-1)

def quicksort2(A,low,hi):
if low<hi:
p=partition(A,low,hi)
quicksort2(A,low,p-1)
quicksort2(A,p+1,hi)

def getPivot(A,low,hi):
mid=(hi+low)//2
pivot=hi
if A[low]<A[mid]:
if A[mid]<A[hi]:
pivot=mid
elif A[low]<A[hi]:
pivot=low
return pivot

def partition(A,low,hi):
pivotindex = getpivot(A,low,hi)
pivotvalue=A[pivotIndex]
A[pivotIndex],A[low]=A[low],A[pivotIndex]
border=low
for i in range(low,hi+1):
if A[i]<pivotValue:
border+=1
A[i],A[border]=A[border],A[i]
A[low],A[border]=A[border],A[low]
return (border)

def quicksort2(A,low,hi):
if hi-low<threshold and low <hi:
quickselection(A,low,hi)
elif low < hi:
p=partition(A,low,hi)
quicksort2(A,low,p-1)
quicksort2(A,p+1,hi)
# Main Function

sum(2,7)
buyFruit('apples',2.4)
sub(1,7)
ifMainFunction()
list=['1','2','34','5','5']
insertionSort(list)
mergesort(list)
quicksort(list)

You might also like