Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Sort letters by height

Parent

Sort letters by height

+3
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+2
−0

Ruby, 53 51 bytes

->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}

Try it online!

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 of a in b. It returns nil when not found.
  • a||-1 if a is falsy, make it -1 instead. (And falsy is much better defined than in JavaScript, this will only turn nil and false 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}}
History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Another way of equal length (3 comments)
Another way of equal length
trichoplax‭ wrote 9 months ago

I couldn't find a way to shorten this, just ended up with the same length. You can use 0 instead of -1 but only if you add an extra character to the string to prevent the index ever being zero, so it costs a byte to save a byte...

->i{i.chars.sort_by{"_tibdfghklpqyj".index(_1)||0}}
trichoplax‭ wrote 9 months ago · edited 9 months ago

Still the same length:

->i{i.chars.sort_by{"jbdfghklpqyit".index(_1)||13}}

Giving up now...

Taeir‭ wrote 9 months ago

Yup, I tried both of those and ran into the same issues 😉