Posting golf kode pertama saya, permintaan maaf atas kesalahan ...
Konteks
Dalam panjat tebing ( khusus batu ), nilai pendakian V / Vermin (AS) mulai dari 'VB' (tingkat termudah), dan kemudian pergi 'V0', 'V0 +', 'V1', 'V2', 'V2', 'V3' , 'V4', 'V5' dll. Hingga 'V17' (nilai tersulit).
Tugas
Anda akan mengambil sebagai input daftar / larik nilai pendakian dan Anda harus mengembalikan atau mencetak daftar / larik nilai yang diurutkan dari yang paling mudah ke yang paling sulit.
Jika input kosong, kembalikan struktur data kosong; jika tidak, input akan selalu valid.
Uji kasus
Input | Output
[] | []
['V1'] | ['V1']
['V7', 'V12', 'V1'] | ['V1', 'V7', 'V12']
['V13', 'V14', 'VB', 'V0'] | ['VB', 'V0', 'V13', 'V14']
['V0+', 'V0', 'V16', 'V2', 'VB', 'V6'] | ['VB', 'V0', 'V0+', 'V2', 'V6', 'V16']
Ini adalah tantangan kode-golf .
code-golf
array-manipulation
sorting
Chris_Rands
sumber
sumber
Jawaban:
Python 2,
5854 bytesTry it online!
How it works
sumber
a=>a.sort((a,b,B10=0)=>(g=s=>eval(s.slice(1)+10))(a)>g(b))
is 58 bytes.a=>a.sort((a,b)=>(g=s=>eval(s.slice(B10=1)+10))(a)-g(b))
is 2 bytes shorter, but that's still too long.JavaScript (ES6) / Firefox, 53 bytes
Test cases
For Firefox:
Show code snippet
For Chrome or Edge (+4 bytes):
Show code snippet
How?
We apply 3 successive transformations that lead to lexicographically comparable strings.
sumber
a=>a.sort((a,b)=>(g=s=>parseInt(s,32)%334+s)(a)>g(b))
on Chrome, it doesn't give the correct answer tof(["VB","V0","V0+","V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16","V17"])
I'm not sure why; the edge-compatible version works fine on chrome.Husk, 5 bytes
Try it online! The results are printed one per line, but internally this is a function that takes and returns a list of strings.
Explanation
This is surprisingly similar to Martin's Retina answer. First we do
Öm±
, meaning "order by mapping is-digit". This putsVB
,V0
andV0+
in the correct order, since they are compared as[0,0]
,[0,1]
and[0,1,0]
. Next we doÖi
, meaning "order by integer value". Given a string,i
returns the first sequence of digits occurring in it as an integer, or 0 if one is not found. The three strings above are all mapped to 0 and the sort is stable, so they will be in the correct order in the output.sumber
Retina, 14 bytes
Try it online!
Explanation
Replace
B
with!
so that the lexicographic order of grades putsVB
(or thenV!
) in front of all the numeric grades.Sort all input lines lexicographically. This doesn't give the right result but it does order
V! < V0 < V0+
correctly.Turn
V!
back intoVB
.Sort the lines numerically. Retina simply looks for the first decimal number in a string to determines its sort key. If there is no number (such as for
VB
), it sets the value to0
. That means all ofVB
,V0
andV0+
have the same sort key. But Retina's sort is stable and we've already put them in the correct relative order.sumber
V, 3 bytes
Try it online!
How does it work?
This command is almost a valid solution, since every line that can't be sorted by numbers (AKA,
VB
) will be placed at the beginning, without the order changed. However, since it's only looking at numbers, it can't distinguish betweenV0
andV0+
. Since Vim uses a stable sort, whichever of these came first will remain first after sorting it. So...sumber
C#,
121838283 bytesSaved 39 bytes thanks to TheLethalCoder and LiefdeWen
Try it online!
Bytecount includes
using System.Linq
.How?
VB
, set the value to -1, if it's equal toVB0+
, set the value to 0.V
.Might be a bit of a hack, but it works! :)
sumber
ToArray()
anIOrderedEnumerable
should be fine..Remove(0,1)
for additional -1 byte :)Ruby,
52 4241 bytesTry it online!
How it works:
Turn the problem around, produce the full sorted list, then get the intersection with our input.
Thanks Lynn for saving 1 byte.
sumber
->x{[?B,0,"0+",*1..17].map{|a|"V#{a}"}&x}
saves a byte.Pyth, 16 bytes
Port of Python answer by OP.
Test suite.
sumber
05AB1E,
17138 bytesTry it online!
sumber
†
is better thanD"VB"åiÁ
by a loong-shot.Jelly, 9 bytes
A monadic link taking a list of lists of characters and returning the sorted list.
Try it online! (the footer formats the result nicely)
How?
sumber
Haskell, 55 bytes
Try it online!
sumber
To kick things off here is my Python 3 solution...Apologies, posted this too soon against convention, now re-posting...Python 3,
6967 bytesTry it online!
sumber
Swift 3, 102 bytes
This is a function. You can call it as such:
Try it online!
How does this work?
This is basically a port of the amazing Javascript answer by @Arnauld, but optimized for Swift.
It maps each of the values to lexicographically orderable Strings as shown in the table below:
Code Explanation
String((Int($0,radix:32) ?? 992)%334)
- Converts each String from a base-32 Number to Decimal. In case the value is "V0+", the call toInt(_:radix:)
will return nil, and we take the value of "V0", 992. We additionally take the result ofmod 334
, and finally convert it to String.+$0
- Adds the current value to the String created above. For instance, if the String isV9
, the function above returns333
and we addV9
, resulting in333V9
.var r={...}
- Declares a variabler
to an anonymous closure, because it saves lots of bytes since it's used twice.func f(l:[String])
- Defines a functionf
with a parameterl
, a list of Strings.print(l.sorted(by:{r($0)<r($1)}))
- Prints the result of sorting the given list, with the key being the variabler
defined above.sumber
PowerShell, 45 bytes
Try it online!
Uses the same process as G B's Ruby answer to construct the full argument list in sorted order, then select out those that are
-in
the input list.sumber
Google Sheets, 142 bytes
Input is a string in
A1
with each entry separated by a comma.Output is the formula's cell plus the
n-1
cells below it wheren
is the number of entries inA1
.It's a long, messy formula so let's unpack it.
If(A1="","",~)
fixes the null input. Without this, an empty input returns a#VALUE!
error because theSplit
function doesn't work on empty inputs.Transpose(Split(A1,","))
splitsA1
at the commas and transposes it into a column because theSort
function only works on columns.Transpose(IfError(Find(),Value()+9))
is breaks into these pieces:Find(Split(A1,","),"VBV0V0+")
tries to find each parameter in that string. These first three are the only ones that must be sorted as strings so we useFind
to get their sort order.Value(Mid(Split(A1,","),2,3))+9
gets the numerical value of the grade. This only matters for V1 and higher so they sort numerically just fine. The+9
at the end is to ensure V1 comes after V0+ since itsFind
value would be5
. Technically, then, only+5
is required but it costs me no more bytes to make extra double sure it sorts correctly.IfError(Find(~),Value(~))
returns theFind
value if the string was found (i.e., the grade is VB, V0, or V0+). If it can't be found, it returns the numerical value of the grade plus nine.Transpose(IfError(~))
again turns it into a column soSort
can use it.Sort(Transpose(Split(~)),Transpose(IfError(Find(~),Value(~)+9)),1)
wraps it all up by sorting the split input using the custom sort order ascending.ArrayFormula(~)
wraps the entire thing so it returns the results as an array instead of just returning the first value in that array. This is what causes the formula in one cell to fill the cells below it, too.sumber
Bash + coreutils, 21
GNU
sort
's-V
ersion sorting mode almost does what we want. Switch theB
for a.
and we're done.Try it online.
sumber
Haskell,
90848361 bytesTry it online!
f
is a function that converts climbing grades to strings that can be compared. If convertsVB
to be the empty string so it gets the highest priority, it then replacesV1
withX
in strings that are three long to lower the priority ofV10
-V17
. For the remainder we do nothing.To sort the list we use
Data.Lists
'ssortOn
function (as suggested by Lynn) to create a point-free function.sumber
g=sortOn f
, which is also inData.List
.f(_:'1':a)='X':a
saves 4 bytes![a]
otherwiseV1
will be pattern matched which is the problem I am trying to circumvent.R, 45 bytes
How does this work?
sumber
Python2, 77 bytes
sumber
Jelly,
1711 bytesTry it online!
sumber
TXR Lisp: 45 bytes
Run:
sumber
Perl 5, 56 + 1 (-a) = 57 bytes
Try it online!
sumber