Skip to content

Commit cb39701

Browse files
My commit for TheAlgorithms#9588
1 parent e60779c commit cb39701

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

Diff for: bit_manipulation/convert_to_base_-2.py

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

0 commit comments

Comments
 (0)