Ask Python Expert

Python Assignment

Problem Description

The project so far supports calculations on numeric values, but most computational statements (and even most calculators) are not limited to numeric literal values. They usually also include variables.

So this assignment will add the ability to assign to variables, and then to later use their values in further computations. This will require a data structure that will associate variables with values, and to do that efficiently, will use a binary search tree.

Parsing and Interpretation

There will be one additional operator for this homework -- the sasignment operator ('='). We will support the C++ meaning of this operator, that not only assigns to a variable, but 'computes' the assigned value. The assignment operator has the lowest precedence (be aware of what that means for the order of calling functions when parsing.

Here are a few examples:

x = 1 + (y = 4) # assign 4 to y and 5 to x
x = y = z = 4 # assign 4 to z, then y, then x
x = y = z + 4 # add 4 to z, then assign sum to y and x
x = 1 + y = 4 # invalid input: cannot assign 4 to (1+y)

Special Note:

It may be tempting to make the function for assignment operator to be just like the ones for sum and product. That would not be correct, because it would do the operations from left to right. HOWEVER: it can be fixed just by changing the name of one function call within the new while loop. Furthermore, if you do so correctly, the while loop would only repeat 0 or 1 times, so the word 'while' could optionally be replaced with a different word.

The postfix forms for the legal expression would be similar to the other operators:

x 1 y 4 = + = # assign 4 to y, add 1 to it, then assign 5 to x
x y z 4 = = = # assign 4 to z, then y, then x
x y z 4 + = = # add 4 to z, then assign sum to y and x

A binary search tree will be used when evaluating the postfix expression. If a variable is the left operand of an assignment, the variable will receive the value. If a variable appears at any other time, its value is retrieved for a computation.

That means that the postfix evaluation function's stack of 'values' will now have a mixture of values and variable names, and will need to be able to know the difference and use the information appropriately.

HINT: The instructor discovered that if value = '0.3', value.isdigit() is False. It is safer to check value[0].isdigit(). That will let you more reliably identify whether the stack element is a variable or a number.

Defining a Binary Tree

There is no need for any self-balancing features in this assignment -- just the a simple binary tree supporting the ability to store new variables, to associate values with those variables, and to retrieve the value of a variable.

For consistency, call your file vartree.py

Required Features in Implementation

Nested Node class definition

for binary tree node

  with __slots__

to save list memory

  and __init__

a constructor

__init__()

a constructor for an empty tree

_search(here, var)

recursively search tree rooted 'here' for variable 'var' 
returning correct Node, or None if not found

_insert(here, var, value)

return a search tree with variable inserted or updated 
this should be a recursive implementation for immutable behavior

assign(var, value)

public method to assign to a variable

lookup(var)

public method retrieve value of variableif the variable does not yet exist, assign it zero

Good functions for Completeness / Practice

is_empty()

Identify whether tree is empty

__len__()

returns number of elements in list

__iter__()

inorder iterator generator(with yield statements)

__str__()

represents entire tree as a string 
For full credit, this must be no worse than log-linear time and not quadratic

Assignment Specifications

One new file and three modified files are to appear in the solution to this assignment:

vartree.py

Implements the binary tree above as a class

newsplit.py

divides into tokens, generating an iterator, with added support for variable names

infixtoiter.py

given an iterator to an infix expression, produces an iterator generator for a postfix expression, with added support for variables and =

evalpostfix.py

evaluates a postfix expression, given an iterator, with added support for variables

That module will have its own variable to hold all of those variables

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

Testing and Evaluation

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

(in the test program)

from infixtoiter import to_postfix
from evalpostfix import eval_postfix

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

test("b=6") # b=6 : 6
test("b * 9") # b * 9 : 54

You may assume that all expressions are legal C expressions with binary operators, but you may NOT assume that:

• All numbers are single digits -- this assumption was not permitted before either
• All variables are single letters -- they may include multiple letters (and maye digits)
• All variables are assigned before use -- if not yet assigned, assign zero.

Python, Programming

  • Category:- Python
  • Reference No.:- M92013145

Have any Question?


Related Questions in Python

Part i the assignment filesone of the most important

Part I: The Assignment Files One of the most important outcomes of this assignment is that you understand the importance of testing. This assignment will follow an iterative development cycle. That means you will write a ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Tasksdemonstrate data scraping of a social network of

Tasks Demonstrate data scraping of a social network of choice. Develop technical documentation, including the development of the code & detailing the results. Provide a report on the findings, that includes research into ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

Python programming assignment -you first need an abstract

Python Programming Assignment - You first need an abstract base class, called, Account which has the following attributes and methods: accountID: This attribute holds the ID assigned the account , if not provided set to ...

Learning outcomes lo3 - research develop and document a

Learning Outcomes LO3 - Research, develop, and document a basic security policy, and analyse, record, and resolve all security incidents LO4 - Identify and assess the threats to, and vulnerabilities of networks Assessmen ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Below zero - ice cream storethe local ice-cream store needs

Below Zero - ice cream store The local ice-cream store needs a new ordering system to improve customer service by streamlining the ordering process. The manager of the store has found that many orders are incorrect and s ...

The second task in this assignment is to create a python

The second task in this assignment is to create a Python program called pancakes.py that will determine the final order of a stack of pancakes after a series of flips.(PYTHON 3) Problem Task In this problem, your input w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As