Skip to main content
add explanation, fix bug
Source Link
NinjaBearMonkey
  • 10.3k
  • 3
  • 36
  • 66

CJam, 2021 bytes

q1q{i2b7Te[}%e__(;.^)\;

Try it online! Probably can squeeze outTry it online!

Explanation

Showing the stack with a few more bytessample input of 5:

1 q      e# Push 1 and then the whole input: 1 "5"
{
  i      e# Convert to its char code: 1 [53]
  2 b    e# Convert to binary: 1 [[1 1 0 1 0 1]]
  7 T e[ e# Left-pad with 0 to length 7: 1 [[0 1 1 0 1 0 1]]
} %      e# Map this block over every character in the string
e_       e# Flatten array: 1 [0 1 1 0 1 0 1]
_ ( ;    e# Duplicate array and remove its first element: 1 [0 1 1 0 1 0 1] [1 1 0 1 0 1]
. ^      e# Element-wise xor: 1 [1 0 1 1 1 1 1]
) ;      e# Remove and pop the last element of the array: 1 [1 0 1 1 1 1]
         e# Stack implicitly printed: 1101111

To see if a bit is different from the previous bit, we do a vector (element-wise) xor between the bit array and explanation coming soonthe bit array without the first element. We also remove the last bit of the result, because it is always the last bit of the longer array unchanged.

CJam, 20 bytes

q{i2b7Te[}%e__(;.^)\

Try it online! Probably can squeeze out a few more bytes, and explanation coming soon.

CJam, 21 bytes

1q{i2b7Te[}%e__(;.^);

Try it online!

Explanation

Showing the stack with a sample input of 5:

1 q      e# Push 1 and then the whole input: 1 "5"
{
  i      e# Convert to its char code: 1 [53]
  2 b    e# Convert to binary: 1 [[1 1 0 1 0 1]]
  7 T e[ e# Left-pad with 0 to length 7: 1 [[0 1 1 0 1 0 1]]
} %      e# Map this block over every character in the string
e_       e# Flatten array: 1 [0 1 1 0 1 0 1]
_ ( ;    e# Duplicate array and remove its first element: 1 [0 1 1 0 1 0 1] [1 1 0 1 0 1]
. ^      e# Element-wise xor: 1 [1 0 1 1 1 1 1]
) ;      e# Remove and pop the last element of the array: 1 [1 0 1 1 1 1]
         e# Stack implicitly printed: 1101111

To see if a bit is different from the previous bit, we do a vector (element-wise) xor between the bit array and the bit array without the first element. We also remove the last bit of the result, because it is always the last bit of the longer array unchanged.

Source Link
NinjaBearMonkey
  • 10.3k
  • 3
  • 36
  • 66

CJam, 20 bytes

q{i2b7Te[}%e__(;.^)\

Try it online! Probably can squeeze out a few more bytes, and explanation coming soon.