Python Practice Stack1

In this class, We do Python Practice Stack1.

For Complete YouTube Video: Click Here

Stack Examples

The reader should have prior knowledge of Data Structures and Python. Please check our Data structures course.

For python, Click here.

The examples we discuss help the reader to improve his coding skills. They can easily crack the placement exams.

For improvement, take our practice tests in the placement training course.

Q1) Consider the following in_stack
in_stack(Top to Bottom)5,4,3,2,1

What is the content of the out_stack from top to bottom.
after executiong the below function which takes in_stack as parameter.

def alter_stack(in_stack):
    out_stack=Stack(5)
    while(not in_stack.isempty()):#isempty will return true if stack is empty
        val=in_stack.pop()
        if (not in_stack.isempty()):
            temp_val=in_stack.pop()
            out_stack.push(val+temp_val)#push and pop are used to insert and delete element from queue
            in_stack.push(temp_val)
        else:
            out_stack.push(val+2)
    return out_stack

In the above example, we must analyze the program and identify the elements present in out_stack after executing the function alter_stack.

The function alter_stack is taking the input stack object In_stack.

The stack class is provided in python. The description of methods of stack class is given in the program.

The while loop in the above program run till the input_stack is empty.

In the loop, they are taking the top two elements from the input_stack and adding the elements.

The sum value of the top two elements is pushed into out_stack, and the second top element is pushed to in_stack.

Analyze the program for better practice.

Step by step explanation is provided in the video.

After executing the function alter_stack. The stack Out_stack contain elements 3,3,5,7,9. Top to bottom.

Q2) Consider the following in_stack and in_list
in_stack(top to bottom)"t","a","e","w","s"
in_list(head to tail) 4->2->1->6

What will be the content of in_stack from top to bottom
after executing the below function where head of the in_list and in_stack are send as parameters

def fuction(head,in_stack):
    if(head==None):
        return
    else:
        temp_stack=Stack(in_stack.get_max_size())
        count1=0
    while(not in_stack.isempty()):
        element=in_stack.pop()
        if (count1 != head.getdata()):
            temp_stack.push(element)
        count1+=2
    while(not temp_stack.isempty()):
        in_stack.push(temp_stack.pop())
    return function(head.getnext(),in_stack)

In the above example, a stack object and a list object is given as input to the function.

The details of the methods of stack and list class are provided in the program.

The function is taking the head of the list and stack as input.

Analyze the code and identify the elements present in In_stack after executing the function.

Step by step explanation is provided in the video.

For better practice, analyze and then check for a solution.

Output: t,w,s. the elements in the in_stack from top to bottom after execution.