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)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

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
Transcribed Image Text: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
Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education