Skip to main content
Update test cases in TIO
Source Link
justhalf
  • 2.3k
  • 1
  • 20
  • 34

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Each is a full program, taking input from stdin, outputting to stdout.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Try it online!

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Try it online!

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Try it online!

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Try it online!

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Each is a full program, taking input from stdin, outputting to stdout.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Try it online!

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Try it online!

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Add link to TIO
Source Link
justhalf
  • 2.3k
  • 1
  • 20
  • 34

Python 3Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Try it online!

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Try it online!

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Try it online!

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Try it online!

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.

Source Link
justhalf
  • 2.3k
  • 1
  • 20
  • 34

Python 3, 117+154 = 271 bytes

Represents a list as space-separated integers.

Encoder, 117 bytes

f=lambda n,s=1:f'{f(len(f"{n:b}"),0)if n>3or s else""}{s}{n:0{2-s}b}' print(''.join(map(f,map(int,input().split())))) 

Prints each encoding backward, from the number itself, then consecutively prints the length of the previous number in binary, separated by the right separator. The parameter s represent the separator to be output. Recurse when length is still more than 3, or it is the original number (s=1).

Decoder, 154 bytes

s=input() r=[] def f(i,l,z): if len(s)==i:return 1/z p=int(s[i]);m=int(s[i+1:i+1+l],2)//(1-z*p);p and r.append(m);f(i+1+l,[m,2][p],p) f(0,2,1) print(*r) 

Interpret the length of the next binary number repetitively, until separator 1 is found, which indicates the final number. Handles invalid encoding by raising ZeroDivisionError (if first bit in a number is not 0, or bitstream ended while original number is not yet found) or ValueError (trying to interpret empty string as number).

The tricky part is handling the invalid encoding.