# PRIP 10.1 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 10.1 Sumita Arora Solutions for class 12 Computer Science. To view Sumita Arora solutions for all chapters, visit here.

Q.1: Given a stack holding some years. Answer the following questions :

(a) Which stack element(s) can be accessed ?
Answer: only last inserted element can be accessed

(b) Name three operations which can be performed on a stack.
push: to push element on the top of stack
pop: to remove element from the top of stack
top: to get the element on the top of the stack

(c) A stack is called a LIFO structure. What does this mean ?
LIFO stands for Last In First Out – last added element will get out first

(d) Give an example of an application of the stack.

1. undo operation in text editor
2. used to evaluate prefix, postfix and infix expressions
3. String reversal
Q.2: Show the Stack’s status after each of the following operations. Show position of top by underlining the element. Stack s is initially empty.
`(a) s.push(20) (c) s.pop() (e) s.pop()(b) s.push(51) (d) s.pop() (f) s.push(43)`

a) [ 20 ]
b) [ 20, 51 ]
c) [ 20 ]
d) []
e) []
f) [ 43 ]

Q.3: Evaluate following postfix expression using a stack:
`30, 5, 2, **, 12, 6, /, +, -, 3`

`Code:def evaluate(a, i, b):    a = int(a)    b = int(b)    if i == '+':        return b+a    if i == '-':        return b-a    if i == '*':        return b*a    if i == '**':        return b**a    if i == '/':        return b/a    if i == '%':        return b%a        def postfix_evaluate(expression):    operators = "+ * / - ** % ^"    stack = []    for i in expression:        if i in operators:    # if i is operator            a = stack.pop()   # pop two operands            b = stack.pop()               ans = evaluate(a,i,b) # evaluate their result            stack.append(ans)     # push result into stack         else:            stack.append(int(i)) # append works same as push in list        print(stack)  # to see status of stack each time    return stack.pop()#__main__    expression = "30, 5, 2, **, 12, 6, /, +, -, 3"expression = expression.split(", ")print("Answer is: {}".format(postfix_evaluate(expression) ))      Output:[30, 5][30, 5, 2][30, 25][30, 25, 12][30, 25, 12, 6][30, 25, 2.0][30, 27][3, 3]Answer is: 3`
Q.4: Add a function` StackSize()` to above program that returns the number of elements on the stack.

`Code:def StackSize(stack):    return len(stack)def evaluate(a, i, b):    a = int(a)    b = int(b)    if i == '+':        return b+a    if i == '-':        return b-a    if i == '*':        return b*a    if i == '**':        return b**a    if i == '/':        return b/a    if i == '%':        return b%a     def postfix_evaluate(expression):    operators = "+ * / - ** % ^"    stack = []    for i in expression:        if i in operators:    # if i is operator            a = stack.pop()   # pop two operands            b = stack.pop()               ans = evaluate(a,i,b) # evaluate their result            stack.append(ans)     # push result into stack         else:            stack.append(int(i)) # append works same as push in list        print(stack, end=' ')  # to see status of stack each time        size = StackSize(stack)        print("stack size: {}".format(size))  # print stacksize      return stack.pop()    expression = "30, 5, 2, **, 12, 6, /, +, -, 3"expression = expression.split(", ")print("Answer is: {}".format(postfix_evaluate(expression) )) Output: stack size: 1[30, 5] stack size: 2[30, 5, 2] stack size: 3[30, 25] stack size: 2[30, 25, 12] stack size: 3[30, 25, 12, 6] stack size: 4[30, 25, 2.0] stack size: 3[30, 27] stack size: 2 stack size: 1[3, 3] stack size: 2Answer is: 3`
Q.5: Write a program to implement a stack of years (4 digit years) storing the years when Olympics were held in Asia or Europe.

Answer: for solution stack functionality is implemented using class

`Code:class stack:          # class stack     def __init__(self):    # init function        self.stack = list() # sreated a empty list            def push(self, year):    # fucntion to push year into stack        self.stack.append(year)        def pop(self):           # function to pop year from stack        self.stack.pop()        def top(self):           # function to get top year from stack        return self.stack[-1]    def isEmpty(self):       # fucntion to check if stack is empty        if len(self.stack)==0:            return True        return False#__main__s = stack()             # created a object of stacks.push(1896)            # pushed all years in stacks.push(1900)s.push(1908)s.push(1912)s.push(1916)s.push(1920)s.push(1924)s.push(1928)s.push(1936)s.push(1940)s.push(1944)s.push(1948)s.push(1952)s.push(1956)s.push(1960)s.push(1964)s.push(1988)s.push(2008)print("Years in decending order:")    #printing stackwhile(not s.isEmpty()):               # while loop till stack is empty    print(s.top())                    # print top year     s.pop()                           # pop top year  Output:Years in decending order:  200819881964196019561952194819441940193619281924192019161912190819001896    `
Q.6: Write a program to print a string in reverse order.
Hint: Extract individual characters from string and push in a stack; once done, then keep popping from stack.

`Code:def isEmpty(stack):    if(len(stack))==0:        return True    return Falsedef reverse(str):    stack = []        #create a empty stack        for i in str:     # push each character to stack        stack.append(i)            output = ""       # create a empty string        while(not isEmpty(stack)): # till stack becomes empty append each charecter to output string        temp = stack.pop()        output+=temp        return output         # return output stringreverse("Welcome to computer tutor")Output:'rotut retupmoc ot emocleW'`
Q.7: A palindrome is a string which is the same whether the characters are read from left to right or from right to left. For example, “radar“, “deed“, and “able was I ere I saw elba” are all examples of palindromes. There is a simple algorithm to determine whether a string is a palindrome given here:
push each letter of the string on a stack. Also place the character into a character array, say `str`.

`Set j = 0 and done = false while the stack is not empty and not (done)    pop a character, say ch    if str(s) == ch then         increment j and continue     else set done to true (the string isn't a palindrome)if done is true     the string isn't a palindrome else it is.`

Write a Python program implementing the above algorithm.

`Code:def isEmpty(stack):    if(len(stack)) == 0:        return True    return Falsedef check_palindrome(string):    str1 = []    stack = []     for i in string:        stack.append(i)   # pushing to stack        str1.append(i)     # adding to character array/list    j = 0    done = False    while(not isEmpty(stack) and not done):        ch = stack.pop()        if(str1[j]== ch):            j+=1        else:            done = True            break    if(done):        print("String is not palindrome")    else:        print("String is  palindrome")             #__main__string = "deed"check_palindrome(string)string = "able was I ere I saw elba"check_palindrome(string)string = "computer tutor"check_palindrome(string) Output:String is  palindromeString is  palindromeString is not palindrome`

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of 