Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Purpose: To practice using a Stack as an algorithmic tool (Chapter 8). Degree of Difficulty: Moderate. Important Note - Read this unless you want zero

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Purpose: To practice using a Stack as an algorithmic tool (Chapter 8). Degree of Difficulty: Moderate. Important Note - Read this unless you want zero marks The purpose of this question is to practice and achieve mastery of the Stack ADT as an algorithmic tool. Your program must use the given Stack ADT for this. Download the Stack ADT implementation named TStack.py from the Assignment 4 page on Moodle. Your script for this question should import this module. To help you avoid errors, this implementation of the Stack ADT does not allow you to violate the ADT in a careless way. You do not need to understand the code in the file TStack.py. You will not be tested on this implementation. The tricky Python code in this ADT is only used to cause a run-time error if you use the ADT the wrong way, and is not to be taken as an example of ADT design. We will study simpler and more accessible implementations starting with Chapter 10. You should focus on using the Stack operations correctly. TStack.py has the same Stack ADT interface, and has been thoroughly tested. If your script does not use the Stack ADT correctly, or if you violate the ADT Principle, this implementation will cause a runtime error. If you see errors coming from the TStack.py module, it's almost certainly because you used the operations incorrectly. You will get zero marks if any of the following are true for your code: Your script uses the reverse() method for lists, or anything similar for strings. . Your script uses the extended slice syntax for lists or strings to reverse the data. Your script does not use the Stack ADT in a way that demonstrates mastery of the stack concept. Task Design and implement a Python script that opens a text file, reads all the lines in the file, and displays it to the console as follows: The lines are displayed in reverse order, the first line in the file is the last line displayed. The word order in the line is reversed; the first word in a line from the file is the last word displayed on a line in the console. The lines are displayed in reverse order, the first line in the file is the last line displayed. The word order in the line is reversed; the first word in a line from the file is the last word displayed on a line in the console. The characters in each word are not reversed; the first letter of each word in the file appears as the first letter in the word displayed. For our purposes here, a word is any text separated by one or more spaces, that is, exactly the strings you get when you use the string method split(). So normal punctuation may look a bit weird when you run this script, that's okay! For example, suppose you have a text file named months.txt with the following three lines: January February March April May June July August. September October November December Running your script on the console should produce the following output: December November October September August. July June May April March February January Notice: The sequence of the lines displayed is reversed compared to the file. The sequence of words displayed on a line is reversed compared to the file The sequence of characters in the words are not reversed. What to Hand In Your implementation of the program: a4q2.py. . Your test script a4q2_testing.py Copy/paste a few examples of your script working on some files (this will suffice as system testing): a4q2_output.txt. Be sure to include your name, NSID, student number, course number and lecture section at the top of all documents. Evaluation 2 marks: Your program displays the reversed contents of the file to the console. . 3 marks: Your program uses the Stack ADT to reverse the order of the lines in the file. . 3 marks: Your program uses the Stack ADT to reverse the order of the words on each line of text (string) 4 marks: Your test script demonstrates adequate testing. . 3 marks: Your output file demonstrates the program working on at least 3 examples. Note: Use of reverse(). or extended slices, or any other technique to avoid using Stacks will result in a grade of zero for this question. # Defines the Stack ADT # TStack.pl # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (Last-in first-out) protocol. # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. # This implementation is not to be considered a good example of anything. # It simply prevents casual and mistaken violations of the ADT Principle. _stack_emsg = "If you're seeing this message, you gave the wrong 1st argument to Stack." def create(): Purpose creates an empty stack Return an empty stack return '_Stack_', list) def is empty(stack): Purpose checks if the given stack has no data in it Pre-conditions: stack is a stack created by create() Return: True if the stack has no data, or false otherwise if type (stack) is tuple: t,5 = stack assert t == '_Stack_', _stack_emsg.format('is_empty()) return len(s) == 0 else: assert False, _stack_emsg.format("is_empty()) def size (stack): Purpose returns the number of data values in the given stack Pre-conditions: stack: a stack created by create Return: The number of data values in the queue if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('size()') return len(s) else: assert False, _stack_emsg.format('size()') def push(stack, value): Purpose adds the given data value to the given stack Pre-conditions: queue: a stack created by create() value: data to be added Post-condition: the value is added to the stack Return: (none) if type (stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('push()') s.append(value) else: assert False, _stack_emsg.format('push') def pop(stack): Purpose removes and returns a data value from the given stack Pre-conditions: stack: a stack created by create() Post-condition: the top value is removed from the stack Return: returns the value at the top of the stack if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('pop()') return s.pop() else: assert False, _stack_emsg.format('pop()') 99 def peek(stack): Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() 100 101 def peek (stack): 102 103 104 105 106 107 108 109 110 111 Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() Post-condition: None Return: the value at the front of the stack 112 113 114 115 if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('peek()') return s[-1] else: assert False, _stack_emsg.format('peek(') 116 117 118 119 Purpose: To practice using a Stack as an algorithmic tool (Chapter 8). Degree of Difficulty: Moderate. Important Note - Read this unless you want zero marks The purpose of this question is to practice and achieve mastery of the Stack ADT as an algorithmic tool. Your program must use the given Stack ADT for this. Download the Stack ADT implementation named TStack.py from the Assignment 4 page on Moodle. Your script for this question should import this module. To help you avoid errors, this implementation of the Stack ADT does not allow you to violate the ADT in a careless way. You do not need to understand the code in the file TStack.py. You will not be tested on this implementation. The tricky Python code in this ADT is only used to cause a run-time error if you use the ADT the wrong way, and is not to be taken as an example of ADT design. We will study simpler and more accessible implementations starting with Chapter 10. You should focus on using the Stack operations correctly. TStack.py has the same Stack ADT interface, and has been thoroughly tested. If your script does not use the Stack ADT correctly, or if you violate the ADT Principle, this implementation will cause a runtime error. If you see errors coming from the TStack.py module, it's almost certainly because you used the operations incorrectly. You will get zero marks if any of the following are true for your code: Your script uses the reverse() method for lists, or anything similar for strings. . Your script uses the extended slice syntax for lists or strings to reverse the data. Your script does not use the Stack ADT in a way that demonstrates mastery of the stack concept. Task Design and implement a Python script that opens a text file, reads all the lines in the file, and displays it to the console as follows: The lines are displayed in reverse order, the first line in the file is the last line displayed. The word order in the line is reversed; the first word in a line from the file is the last word displayed on a line in the console. The lines are displayed in reverse order, the first line in the file is the last line displayed. The word order in the line is reversed; the first word in a line from the file is the last word displayed on a line in the console. The characters in each word are not reversed; the first letter of each word in the file appears as the first letter in the word displayed. For our purposes here, a word is any text separated by one or more spaces, that is, exactly the strings you get when you use the string method split(). So normal punctuation may look a bit weird when you run this script, that's okay! For example, suppose you have a text file named months.txt with the following three lines: January February March April May June July August. September October November December Running your script on the console should produce the following output: December November October September August. July June May April March February January Notice: The sequence of the lines displayed is reversed compared to the file. The sequence of words displayed on a line is reversed compared to the file The sequence of characters in the words are not reversed. What to Hand In Your implementation of the program: a4q2.py. . Your test script a4q2_testing.py Copy/paste a few examples of your script working on some files (this will suffice as system testing): a4q2_output.txt. Be sure to include your name, NSID, student number, course number and lecture section at the top of all documents. Evaluation 2 marks: Your program displays the reversed contents of the file to the console. . 3 marks: Your program uses the Stack ADT to reverse the order of the lines in the file. . 3 marks: Your program uses the Stack ADT to reverse the order of the words on each line of text (string) 4 marks: Your test script demonstrates adequate testing. . 3 marks: Your output file demonstrates the program working on at least 3 examples. Note: Use of reverse(). or extended slices, or any other technique to avoid using Stacks will result in a grade of zero for this question. # Defines the Stack ADT # TStack.pl # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (Last-in first-out) protocol. # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. # This implementation is not to be considered a good example of anything. # It simply prevents casual and mistaken violations of the ADT Principle. _stack_emsg = "If you're seeing this message, you gave the wrong 1st argument to Stack." def create(): Purpose creates an empty stack Return an empty stack return '_Stack_', list) def is empty(stack): Purpose checks if the given stack has no data in it Pre-conditions: stack is a stack created by create() Return: True if the stack has no data, or false otherwise if type (stack) is tuple: t,5 = stack assert t == '_Stack_', _stack_emsg.format('is_empty()) return len(s) == 0 else: assert False, _stack_emsg.format("is_empty()) def size (stack): Purpose returns the number of data values in the given stack Pre-conditions: stack: a stack created by create Return: The number of data values in the queue if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('size()') return len(s) else: assert False, _stack_emsg.format('size()') def push(stack, value): Purpose adds the given data value to the given stack Pre-conditions: queue: a stack created by create() value: data to be added Post-condition: the value is added to the stack Return: (none) if type (stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('push()') s.append(value) else: assert False, _stack_emsg.format('push') def pop(stack): Purpose removes and returns a data value from the given stack Pre-conditions: stack: a stack created by create() Post-condition: the top value is removed from the stack Return: returns the value at the top of the stack if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('pop()') return s.pop() else: assert False, _stack_emsg.format('pop()') 99 def peek(stack): Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() 100 101 def peek (stack): 102 103 104 105 106 107 108 109 110 111 Purpose returns the value from the front of given stack without removing it Pre-conditions: stack: a stack created by create() Post-condition: None Return: the value at the front of the stack 112 113 114 115 if type(stack) is tuple: t,s = stack assert t == '_Stack_', _stack_emsg.format('peek()') return s[-1] else: assert False, _stack_emsg.format('peek(') 116 117 118 119

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions