All problems
Zigzag Encoding
Problem
Map a signed integer to an unsigned integer so small magnitudes of either sign get small codes: non-negative values become twice themselves and negative values become minus twice themselves minus one.
For example 0, -1, 1, -2, 2 map to 0, 1, 2, 3, 4.
Hint
Negative values are mapped to odd codes and non-negative values to even codes.
Test cases
case 1
in: [0]
expected: 0
case 2
in: [-1]
expected: 1
case 3
in: [1]
expected: 2
case 4
in: [-2]
expected: 3
case 5
in: [2]
expected: 4
case 6
in: [100]
expected: 200
Starter codePython
def zigzag_encode(n):
# Your code here
pass6 test cases · run your code against them in the editor