Program Stack Implementation – Case Study: Book Shelf Arrangement

BY IN Python Comments Off on Program Stack Implementation – Case Study: Book Shelf Arrangement ,

Refer to this case study:

Computer Science: Where’s my Sherlock Holmes?

Here’s the implementation in Python

#Program Stack Implementation
#Where’s My Sherlock Holmes?
#Initialize Stack
shelf1=[‘In The Deep Blue Ocean’,’The Stranger’,’Polly wants a Cracker’]
shelf2=[‘The Secret of Monkey Island’,”,”]
shelf3=[‘A Good Leader is a Good Servant’,’Quest of Rosetta’,’Le Chuck Revenge’]
shelf4=[‘The Bloddy Hell Mary’,’The Lost Necklace’,’The Magical Season of Christmas’]
shelf5=[‘Sherlock Holmes’,’Graduation Day’,”]

#Initialize pointer
top1=2
top2=0
top3=2
top4=2
top5=1
def isEmpty(shelf_no):
global top1,top2,top3,top4,top5
if (shelf_no==1):
if (top1==-1):
return True
else:
return False
if (shelf_no==2):
if (top2==-1):
return True
else:
return False
if (shelf_no==3):
if (top3==-1):
return True
else:
return False
if (shelf_no==4):
if (top4==-1):
return True
else:
return False
if (shelf_no==5):
if (top5==-1):
return True
else:
return False

def isFull(shelf_no):
global top1,top2,top3,top4,top5
if (shelf_no==1):
if (top1==2):
return True
else:
return False
if (shelf_no==2):
if (top2==2):
return True
else:
return False
if (shelf_no==3):
if (top3==2):
return True
else:
return False
if (shelf_no==4):
if (top4==2):
return True
else:
return False
if (shelf_no==5):
if (top5==0):
return True
else:
return False

def push(shelf_no,book_title):
global top1,top2,top3,top4,top5
if (shelf_no==1):
if (not isFull(1)):
top1=top1+1
shelf1[top1] = book_title
if (shelf_no==2):
if (not isFull(2)):
top2=top2+1
shelf2[top2] = book_title
if (shelf_no==3):
if (not isFull(3)):
top3=top3+1
shelf3[top3] = book_title
if (shelf_no==4):
if (not isFull(4)):
top4=top4+1
shelf4[top4] = book_title
if (shelf_no==5):
if (not isFull(5)):
top5=top5+1
shelf5[top5] = book_title

def pop(shelf_no):
global top1,top2,top3,top4,top5
if (shelf_no==1):
if (not isEmpty(1)):
isi=shelf1[top1]
shelf1[top1]=”
top1=top1-1
return isi
if (shelf_no==2):
if (not isEmpty(2)):
isi=shelf2[top2]
shelf2[top2]=”
top52top2-1
return isi
if (shelf_no==3):
if (not isEmpty(3)):
isi=shelf3[top3]
shelf3[top3]=”
top3=top3-1
return isi
if (shelf_no==4):
if (not isEmpty(4)):
isi=shelf5[top4]
shelf4[top4]=”
top4=top4-1
return isi
if (shelf_no==5):
if (not isEmpty(5)):
isi=shelf5[top5]
shelf5[top5]=”
top5=top5-1
return isi

def move(shelf_no_from,shelf_no_to):
global top1,top2,top3,top4,top5
book = pop (shelf_no_from)
push (shelf_no_to, book)

def output():
print (“Content of Shelf No. 1”)
i=2
while (i>=0):
print(shelf1[i])
i=i-1
print (“======================”)
print (“Content of Shelf No. 2”)
i=2
while (i>=0):
print(shelf2[i])
i=i-1
print (“======================”)
print (“Content of Shelf No. 3”)
i=2
while (i>=0):
print(shelf3[i])
i=i-1
print (“======================”)
print (“Content of Shelf No. 4”)
i=2
while (i>=0):
print(shelf4[i])
i=i-1
print (“======================”)
print (“Content of Shelf No. 5”)
i=2
while (i>=0):
print(shelf5[i])
i=i-1

print(“Shelf Position Before Arrangement”)
print(“\n”)
output()
move(5,2)
move(5,2)
move(1,5)
print(“\n”)
print(“Shelf Position After Arrangement”)
print(“\n”)
output()

 




Comments are closed.