Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
21 votes
Accepted

Fibonacci Encoding

Japt, 13 bytes @!X¤øB}iU ¤ÔÄ Explanation: ...
Etheryte's user avatar
  • 3,878
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 ...
Leo's user avatar
  • 12.9k
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. ...
ovs's user avatar
  • 61.2k
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 ...
Noodle9's user avatar
  • 20.4k
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 ...
Delfad0r's user avatar
  • 6,276
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...) ...
Jonathan Allan's user avatar
6 votes

Gray code on N symbols

Vyxal, 10 bytes ʁÞẊṖ'¯:fTl Try it Online! ...
emanresu A's user avatar
  • 46.2k
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-...
Jonathan Allan's user avatar
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 ...
Arnauld's user avatar
  • 206k
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 (...
alephalpha's user avatar
  • 51.9k
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 ...
Kevin Cruijssen's user avatar
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 ...
rak1507's user avatar
  • 4,690
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. ...
dingledooper's user avatar
  • 23.4k
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 ...
tsh's user avatar
  • 36.2k
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 ...
Jonah's user avatar
  • 34.1k
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 ...
tsh's user avatar
  • 36.2k
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!
Jonathan Allan's user avatar
4 votes

Gray code on N symbols

JavaScript (ES7),  82  78 bytes Expects (N)(k). ...
Arnauld's user avatar
  • 206k
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 ...
Command Master's user avatar
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 ...
Jonathan Allan's user avatar
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 ...
user's user avatar
  • 457
3 votes

Fibonacci Encoding

05AB1E, 14 bytes ∞ʒb11å>}s<èb1ì Try it online! ...
Makonede's user avatar
  • 6,799
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 ...
Razetime's user avatar
  • 27.6k
3 votes

Test if a Gray code is Beckett

Jelly, 10 bytes _ƝµṂƇNṖ⁼ṬƇ Try it online! Semi-translation of Leo's excellent Husk answer. ...
Unrelated String's user avatar
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!!! ...
Noodle9's user avatar
  • 20.4k
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 ...
Bubbler's user avatar
  • 79.3k
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 ...
anatolyg's user avatar
  • 14.1k
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: ...
peak's user avatar
  • 145
3 votes

Fibonacci Encoding

Japt, 12 bytes Based off Etheryte's solution, posted with permission. _øB ªU´}f¤ÔÄ Try it
Shaggy's user avatar
  • 45k
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=[]) ...
user81655's user avatar
  • 11.3k

Only top scored, non community-wiki answers of a minimum length are eligible