Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python 3.x There is a lot of information in this Text file data to be used: steel_samurai URG-KG8_incident gramarye_murder airlines URG-dl6_incident URG-edgeworth reunion_turnabout larry_butz URG-SL9_incident
Python 3.x
There is a lot of information in this
Text file data to be used: steel_samurai URG-KG8_incident gramarye_murder airlines URG-dl6_incident URG-edgeworth reunion_turnabout larry_butz URG-SL9_incident doug turnabout_corner romein_letouse URG-mask*demasque URG-kidnapping
TStack.py:
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. 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,s = stack assert t == '__Stack__', 'Type Error : Expected __Stack__ but received '+t return len(s) == 0 else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) 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__', 'Type Error: Expected __Stack__ but received '+t return len(s) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) 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__', 'Type Error: Expected __Stack__ but received '+t s.append(value) else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) 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__', 'Type Error: Expected __Stack__ but received '+t return s.pop() else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack)) def peek(stack): """ 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 """ if type(stack) is tuple: t,s = stack assert t == '__Stack__', 'Type Error: Expected __Stack__ but received '+t return s[-1] else: assert False, 'Type Error: Expected __Stack__ but received '+str(type(stack))
TQueue.py:
def create(): """ Purpose creates an empty queue Return an empty queue """ return '__Queue__',list() def is_empty(queue): """ Purpose checks if the given queue has no data in it Pre-conditions: queue is a queue created by create() Return: True if the queue has no data, or false otherwise """ if type(queue) is tuple: t,q = queue assert t == '__Queue__', 'Type Error: Expected __Queue__ but received '+t return len(q) == 0 else: assert False, 'Type Error: Expected __Queue__ but received '+str(type(queue)) def size(queue): """ Purpose returns the number of data values in the given queue Pre-conditions: queue: a queue created by create() Return: The number of data values in the queue """ if type(queue) is tuple: t,q = queue assert t == '__Queue__', 'Type Error: Expected __Queue__ but received '+t return len(q) else: assert False, 'Type Error: Expected __Queue__ but received '+str(type(queue)) def enqueue(queue, value): """ Purpose adds the given data value to the given queue Pre-conditions: queue: a queue created by create() value: data to be added Post-condition: the value is added to the queue Return: (none) """ if type(queue) is tuple: t,q = queue assert t == '__Queue__', 'Type Error: Expected __Queue__ but received '+t q.append(value) else: assert False, 'Type Error: Expected __Queue__ but received '+str(type(queue)) def dequeue(queue): """ Purpose removes and returns a data value from the given queue Pre-conditions: queue: a queue created by create() Post-condition: the first value is removed from the queue Return: the first value in the queue """ if type(queue) is tuple: t,q = queue assert t == '__Queue__', 'Type Error: Expected __Queue__ but received '+t return q.pop(0) else: assert False, 'Type Error: Expected __Queue__ but received '+str(type(queue)) def peek(queue): """ Purpose returns the value from the front of given queue without removing it Pre-conditions: queue: a queue created by create() Post-condition: None Return: the value at the front of the queue """ if type(queue) is tuple: t,q = queue assert t == '__Queue__', 'Type Error: Expected __Queue__ but received '+t return q[0] else: assert False, 'Type Error: Expected __Queue__ but received '+str(type(queue))
Question 1 (10 points) Purpose: To practice file l/O, and the use of both Queues and Stacks ADT Degree of Difficulty: Easy Phoenix Wright ace attorney, takes a lot of court cases. There are two types of cases: urgent cases and non-urgent cases. Phoenix needs some help scheduling the order he should take his cases. A full list of all his court cases is on moodle (cases.txt). Each LINE in cases.txt is a different month, and each WORD on each line is a different case. A case beginning with 'URG' is an urgent case, and is of the utmost priority Write a Python script that opens cases.txt, reads all the lines in the file, and for EACH MONTH/LINE in the text file, repeat the following Display the number of the month (with the first line being month 1 Display all urgent cases, beginning with the most recent case (the rightmost urgent case on current line) Display all non-urgent cases in the order they appeared (starting with the leftmost non-urgent case on current line For our purposes here, a case is any text separated by one or more spaces that is, exactly the strings you get when you use the string method split). It is important to display the information for each month on the same line when printing to the console. You can do so by giving print the 'end' keyword parameter. Ex print("OB JECTION!", end- Example output is below: python a4q1.py cases.txt Month 1: URG-d16_incident URG - KG8_incident steel_samurai gramarye_murder airlines Month 2: URG-SL9_incident URG -edgeworth reunion_turnabout larry butz doug swallow Month 3: URG-kidnapping URG -mask*demasque turnabout corner romein letouse Notice the following: All information from the current line/month are displayed on a single on the console. The most recent URGENT cases are printed out first (going right to left). The sequence of non-urgent cases are in order of left to right. You can test your script informally: you will not need to hand in any formal testing beyond a few example runs. But you should run your script on a few different files. There are also some example text files on the A4 Moodle page that you can use to try your program out. For this program, you are required to use BOTH a stack and a queue for this question. Hint: there are two types of cases. Perhaps they should be stored using different data structures... Using the Stack and Queue ADT's Download the Stack and Queue ADT implementation named TStack.py and TQueue.py from the Assign ment 4 page on Moodle. To help you avoid errors, these implementations do not allow you to violate the ADT in a careless way. You do not need to understand the code in the files TStack.py and TStack.py You do not even need tolook at them. We will study simpler and more accessible implementations later in the course. You should focus on using the ADT 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, a runtime error will be caused
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started