All your Writing needs covered

[SOLVED] using python to do the assignment

Calculate the price
of
your order:

275 words
+
Approximate price
$ 0.00

Although people may be very accustomed to reading and understanding calculations like those in the preceding assignments, it is evident they are not very self-evident or obvious in the world of computing. One way to simplify the problem is just to redefine how expressions are represented, to allow for simpler interpretation.

One knows that a plus sign means to add, but typically when a plus sign is encountered in the middle of an expression, there is not yet enough information to know what values are to be added. One has to continue scanning the expression to find the second operand, and also decide if there are other operations of higher precedence that must take place first. How much simpler it would be if a parser could know to add just as soon as the plus sign appears.

This is the motivation for a representation called postfix notation, in which all operators follow their operands. Here are some illustrations.

Infix Representation Postfix Representation Operations
(1 + 2) * 3 1 2 + 3 * Add 1 and 2, then multiply by 3
1 + (2 * 3) 1 2 3 * + Multiply 2 and 3, then add to 1
(1+2) * (6-4) 1 2 + 6 4 – * Add 1 and 2, subtract 4 from 6, multiply

Hewlett Packard has produced a line of calculators that expected all inputs to be in postfix form, so every operation key could compute the moment it was pressed.

The goal of this assignment is to convert an infix expression into a postfix expression. This can be accomplished using the same algorithm as was used to evaluate the infix expression, simply yielding a new expression instead of a computed value. In the last example, instead of adding, it would produce an expression ending with a plus sign. The multiplication function would similarly produce an expression from its operands, followed by the multiplication operator.

Applying a Linked List

One of the purposes of this course is to find the data structures that would assist in producing the best results as efficiently as possible. The linked list is quite serviceable for the needs of this assignment.

A linked list will be useful in the calculation portion of this assignment. As the postfix expression is scanned, values must be saved until an operator is discovered. Each operator would apply to the two values that precede it, and then its result would also be saved. As an extreme example, consider this expression:

      1 2 3 4 5 * - + -              multiply 4 * 5, subtract from 3, add to 2,  subtract from 1  

Note this is not at all the same meaning as:

1 2 * 3 - 4 + 5 -              or           4 5 * 3 - 2 + 1 -

(If you need to more clearly see the difference, try inserting parentheses around all operations, such that each parentheses consists of two expressions followed by an operator.)

Defining a Linked List

The linked list is a rather simple data structure, and the required operations should be rather simple, so very little will be said here about what to do. Instead, here is a quick highlight of what should appear in your implementation. For consistency, call the implementation file linkedlist.py

Required Features in Implementation
Nested Node class definition for single-linked list node
  with __slots__ to save list memory
  and __init__ a constructor
__init__() a constructor for an empty list
push(value) add a value in constant time
pop() retrieve last insertion in constant time
Good functions for Completeness / Practice
top() return last insertion, without removal
is_empty() Identify whether list is empty
__len__() returns number of elements in list
__iter__() iterator function (with yield statements)
__str__() represents entire list as a string 

For full credit, this must beno worse than log-linear time,  not quadratic

The __iter__ function will be used to traverse a list to support __str__.

__str__ would allow a list to be an argument to print() for debugging

Assignment Specifications

Three new files are to appear in the solution to this assignment:

linkedlist.py
Implements a linked list as a class
infixtoiter.py
given an iterator to an infix expression,

produces a generator for a postfix expression
evalpostfix.py
evaluates a postfix expression, given an iterator

Do include newsplit.py in your submission since it is a necessary part of the project.

You are also encouraged to insert your own unit testing into each file, but that will not be itself graded.

Helpful Time-Saving Hint: 

One feature of an interpreted language like Python is to create code at run-time to execute. You can support all the calculations by taking advantage of this feature:

left="2"
right="2"
op = '+'
eval( left+op+right )       # evaluate "2+2" to get 4

This time-saving will become even more useful when we will support all the relation operators (such as > and ==)

Testing and Evaluation

Here are a few lines of code from the instructor’s solution and test at this time of writing:

(in infixtoiter.py)
from peekable import Peekable, peek
from newsplit import new_split_iter

def to_postfix( expr ):
    return postfix_sum(Peekable(new_split_iter(expr)))

(in the test program)
from infixtoiter import to_postfix
from evalpostfix import eval_postfix

def test(expr):
    print (expr, '=', eval_postfix(to_postfix(expr)) )

So here is the sequence of function calls as they operate on the input:

  1. The process begins with a character string in expr
  2. It is broken into tokens by new_split_iter, which yields an iterator
  3. The Peekable constructor adds peek functionality
  4. The parsing functions in infixtoiter produce a linked list postfix expression
  5. That expression is evaluated by eval_postfix
  6. The original string and computed value are displayed

Basic features

  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support

On-demand options

  • Writer's samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading

Paper format

  • 275 words per page
  • 12pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, CHicago/Turabian, Havard)

Guaranteed originality

We guarantee 0% plagiarism! Our orders are custom made from scratch. Our team is dedicated to providing you academic papers with zero traces of plagiarism.

Affordable prices

We know how hard it is to pay the bills while being in college, which is why our rates are extremely affordable and within your budget. You will not find any other company that provides the same quality of work for such affordable prices.

Best experts

Our writer are the crème de la crème of the essay writing industry. They are highly qualified in their field of expertise and have extensive experience when it comes to research papers, term essays or any other academic assignment that you may be given!

Calculate the price of your order

You will get a personal manager and a discount.
We'll send you the first draft for approval by at
Total price:
$0.00

Expert paper writers are just a few clicks away

Place an order in 3 easy steps. Takes less than 5 mins.