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

added 692 characters in body
Source Link
Sara J
  • 3.9k
  • 19
  • 38

jq, 7150 bytes

map(add)|sort|.[0],range(length)as$i|.[$i+1]-.[$i]

Try it online!

Uses the Arnauld/xnor approach of sum then sort then pairwise differences. Raises an error, but only after the entire output has been streamed.

General solution, 71 bytes

sort_by(length)|while(any;.[0][0]as$i|map(del(.[index($i)]))[1:])[0][0]

Try it online!

WeWorks on lists with arbitrary content - we take the shortest prefix (which has a length of 1), remove it and remove its first (only) value from all remaining prefixes. This gives us a prefix list for the list's tail. We then repeat the process for that prefix list. This eventually gives us a prefix list for the target list and all its suffixes. We then take the first (only) value from the first (shortest) prefix in each of those lists, to get the elements of the target list in order.

jq, 71 bytes

sort_by(length)|while(any;.[0][0]as$i|map(del(.[index($i)]))[1:])[0][0]

Try it online!

We take the shortest prefix (which has a length of 1), remove it and remove its first (only) value from all remaining prefixes. This gives us a prefix list for the list's tail. We then repeat the process for that prefix list. This eventually gives us a prefix list for the target list and all its suffixes. We then take the first (only) value from the first (shortest) prefix in each of those lists, to get the elements of the target list in order.

jq, 50 bytes

map(add)|sort|.[0],range(length)as$i|.[$i+1]-.[$i]

Try it online!

Uses the Arnauld/xnor approach of sum then sort then pairwise differences. Raises an error, but only after the entire output has been streamed.

General solution, 71 bytes

sort_by(length)|while(any;.[0][0]as$i|map(del(.[index($i)]))[1:])[0][0]

Try it online!

Works on lists with arbitrary content - we take the shortest prefix (which has a length of 1), remove it and remove its first (only) value from all remaining prefixes. This gives us a prefix list for the list's tail. We then repeat the process for that prefix list. This eventually gives us a prefix list for the target list and all its suffixes. We then take the first (only) value from the first (shortest) prefix in each of those lists, to get the elements of the target list in order.

Source Link
Sara J
  • 3.9k
  • 19
  • 38

jq, 71 bytes

sort_by(length)|while(any;.[0][0]as$i|map(del(.[index($i)]))[1:])[0][0]

Try it online!

We take the shortest prefix (which has a length of 1), remove it and remove its first (only) value from all remaining prefixes. This gives us a prefix list for the list's tail. We then repeat the process for that prefix list. This eventually gives us a prefix list for the target list and all its suffixes. We then take the first (only) value from the first (shortest) prefix in each of those lists, to get the elements of the target list in order.

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