Comments on Sort letters by height
Parent
Sort letters by height
Given a sequence of lower case letters, sort them into order of height.
Heights
The heights of letters are dependent on font, so for this challenge the height order to be used is as defined below:
acemnorsuvwxz
t
i
bdfghklpqy
j
Letters on the same line are defined to be the same height. The first line is the shortest letters, the last line is the tallest letter.
Input
- A sequence of lower case letters
- This may be a string or any data structure of characters
Output
- A sequence of the same letters in sorted order
- This may be a string or any ordered data structure of characters. It does not need to match the input format (provided it is consistent between inputs)
- For example, you may take input as an array of characters, and output as a string, provided this format does not change for different inputs
- The sort does not need to be stable (letters that are the same height do not need to remain in the same order as the input, even if the input was already sorted)
- The sort may be either ascending or descending
Test cases
Since the sort order can be either ascending or descending, and the sort does not need to be stable, most inputs will have many possible valid outputs. The test cases are in the format "input" : ["valid", "outputs"]
. You may choose any of the valid outputs, but you must output only one of them.
"a" : ["a"]
"aa" : ["aa"]
"atibj" : ["atibj", "jbita"]
"tick" : ["ctik", "kitc"]
"now" : ["now", "nwo", "onw", "own", "wno", "won"]
"just" : ["jtus", "jtsu", "sutj", "ustj"]
"pjztyix" : ["xztipyj", "zxtipyj", "xztiypj", "zxtiypj", "jypitzx", "jypitxz", "jpyitzx", "jpyitxz"]
Explanations are optional, but I'm more likely to upvote answers that have one.
Vyxal, 12 bytes ``` µ«,←⋎„¶ɽ …
2y ago
Ruby, 53 51 bytes ```ruby …
1y ago
[Python 3], 43 bytes …
2y ago
Japt, 23 16 15 13 bytes Lex …
1y ago
[C (gcc)], 152 bytes …
2y ago
Post
Ruby, 53 51 bytes
->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
Works in Ruby 2.7 and Ruby 3.
Explanation
-
->i{...}
is a short way to define a 1-argument lambda with parameter i -
.chars
will turn a string into an array of characters -
.sort_by
will do a normal sort, transforming each element for sorting purposes to whatever the block returns. -
_1
tells ruby that my block actually wanted a variable and that I'm using it at that location. -
a.index(b)
returns the index ofa
inb
. It returnsnil
when not found. -
a||-1
ifa
is falsy, make it-1
instead. (And falsy is much better defined than in JavaScript, this will only turnnil
andfalse
into -1)
Fun facts
In Ruby parentheses are optional, so I would have almost been able to do .index _1||-1
. Unfortunately, the precedence of ||
is such that this now works on the argument (_1
) rather than the output of the whole function. One could use or
since it has different precedence than ||
, but this doesn't save space because you now need an extra space. So all this yields us an equally long but possibly more convoluted:
->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
0 comment threads