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 Revisions

7 of 7
Invariant is sort, not sum...
Ven
  • 3.6k
  • 1
  • 20
  • 25

Pyth, 18 bytes

L?SIb0hys%L2>Bb1
y

Try it online!

-2 thanks to @Erik the Outgolfer.

The script has two line: the first one defines a function y, the second line calls y with the implicit Q (evaluated stdin) argument.

L?SIb0hys%L2>Bb1
L function y(b)
 ? if...
 SIb the Invariant b == sort(b) holds
 0 return 0
 h otherwise increment...
 y ...the return of a recursive call with:
 B the current argument "bifurcated", an array of:
 b - the original argument
 > 1 - same with the head popped off
 L map...
 % 2 ...take only every 2nd value in each array
 s and concat them back together

1

Ven
  • 3.6k
  • 1
  • 20
  • 25

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