21
votes
Accepted
13
votes
Test if a Gray code is Beckett
Husk, 9 bytes
Λ≡½hÖ▼Ẋz-
Try it online! (Version with formatting header to allow pasting testcases: Try it online!)
Explanation
If we compute differences from each ...
10
votes
Gray code on N symbols
BQN, 12 bytesSBCS
Based on an algorithm given on Wikipedia. Takes k as left and N as right argument.
...
8
votes
Fibonacci Encoding
C (gcc), 51 50 bytes
k;f(n){for(k=0;n-=!(++k&k/2););n=log2(k);k+=2<<n;}
Try it online!
Uses the formula from Etheryte's Japt answer.
Returns the ...
6
votes
Fibonacci Encoding
Haskell, 76 bytes
1#2
(u#v)x|x<v=["11"|x==u]|z<-u+v=map('0':)(v#z$x)++map("10"++)(z#(z+v)$x-u)
Try it online!
Outputs a string of ...
6
votes
Fibonacci Encoding
Jelly, 21 12 bytes
Using Etheryte's observation from their Japt answer is much terser and more efficient to boot (it may be possible in even less, let's see...)
...
6
votes
6
votes
Gray code on N symbols
Jelly, 6 bytes
ṗ@Ż€I%
A dyadic Link that accepts the length, \$k\$, on the left and the number of symbols, \$N\$ on the right and yields a list of lists of non-...
5
votes
Fibonacci Encoding
JavaScript (ES6), 46 44 bytes
Based on @Etheryte's insight.
Returns a string of bits in reverse order.
f=(n,k)=>k&k/2||n--?f(n,-~k):1+k.toString(2)
Try ...
5
votes
Gray code on N symbols
Wolfram Language (Mathematica), 45 bytes
Mod[#-{0,##&@@Most@#}&/@Range@#~Tuples~#2,#]&
Try it online!
A port of @ovs's BQN answer.
Wolfram Language (...
5
votes
Gray code on N symbols
05AB1E, 11 10 9 bytes
LIãε0š¥¹%
-1 byte porting @ovs' BQN answer, using the approach described on Wikipedia.
Inputs in the order \$N,k\$. Outputs a single result ...
4
votes
Test if a Gray code is Beckett
APL (Dyalog Classic), 35 bytes
{(∊⍸¨↓b)≡(∨/b←2>⌿⍵)/¯1↓⊣/⍒⍤1⊥⍨¨,⍀⍵}
-27 from Bubbler, -13 for pointing out the input is a Gray code, -3 for removing an excess ...
4
votes
Test if a Gray code is Beckett
Python 2, 62 bytes
Output is via exit code: 0 if it is a Beckett-Gray Codex and 1 otherwise.
...
4
votes
Test if a Gray code is Beckett
JavaScript (Node.js), 55 bytes
a=>a.every(n=>(n-=a[++i]|0)>0?m<(m=a[-n]):a[n]=i,i=m=0)
Try it online!
Input an array of equivalent integers, output ...
4
votes
Test if a Gray code is Beckett
J, 32 25 bytes
[:-:/[:(|/.~/:~"1)2-/\,&0
Try it online!
Based on Leo's excellent idea
2-/\,&0 Adjacent ...
4
votes
Fibonacci Encoding
JavaScript (Node.js), 43 bytes
n=>(g=p=>n<p||2*g(p+q,q=p)+(n>(n%=p)))(q=1)
Try it online!
It returns an integer whose binary form is reversed ...
4
votes
Fibonacci Encoding
Python 3.8 (pre-release), 51 bytes
Another based on Etheryte's Japt answer
f=lambda n,k=0:-n*f'1{k-1:b}'or f(n-(k&k//2<1),k+1)
Try it online!
4
votes
4
votes
Prefix code generator
05AB1E, 21 bytes
āø{ø`soDzηO>*<b€¦sākè
Try it online!
Uses the same idea as arithmetic coding. Fails for lengths greater than 53 due to floating-point ...
4
votes
Prefix code generator
Jelly, 18 bytes
eƤ€`Ḅ=QƑẠ
Ø.ṗŒpÇƇṪ
A monadic Link that accepts the lengths and yields a list of prefix-codes in the same order.
Try it online!
Save a byte by ...
3
votes
Fibonacci Encoding
Scala, 69 bytes
Based on Etheryte's amazing answer
"1"+Stream.from(0).map(_.toBinaryString).filter(!_.contains("11"))(_)
Try it online!
Outputs ...
3
votes
3
votes
Fibonacci Encoding
Stax, 16 bytes
âî}g♥▼«zΩ╥;QbB]╔
Run and debug it
Based on Etheryte's observation, which shaved a off a ton of bytes. Check out their answer!
Explanation
...
3
votes
Test if a Gray code is Beckett
Jelly, 10 bytes
_ƝµṂƇNṖ⁼ṬƇ
Try it online!
Semi-translation of Leo's excellent Husk answer.
...
3
votes
Test if a Gray code is Beckett
Python 3, 138 \$\cdots\$ 102 95 bytes
Saved 13 bytes thanks to rak1507!!!
Saved 7 bytes thanks to dingledooper!!!
...
3
votes
Correct errors using Hamming(7,4)
APL (Dyalog Extended), 26 25 bytes
(⊤⍳7)(3 5 6 7⊇⊢≠≠.∧∧.=⊣)⊢
Try it online!
-1 byte thanks to @Adám.
A bit shortened further using monadic ...
3
votes
Correct errors using Hamming(7,4)
IA-32 machine code, 36 bytes
Hexdump:
33 c0 40 91 a8 55 7a 02 d0 e1 a8 66 7a 03 c0 e1
02 a8 78 7a 03 c1 e1 04 d0 e9 32 c1 24 74 04 04
c0 e8 03 c3
Equivalent C ...
3
votes
Fibonacci Encoding
jq (74 bytes)
Since shortness is the goal ...
def f:def i:.,(i|[0]+.,[1]+.);nth(.-1;[1]|i|select(index([1,1])|not))+[1];
Example:
...
3
votes
Fibonacci Encoding
Japt, 12 bytes
Based off Etheryte's solution, posted with permission.
_øB ªU´}f¤ÔÄ
Try it
2
votes
Test if a Gray code is Beckett
JavaScript (V8), 106 bytes
a=>!a.some((c,i)=>c.map((x,j)=>n+=x<(y=(a[i+1]||a[0])[j])?!!o.push(j):x>y?1<<o.shift()-j:0,n=0)&&n-1,o=[])
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
coding-theory × 11code-golf × 10
binary × 5
math × 2
sequence × 2
error-correction × 2
string × 1
code-challenge × 1
decision-problem × 1
number-theory × 1
integer × 1
optimization × 1
fibonacci × 1
open-ended-function × 1
conversion × 1
encoding × 1