Write PUSH (Books) and POP (Books) methods in python to add Books and remove Books considering them to act as Push and Pop operations of Stack.
def push(Books):
Stack.append(Books)
print 'Element:', Book, 'inserted successfully'
def pop():
if Stack == []:
print('Stack is empty!')
else:
print('Deleted element is',Stack.pop())
Evaluate the following postfix notation of expression. Show status of the stack after every operation.
84, 62, –, 14, 3, *, +
Stack status of every operation is given below:
Element | Stack |
84 | 84 |
62 | 84,62 |
- | 22 |
14 | 22,14 |
3 | 22, 14, 3 |
* | 22, 42 |
+ | 64 |
Write a method in python to find and display the prime numbers between 2 to N. Pass N as an argument to the method.
def prime_numbers(N):
for I in range(2, N+1):
M = I // 2
IsPrime=1
for J in range(2, M+1):
if I % J == 0:
IsPrime=0
break
if IsPrime==1:
print(I)
Consider the following definition of class Staff, write a method in python to search and display the content in a pickled file staff.dat, where Staff code is matching with ‘S0105’.
class Staff:
def __init__(self,S,SNM):
self.Staffcode=S
self.Name=SNM
def Show(self):
print(self.Staffcode,' ',
self.Name)
def search():
f = open('staff.dat', 'rb')
try:
while True:
e = pickle.load(f)
if e.Staffcode == ‘S0105’:
e.Show()
except EOFError:
pass
f.close()