CPSC120A
Fundamentals of Computer Science I

Lab 19

Binary

Use the command line to create a new directory called lab19 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Parse Integer

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.

Details

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?

Sample Test Cases

Function Call Expected Output
parse_int('123') 123
type(parse_int('123')) <class 'int'>

Hint

  • 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.

Challenge

The function assumes that all input strings represent a positive integer. Modify the function to also convert strings that represent a negative number.
Parse Integer

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.

Details

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?

Sample Test Cases

Function Call Expected Output
parse_int('101') 5
type(parse_int('101')) <class 'int'>

Hint

  • 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.

Challenge

The function assumes that all input strings represent a positive integer. Unlike decimal numbers, binary numbers typically do not use the minus sign, instead they use 0 and 1 and two's compliment. Modify the function to also convert strings that represent a negative number using two's complement.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.