Instructions for decimalToBinary

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:

  • Adding a 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.
  • Adding a 1 to the end of a binary numeral multiplies its decimal value by 2 and then adds one. E.g., 110112 = 2710.
  • The above two facts imply that:
    • The binary numeral for a even number must end in 0.
    • The binary numeral for an odd number must end in 1.
    • If a binary numeral has the form B0 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.

  • In the expected case where the argument value is a nonnegative integer, it returns a binary string (a string of 0s and 1s) that is the binary representation of the integer. It should build this string using a while loop that uses the iteration strategy described above. For full credit, the body of this while loop 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's type function to determine the type of the argument value.)
The sample output gives examples of how intToBinaryString should 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 intToBinaryString by (1) saving the the file decimalToBinary.py and (2) running the program test_decimalToBinary.py. The latter program will test your intToBinaryString definition 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 of intToBinaryString passes all tests in test_decimalToBinary.py, but this does not guarantee that your definition is completely correct or that it stylistically elegant.

Examples

intToBinaryString Examples

The following are some examples that show how intToBinaryString should work:

In []:
intToBinaryString(1)
Out[]:
'1'
In []:
intToBinaryString(2)
Out[]:
'10'
In []:
intToBinaryString(5)
Out[]:
'101'
In []:
intToBinaryString(10)
Out[]:
'1010'
In []:
intToBinaryString(20)
Out[]:
'10100'
In []:
intToBinaryString(41)
Out[]:
'101001'
In []:
intToBinaryString(83)
Out[]:
'1010011'
In []:
intToBinaryString(0)
Out[]:
'0'
In []:
intToBinaryString(-17)
Out[]:
'integer, but negative'
In []:
intToBinaryString(3.14)
Out[]:
'not an integer'
In []:
intToBinaryString('83')
Out[]:
'not an integer'
In []:
intToBinaryString(True)
Out[]:
'not an integer'

Rubric

This task does not have an explicit list of rubric items. But here are some things to focus on:
  • Your 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.
  • Your definition should be easy to understand. Use comments and parameters/variables with >well-chosen, meaningful names to make it clear why the function calculates the correct result.
  • Your definition should follow the do not repeat yourself (DRY) principle to avoid unnecessary repetition of code patterns.
  • Your definition should satisfy any implementation constraints mentioned in the function specification.