As usual, create two directories for today's class. Create a
directory called lecture22
under activities, and
a directory called lab22
under labs.
Everything in a computer can be represented as a number. We saw this fact in practice using the ord function in the previous lecture, to convert characters to their appropriate integer value. However, this is much more simple than what is actually going on. Today, you will learn how to encode numbers using binary.
Create a function parse_int
that takes as input a string,
and returns the decimal integer representation of the number in the string.
Your function should assume that the input string is completely
numeric (all of the characters are numeric characters).
DO NOT USE THE BUILT IN int
FUNCTION.
Don't forget to define your test cases. Your program should execute your test cases automatically.
In the same file as before, write a function called
parse_binary
, which takes as input a string, and also
returns a base-10 integer representation of that string. However, in
this case your string will only contain the characters 0 and 1.
DO NOT USE THE BUILT IN int
FUNCTION.
Don't forget to define your test cases. Your program should execute your test cases automatically.
The same structure demonstrated in class for binary can be used in general for an arbitrary base. Although we've only discussed decimal (base 10) and binary (base 2), any arbitrary number can be used as a base for a number system. In this assignment, you will write code to convert numbers into an arbitrary base.
Create a file called base_conversion.py
in
your lab22 directory. You are to write a function called
convert_base
that takes two parameters: an integer of the
number to convert, and the base to convert that integer to (some
number in the range [2, 10]). This function should return a string
representation of the input parameter in the specified base.
Make sure you test your program well. How many test cases do you need? Include your test cases in your program file. Comment them appropriately.
When you have finished, create a tar file of your lab22
directory. To create a tar file, execute the following commands:
cd ~/cs120/labs tar czvf lab22.tgz lab22/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 22
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your files.
Do not forget to email your partner today's files!