I'm getting a different output. I need to get the expected output Python Code: from Stack import Stack def display_(S_): node = S_.list.head while node != None: print(node.data) node = node.next def Sorting_Stack(num_stack): temp_head1 = num_stack.list.head # sorting the strings in the stack in descending order using bubble sort while(temp_head1!=None): temp_head2 = temp_head1.next # looping through to end and placing the greatest element at the current position while(temp_head2!=None): # if next element of the current is greater then swapping if(temp_head2.data>temp_head1.data): temp_d = temp_head1.data temp_head1.data = temp_head2.data temp_head2.data = temp_d temp_head2 = temp_head2.next temp_head1 = temp_head1.next if __name__ == '__main__': # creatin stack str_stack = Stack() # taking user input till sentinel value is entered inp_str = input() while (inp_str != "End"): str_stack.push(inp_str) inp_str = input() if (str_stack.list.head == None): print("Empty Stack, enter a valid string!") inp_str = input() str_stack.push(inp_str) # sorting and displaying the sorted stack Sorting_Stack(str_stack) display_(str_stack)
I'm getting a different output. I need to get the expected output
Python Code:
from Stack import Stack
def display_(S_):
node = S_.list.head
while node != None:
print(node.data)
node = node.next
def Sorting_Stack(num_stack):
temp_head1 = num_stack.list.head
# sorting the strings in the stack in descending order using bubble sort
while(temp_head1!=None):
temp_head2 = temp_head1.next
# looping through to end and placing the greatest element at the current position
while(temp_head2!=None):
# if next element of the current is greater then swapping
if(temp_head2.data>temp_head1.data):
temp_d = temp_head1.data
temp_head1.data = temp_head2.data
temp_head2.data = temp_d
temp_head2 = temp_head2.next
temp_head1 = temp_head1.next
if __name__ == '__main__':
# creatin stack
str_stack = Stack()
# taking user input till sentinel value is entered
inp_str = input()
while (inp_str != "End"):
str_stack.push(inp_str)
inp_str = input()
if (str_stack.list.head == None):
print("Empty Stack, enter a valid string!")
inp_str = input()
str_stack.push(inp_str)
# sorting and displaying the sorted stack
Sorting_Stack(str_stack)
display_(str_stack)
![Input
Your output
Expected output
End
End
roar
room
boom
zoom
End
Empty Stack, enter a valid string!
End
Empty Stack, enter a valid string!
Empty Stack, enter a valid string!
zoom
room
roare
boome](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F45d19586-ebc3-4f1a-8702-47e93c6193b1%2Fc545cc1b-aefc-4a14-a768-c557a1487b26%2F8rkim35_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
Need to fix code to pass the final output in image
Python code:
from Stack import Stack
def display_(S_):
node = S_.list.head
while node != None:
print(node.data)
node = node.next
def Sorting_Stack(num_stack):
temp_head1 = num_stack.list.head
# sorting the strings in the stack in descending order using bubble sort
while(temp_head1!=None):
temp_head2 = temp_head1.next
# looping through to end and placing the greatest element at the current position
while(temp_head2!=None):
# if next element of the current is greater then swapping
if(temp_head2.data>temp_head1.data):
temp_d = temp_head1.data
temp_head1.data = temp_head2.data
temp_head2.data = temp_d
temp_head2 = temp_head2.next
temp_head1 = temp_head1.next
if __name__ == '__main__':
# creatin stack
str_stack = Stack()
# taking user input till sentinel value is entered
inp_str = input()
while (inp_str != "End"):
str_stack.push(inp_str)
inp_str = input()
if (str_stack.list.head == None):
print("Empty Stack, enter a valid string!")
inp_str = input()
str_stack.push(inp_str)
# sorting and displaying the sorted stack
Sorting_Stack(str_stack)
display_(str_stack)
![In this lab you are asked to complete the code provided below so that it:
• accepts strings as input from the user and generates a stack till the user enters the input sentinel string "End"
• your code should ensure that the stack is not empty, if it is then it should display "Empty Stack, enter a valid string!", and allow
the user to enter a string
• Next, it should sort the given stack into descending order. There are a number of ways you could approach this:
• implement one of the sorting algorithms that you have seen (e.g. selection sort)
• use an intermediate data structure (e.g. a python list)
• use a STACK (this is a more challenging exercise - I will set it as an additional lab exercise for final challenge revision)
Finally, it should print the sorted stack
1: Compare output
.
Input
Your output
rose
more
apple
banana
zoo
End
zoo
rose
more
banana
apple
2: Compare output
3: Compare output
Your output
Output differs. See highlights below.
Input
Input
Your output
Expected output
End
End
roar
room
boom
zoom
End
Checking
End
Checking
Special character legend
Empty Stack, enter a valid string!
End
Empty Stack, enter a valid string!
Empty Stack, enter a valid string!
zoome
room
roard
boom](https://content.bartleby.com/qna-images/question/45d19586-ebc3-4f1a-8702-47e93c6193b1/5a3c4e39-d5ee-4701-be5c-a2bfa5f4f4a1/lvf0y1g_thumbnail.png)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)