Use the command line to create a new directory called lab20 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.
We can convert from binary to decimal values, however, there are many instances where we want to go the other direction. Having a binary number can assist in computing permutations of a set, as well as many other practical applications. Today, you will write a function that will perform the computation the opposite direction.
Details
Write a function called convert_base(integer, new_base)
in a file called base_conversion.py. This function takes
two integer parameters: the decimal number to convert and the new
base to convert to (\(1 < new\_base < 10\)). This function should
return a string representation of integer
in the
specified base.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Sample Test Cases
Function Call | Expected Output |
---|---|
convert_base(2, 2) | 10 |
convert_base(3, 2) | 11 |
convert_base(3, 3) | 10 |
Hint
-
You are going to use the accumulator pattern to accumulate a string that represents the input integer converted to the specified base.
-
Inside the loop calculate the quotient and remainder of dividing the input integer by the new base. The remainder is a place in the converted integer and should be accumulated.
-
The quotient of the previous iteration is the input integer of the next iteration. At the bottom of the loop, update the input integer to be the quotient.
Challenge
Another very popular numeric system is known as hexadecimal. hexadecimal is simply a base 16 number system. However, we only have 10 "numeric" digits to work with in our number system. We typically get around this by using letters for the rest of the numbers. So, 'A' is used to represent the digit 10, 'B' for the digit 11, and up to 'F' representing the digit 15.
Alter your convert_base
function to allow for
conversions of bases up to 16. In an ideal world, this change
would actually account for bases up to 36. Recall that you can
use the chr
function to convert a number into an
ASCII character.
One of the new bases that we introduced today in class was Hexadecimal, base-16. Base-16 is interesting because some of the tokens for a base-16 number are not numerals. Some of the digits in a hex number are characters. To demonstrate your knowledge of hexadecimal, write a function that converts hexadecimal numbers to decimal integers.
Details
Write a function called parse_hex(hex_string)
in a file
called parse_hex.py. The function should return an int
value that is equivalent to the hexadecimal integer represented
with hex_string
. The function should assume
that hex_string
consists of the only the characters
'0'-'9' and 'A'-'F'.
Make sure your program handles all necessary cases gracefully. What additional test cases should you check?
Sample Test Cases
Function Call | Expected Output |
---|---|
parse_hex('B') | 11 |
parse_hex("00") | 0 |
Hint
-
You are going to use the accumulator pattern for this activity.
-
Unlike the previous parsing functions you wrote, you cannot simply figure out the decimal by taking the
ord
of the character you are inspecting in the string. You need to figure out if it's alpha or a digit. You can use theisalpha
andisdigit
method for strings to determine this. - Otherwise, the same process from Monday applies. What are you multiplying by in this case?
Challenge
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.