Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Bounty Awarded with 300 reputation awarded by xnor
saved 5 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

Python 2, 7368 bytes

Saved 1 byte thanks to @Sisyphus
Saved 5 more bytes thanks to @xnor

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>04-k%-9%2)*f+9%~k%2*f(n-1,k%2*4k%2*2)

Try it online! Try it online!

Below is a summary of the results returned by each expression, split by key groups:

 expression | 1 3 7 9 | 42 68 | 24 86 | 0 | description
--------------+---------+-----+-----+---+---------------------------------------
 2-k%2 | 1 1 1 1 | 2 2 | 2 2 | 2 | weight for the 1st recursive call
 0**k*3+1+k%24-k%-9%2  | 24 24 24 24 | 13 13 | 13 13 | 4 | target key for the 1st recursive call
 k&5>0 9%~k%2 | 1 1 1 1 | 1 1 | 0 0 | 0 | weight for the 2nd recursive call
 k%2*4 k%2*2 | 42 42 42 42 | 0 0 | - - | - | target key for the 2nd recursive call

Python 2, 73 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>0)*f(n-1,k%2*4)

Try it online!

Below is a summary of the results returned by each expression, split by key groups:

 expression | 1 3 7 9 | 4 6 | 2 8 | 0 | description
--------------+---------+-----+-----+---+---------------------------------------
 2-k%2 | 1 1 1 1 | 2 2 | 2 2 | 2 | weight for the 1st recursive call
 0**k*3+1+k%2 | 2 2 2 2 | 1 1 | 1 1 | 4 | target key for the 1st recursive call
 k&5>0  | 1 1 1 1 | 1 1 | 0 0 | 0 | weight for the 2nd recursive call
 k%2*4  | 4 4 4 4 | 0 0 | - - | - | target key for the 2nd recursive call

Python 2, 68 bytes

Saved 1 byte thanks to @Sisyphus
Saved 5 more bytes thanks to @xnor

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,4-k%-9%2)+9%~k%2*f(n-1,k%2*2)

Try it online!

Below is a summary of the results returned by each expression, split by key groups:

 expression | 1 3 7 9 | 2 8 | 4 6 | 0 | description
------------+---------+-----+-----+---+---------------------------------------
 2-k%2 | 1 1 1 1 | 2 2 | 2 2 | 2 | weight for the 1st recursive call
 4-k%-9%2  | 4 4 4 4 | 3 3 | 3 3 | 4 | target key for the 1st recursive call
 9%~k%2 | 1 1 1 1 | 1 1 | 0 0 | 0 | weight for the 2nd recursive call
 k%2*2 | 2 2 2 2 | 0 0 | - - | - | target key for the 2nd recursive call
added a summary of the results for each expression used in the code
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

Python 2, 73 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>0)*f(n-1,k%2*4)

Try it online!

Below is a summary of the results returned by each expression, split by key groups:

 expression | 1 3 7 9 | 4 6 | 2 8 | 0 | description
--------------+---------+-----+-----+---+---------------------------------------
 2-k%2 | 1 1 1 1 | 2 2 | 2 2 | 2 | weight for the 1st recursive call
 0**k*3+1+k%2 | 2 2 2 2 | 1 1 | 1 1 | 4 | target key for the 1st recursive call
 k&5>0 | 1 1 1 1 | 1 1 | 0 0 | 0 | weight for the 2nd recursive call
 k%2*4 | 4 4 4 4 | 0 0 | - - | - | target key for the 2nd recursive call

Python 2, 73 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>0)*f(n-1,k%2*4)

Try it online!

Python 2, 73 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>0)*f(n-1,k%2*4)

Try it online!

Below is a summary of the results returned by each expression, split by key groups:

 expression | 1 3 7 9 | 4 6 | 2 8 | 0 | description
--------------+---------+-----+-----+---+---------------------------------------
 2-k%2 | 1 1 1 1 | 2 2 | 2 2 | 2 | weight for the 1st recursive call
 0**k*3+1+k%2 | 2 2 2 2 | 1 1 | 1 1 | 4 | target key for the 1st recursive call
 k&5>0 | 1 1 1 1 | 1 1 | 0 0 | 0 | weight for the 2nd recursive call
 k%2*4 | 4 4 4 4 | 0 0 | - - | - | target key for the 2nd recursive call
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

Python 2, 7473 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5and fk&5>0)*f(n-1,k%2*4))

Try it online! Try it online!

Python 2, 74 bytes

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5and f(n-1,k%2*4))

Try it online!

Python 2, 73 bytes

Saved 1 byte thanks to @Sisyphus

This is based on the logic used in my 62-byte JS version, with a different implementation to make it easier to golf in Python. I've since ported this back to JS, as it turned out to be shorter as well.

f=lambda n,k:n<1or k-5and(2-k%2)*f(n-1,0**k*3+1+k%2)+(k&5>0)*f(n-1,k%2*4)

Try it online!

saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /