# Author: warner22p (Paul Warner) # Convert a number to its binary representation # With at least a length of minLen def toBin(n: int, minLen:int=0) -> str: # Initialize the binary string bin_str = "" # Use divmod to calculate the digit and the next number while n > 0: # Find the next value and the current binary digit n, digit = divmod(n, 2) # Append the digit to bin_str bin_str = str(digit) + bin_str # Insert "0" while not minLen while (len(bin_str) < minLen): bin_str = "0" + bin_str # Return the value return bin_str if __name__ == "__main__": for i in range(16): print(f"{i:02}: {toBin(i, 4)}")