CIT 590 Assignment 4: Balanced Ternary
Fall 2015, David Matuszek

Purposes of this assignment

General idea of the assignment

Your assignment is to write a "calculator" for balanced ternary integers. It will be able to do addition, subtraction, multiplication, and integer division, as well as negation and modulus (remainder). It will also have a one-number "memory,", and it will be able to convert between decimal and balanced ternary.

Details

In the descriptions below, the word integer will refer to an ordinary integer, such as you have already been using in Python. The term balanced ternary number (BT for short) will mean a string composed of one or more of the characters '1', '0', and 'N' (but not 'n')

You will have a single global variable, memory, which will hold a single number; it is your choice whether to keep this number as an integer or as a BT string. Most of the operations specified below will change this number.

The user may type in any of the following expressions, where BT stands for a balanced ternary number. These are the only kinds of expressions that your calculator needs to handle. There may or may not be a space between the operator and the memory. The user may also type in more than one expression on a line, with optional spaces between expressions, and the computer should do the computations left to right and display only the final answer.

Expression What it means
+ BT Adds the number to memory
- BT Subtracts the number from memory
* BT Multiplies memory by the number
/ BT Divides memory by the number
% BT Sets memory to memory mod the number
= BT Just puts the number into memory

The user is allowed to enter leading zeros (such as '00N1'); however, your calculator should not give results with leading zeros.

Save your program on a file named bt.py and your unit tests on a file named bt_test.py.

Here are the functions you should have.

Functions that do computation

Except as noted,

add(bt)
Adds bt to memory.
subtract(bt)
Subtracts bt from memory.
multiply(bt)
Multiplies memory by bt.
divide(bt)
Divides memory by bt. Raises an Exception if bt == 0.
remainder(bt)
Sets memory to memory % bt. Raises an Exception if bt == 0. If negative numbers are involved, the results are the same as they would be for integers in Python (in other words, just let Python do whatever it does in this case).
negate()
Multiplies memory by the decimal number -1 (or the balanced ternary number N).
store(bt)
Sets memory to bt, replacing whatever value had been there previously.
bt_to_int(bt)
Returns the integer value represented by the string parameter bt. Does not use the global variable memory.
int_to_bt(n)
Returns a string representing the BT value of the integer parameter n. Does not use the global variable memory.
memory_as_int()
Returns the integer value of the number stored in memory.
memory_as_bt()
Returns a string representing the balanced ternary number stored in memory.
evaluate(string)
Takes a string that may have been entered by the user (or may have come from our test methods), and
  1. Finds the various parts of the string. The input string should consist of one or more expressions optionally separated by whitespace, for example '=1NN1 + N01*1N'. If an error is found, return an error message.
  2. Evaluate the expressions, left to right. If successful, return the contents of memory as a balanced ternary string.
  3. If an exception occurs during expression evaluation, catch the exception, and return the error message as a string. The value of memory is undefined (but probably what it was just before the exception happened).

You may find it useful to write "helper functions" for some of these steps, otherwise you may end up with a very long function.

The only function that interacts with the user

REPL()
This function implements a basic Read-Evaluate-Print-Loop. It should:
This should be the only function that does any input/output. It's also the only function you don't need to unit test.

Programming hints

It's a good idea to end your program with

    if __name__ == '__main__':
        REPL()
(In this program, the "main" method is named REPL, not main. )

Due date:

Before 6am Wednesday September 23. Zip your two files and submit them to Canvas. No other form of submission will be accepted.