CIT 591 Assignment 2: Balanced Ternary
Fall 2010, David Matuszek
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.
The user will use your program by typing in expressions such as N0 + 1NN and will get a balanced ternary number such as 1N as a result. Details of what the user may type are given below.
We will do some automated testing of your assignment. That means our program will call functions in your program, and if those functions are missing, or have a different name, or different parameters, or return the wrong kind of values, then they will fail our tests. So be sure to write functions exactly as specified below.
Example (I am using "Compute:" as a prompt):
Compute: N0000 + 1N0
N01N0
Compute: dec mem
-75
Compute: bt -75
N01N0
Compute: mem * N
10N10
Compute: hello
Error: I don't understand: hello
Compute: quit
Done.
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')
The user may type in any of the following expressions:
BT + BTAdds the two numbers BT - BTSubtracts the second number from the first BT * BTMultiplies the two numbers BT / BTDivides the first number by the second BT % BTGives the remainder when dividing the first number by the second (see note) -BTComputes the negative of the given number dec BTGives the decimal value of the BT number bt decimalGives the BT value of the decimal number quitQuits the program
Your "calculator" will remember the last number computed (initially zero, when you start the program). In each of the above expressions,
BT may be a balanced ternary number, written without quotes, or the word mem. If it is the word mem, then the calculator uses the number stored in its "memory" as that operand. '00N1'); however, your calculator should not give results with leading zeros.Save your program on a file named bt.py.
Here are the functions you should have.
Except as noted,
add(bt1, bt2)subtract(bt1, bt2)bt1 - bt2. Also saves the result in memory. multiply(bt1, bt2)divide(bt1, bt2)bt1 / bt2. Does not check to make sure bt2 != 0. Also saves the result in memory. remainder(bt1, bt2)bt1 % bt2. Does not check to make sure bt2 != 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). Also saves the result in memory. negate(bt)bt, would give zero. Also saves the result in memory. bt_to_int(bt)bt. Also saves the given bt (not the decimal result!) in memory .int_to_bt(int)int. Also saves the result in memory.store(bt)fetch() '0', if store has never been called). This should be the only function that refers to this global variable. evaluate(string)mem). mem), and return the result as a string. I want to make this as clear as I can: For every legal operation that the user types in, except quit, the balanced ternary version of the result is saved in memory. This includes the operations bt and dec. (Illegal operations should not change memory.) To evaluate bt and dec, you will probably call functions int_to_bt and bt_to_int, but the functions int_to_bt and bt_to_int should not themselves change memory, or it will be very difficult to make your program do the right thing.
To evaluate dec, call bt_to_int to get an answer, then save the result; and similarly for the bt operation.
Catch as many errors in the user input as you can. You probably won't be able to catch them all, unless you use features of Python that we haven't talked about yet. We will be testing that your methods work when given correct input.
REPL()quit. evaluate method, REPL. bt_to_int(bt) and int_to_bt(int) are fundamental, so do these first. Both are fairly difficult; bt_to_int(bt) is somewhat easier than int_to_bt(int).
n % 10. To remove the last digit of a decimal number n, use n / 10. n % 3, where n is an ordinary integer, to find what the rightmost ternary digit should be. That might give you 2, with is not a legal balanced ternary digit, but you should be able to figure out what to do with it. REPL function is very short (7 lines), because most of the work is done in evaluate(string).
evaluate(string)does a lot of work--too much for one function. Use additional "helper" functions. For example, one of my helper functions splits the string into three parts.
a, b, c = 1, 2, 3 ? You can do the same thing with functions: def powers(n):
return n * n, n * n * n
square, cube = powers(5)len(s) -- returns the length of the strings.upper(), s.lower() -- returns the uppercase/lowercase version of the string s.startswith(t) -- returns True if string s starts with string t s.replace(t, u) -- returns a string like s, but with all occurrences of string t replaced by string u. u may be the empty string, '', in which case all occurrences of string t are deleted. s in t -- returns True if string s is a part of string ts[n:m],
s[n:],
and s[:m]. .py file to Blackboard.