|
| 1 | +def baseneg2(n: int) -> str: |
| 2 | + """ |
| 3 | + Converts an integer 'n' to its negative base (-2) representation |
| 4 | + and returns it as a string. |
| 5 | +
|
| 6 | + The negative base (-2) representation |
| 7 | + is a positional numeral system in which: |
| 8 | + - The base is -2. |
| 9 | + - The only digits allowed are '0' and '1'. |
| 10 | + - The value of the number is calculated as follows: |
| 11 | + - Let's say the number is 'n'. |
| 12 | + - Starting from the rightmost digit, the value at each position |
| 13 | + is determined by (-2)^position * digit. |
| 14 | + - The sum of these values at all positions gives the decimal |
| 15 | + value of the number 'n'. |
| 16 | +
|
| 17 | + Parameters: |
| 18 | + - n (int): The integer to be converted to its negative base (-2) representation. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + - str: The negative base (-2) representation of the input integer 'n' as a string. |
| 22 | +
|
| 23 | + Examples: |
| 24 | + >>> baseneg2(0) |
| 25 | + '0' |
| 26 | + |
| 27 | + >>> baseneg2(1) |
| 28 | + '1' |
| 29 | +
|
| 30 | + >>> baseneg2(2) |
| 31 | + '110' |
| 32 | +
|
| 33 | + >>> baseneg2(3) |
| 34 | + '111' |
| 35 | +
|
| 36 | + >>> baseneg2(-3) |
| 37 | + '1101' |
| 38 | +
|
| 39 | + >>> baseneg2(10) |
| 40 | + '11110' |
| 41 | +
|
| 42 | + >>> baseneg2(-10) |
| 43 | + '1010' |
| 44 | +
|
| 45 | + Edge Cases: |
| 46 | + - If the input 'n' is 0, the function returns "0" since the negative base (-2) |
| 47 | + representation of 0 is "0". |
| 48 | +
|
| 49 | + Errors for Incorrect Input: |
| 50 | + 1. If the input 'n' is not an integer, a TypeError will be raised, |
| 51 | + as the function expects 'n' to be an integer. |
| 52 | + >>> baseneg2("abc") |
| 53 | + Traceback (most recent call last): |
| 54 | + ... |
| 55 | + TypeError: Input 'n' must be an integer |
| 56 | +
|
| 57 | + 2. If 'n' is not in the expected range for the negative base (-2) representation, |
| 58 | + the result may not be valid, but no specific error will be raised. |
| 59 | + >>> baseneg2(0.5) |
| 60 | + Traceback (most recent call last): |
| 61 | + ... |
| 62 | + TypeError: Input 'n' must be an integer |
| 63 | + |
| 64 | + >>> baseneg2(-0.5) |
| 65 | + Traceback (most recent call last): |
| 66 | + ... |
| 67 | + TypeError: Input 'n' must be an integer |
| 68 | + |
| 69 | + >>> baseneg2(255) |
| 70 | + '100000011' |
| 71 | + """ |
| 72 | + |
| 73 | + # Check if the input 'n' is an integer |
| 74 | + if not isinstance(n, int): |
| 75 | + raise TypeError("Input 'n' must be an integer") |
| 76 | + |
| 77 | + # Handle the case when 'n' is 0 |
| 78 | + if n == 0: |
| 79 | + return "0" |
| 80 | + |
| 81 | + result = [] |
| 82 | + while n != 0: |
| 83 | + # Calculate the remainder when dividing by -2 |
| 84 | + remainder = n % (-2) |
| 85 | + # Perform integer division by -2 |
| 86 | + n //= (-2) |
| 87 | + |
| 88 | + # If the remainder is negative, adjust it and increment 'n' accordingly |
| 89 | + if remainder < 0: |
| 90 | + remainder += 2 |
| 91 | + n += 1 |
| 92 | + |
| 93 | + # Append the remainder (0 or 1) as a string to the result |
| 94 | + result.append(str(remainder)) |
| 95 | + |
| 96 | + # Reverse the result list and join the elements to form the final negative base |
| 97 | + return ''.join(result[::-1]) |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + import doctest |
| 101 | + |
| 102 | + doctest.testmod() |
0 commit comments