As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab23 $ cd lab23
Decimal integers can be represented by a computer as either a string or as a number. Both representations are useful. For example, the string representation can be mixed with text for user input and output, but only the numeric representation can be used with the numeric operators, addition, subtraction, etc.
Write a function called parse_integer(decimal_string)
in a file called parse_int.py. The function should return
an int value that is equivalent to the decimal integer represented
with decimal_string
. The function should assume
that decimal_string
consists of only numeric
characters, the characters '0' - '9'. The function should
not use the built-in function int
.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Function Call | Expected Output |
---|---|
parse_int('123') | 123 |
type(parse_int('123')) | <class 'int'> |
To convert the string to an int, the function will need to
convert and accumulate each digit in the input string. The
function will therefore follow the accumulator pattern that
we have seen multiple times. However, the code is slightly
easier to write if the function iterates over the input
string's digits in reverse order. The least significant
digits of the input string are the characters with the
highest indices. So by iterating in reverse order the input
string's digits can be converted in order of ascending
significance. To create a loop that iterates over the
characters in reverse order, use the for i in
range
version of the for loop. Print the characters
at each iteration of the loop to verify that the loop is
written correctly.
Inside the loop, convert the current digit character to an
int using the ord
function. Note, simply using
the ord
function is not sufficient. The
ordinal value of the character '0' is not 0. The ordinal
values of the characters '0' through '9' are, however, in
sequential order. So converting them to the range 0 - 9 can
be done by subtracting the ordinal value of '0' from the
ordinal value of the current digit character..
Converting each digit character to an int, is not sufficient. For example, in the number 842 the digit 4 has the value 40 and the digit 8 has the value 800. So, the first digit must be multiplied by 1, the second by 10, the third by 100, and so on. To do this, create an integer before the loop that is initially 1 and represents the digit's place. In side the loop the integer should increase by a multiple of 10 each iteration. Print the variable to make sure that it is increasing as desired.
Finally, add an accumulator variable that is initially 0. Each iteration of the loop increase the accumulator by the current digit multiplied by the place integer. Don't forget to return the accumulated value.
All computer memory is binary, so whether an integer is represented as a string or as a number, it is still represented as a number in binary.
Write a function called parse_binary(binary_string)
in
a file called parse_bin.py. The function should return an
int value that is equivalent to the binary integer represented
with binary_string
. The function should assume
that binary_string
consists of only the characters '0'
and '1'. The function should not use the built-in
function int
.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Function Call | Expected Output |
---|---|
parse_int('101') | 5 |
type(parse_int('101')) | <class 'int'> |
This function will look very similar to
the parse_integer
function. It will need a for
loop, an accumulator, and a place integer.
The major difference is that the place integer should not increase by multiples of 10, it should increase by multiples of 2.