This task is part of project03 which is due at 23:00 EST on 2026-09-29.
This is an individual task that you must do on your own.
You can download the starter code for this task using this link.
See how to submit this task using this link.
Put all of your work for this task into the file
decimalToBinary.py
(which is provided among the starter files)
This task gives you practice with while loops in the context of converting
decimal to binary notation.
In the decimal (base 10) system with which we are so familar, numbers are expressed as a sequence of digits ranging from 0 to 9, where each digit it multiplied by powers of 10 (100 = 1, 101 = 10, 102 = 100, 103 = 1000, etc.) based on their position from right to left. So 174910 (the 10 subscript emphasizes the number is written in base 10) means 1*103 + 7*102 + 4*101 + 9*100.
In the binary (base 2) system, there are only two binary digits (0 and 1) known as "bits" (short for "binary digits") and each bit is multiplied by powers of 2 (20 = 1, 21 = 2, 22 = 4, 23 = 8, etc.) based on their position from right to left. So 11012 (the 2 subscript emphasizes the number is written in binary) means 1*23 + 1*22 + 0*21 + 1*20 = 1310.
This problem involves implementing an algorithm for converting Python's decimal integers
to strings consisting of the characters '0' and
'1'. The essence of this algorithm is based on the following
observations:
0 to the end of a binary numeral multiplies its decimal value by 2,
because it shifts each 1 in the numeral left by one unit,
making it worth twice as much. E.g., 110102 = 2610 = 2*1310
because it's the result of postpending 0 to 11012 = 1310.1 to the end of a binary numeral multiplies its decimal value by 2
and then adds one. E.g., 110112 = 2710.0.1.0
or B1 for some binary numeral B that's
one binary digit shorter, then B denotes a number that's
half (in the "integer division by 2" sense) the value denoted
by B0 or by B1.The above insights lead to an iterative algorithm in which an integer
is successively halved, and the 0 and 1
bits determined by even/odd checks are collected to form
the correct binary number for the intial integer.
This process is illustrated by example in the following iteration
table that shows the conversion of the integer 83
into the binary string '1010011'.
+-----+-----------+---------------------------+
| num | binary | explanation |
+-----+-----------+---------------------------|
| 83 | '' | |
| 41 | '1' | 83 is odd so prepend '1' |
| 20 | '11' | 41 is odd so prepend '1' |
| 10 | '011' | 20 is even so prepend '0' |
| 5 | '0011' | 10 is even so prepend '0' |
| 2 | '10011' | 5 is odd so prepend '1' |
| 1 | '010011' | 2 is even so prepend '0' |
| 0 | '1010011' | 1 is odd so prepend '1' |
+-----+-----------+---------------------------|
Your task is to flesh out the skeleton of the following single function
provided in the file decimalToBinary.py:
intToBinaryString- Takes one argument that can be any Python value.The sample output gives examples of how
- In the expected case where the argument value is a nonnegative integer, it returns a binary string (a string of
0s and1s) that is the binary representation of the integer. It should build this string using awhileloop that uses the iteration strategy described above. For full credit, the body of thiswhileloop should use a conditional expression rather than a conditional statement. (However, it is expected that a chained conditional statement should be used outside the loop.)- In the case where the argument value is a negative integer, the function must return the error string
'integer, but negative'.- In the case where the argument value is not an integer, the function must return the error string
'not an integer'. (Use Python'stypefunction to determine the type of the argument value.)intToBinaryStringshould work.Notes
- You are not allowed to define or use any helper functions.
- You should strive to make your code as understandable as possible, using comments to explain any aspects of your code that are not obvious.
- You can test your current version of
intToBinaryStringby (1) saving the the filedecimalToBinary.pyand (2) running the programtest_decimalToBinary.py. The latter program will test yourintToBinaryStringdefinition on many inputs, reporting any argument values for which it returns the wrong result. It will also perform some tests that verify it satisfies certain structural requirements and prohibitions. It's a good sign if your definition ofintToBinaryStringpasses all tests intest_decimalToBinary.py, but this does not guarantee that your definition is completely correct or that it stylistically elegant.
intToBinaryString Examples
The following are some examples that show how intToBinaryString should work:
In []:Out[]:intToBinaryString(1)In []:'1'Out[]:intToBinaryString(2)In []:'10'Out[]:intToBinaryString(5)In []:'101'Out[]:intToBinaryString(10)In []:'1010'Out[]:intToBinaryString(20)In []:'10100'Out[]:intToBinaryString(41)In []:'101001'Out[]:intToBinaryString(83)In []:'1010011'Out[]:intToBinaryString(0)In []:'0'Out[]:intToBinaryString(-17)In []:'integer, but negative'Out[]:intToBinaryString(3.14)In []:'not an integer'Out[]:intToBinaryString('83')In []:'not an integer'Out[]:intToBinaryString(True)'not an integer'
intToBinaryString function definition should be
correct:
it should return the correct binary string for all nonnegative integers,
and should return the correct error strings for negative integers
and for nonintegers.