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

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 attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

5 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+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 attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Another way of equal length (3 comments)
+3
−0

Vyxal, 12 bytes

µ«,←⋎„¶ɽ₌Ż«ḟ

Try it Online!

Outputs as a list of characters. Sort by index in (compressed) tibdfghklpqyj.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt, 23 16 15 13 bytes

Lexicographical sort using the custom alphabet tipbdghkflyqj. I/O as characters arrays, sorts in ascending order.

n`“pbdghkf§qj

Try it

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Python 3, 43 bytes

lambda x:sorted(x,key='tibdfghklpqyj'.find)

Try it online!

Sorts based on index in a sorted string. Inputs as a string and outputs as a list.

-14 bytes by replacing .index() with .find()

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

C (gcc), 152 bytes

r;e(c){r=c=='j'?5:strchr("bdfghklpqy",c)?4:c=='i'?3:c=='t'?2:1;}c;s;f(char*i,char*o){s?(e(*i),c=r,e(*o)):(qsort(i,strlen(i),s=1,f),puts(i));return r-c;}

Try it online!

Output:

a
aa
jbita
kitc
now
jtus
jpyitzx

I didn't fine tune it much, but it's delightfully crazy :)

Explanation:

  • The function f takes one input string and one output string as parameter. It returns int as per gcc's old C90 default behavior.
  • Since in-place modification of the input should be fine according to Codidacts input/output rules, I actually never use the output but modify the input. (Would crash & burn in case of string literals though, but the challenge only said strings, so I skipped the copy from input to output buffer.)
  • The global variable s gets zero-initialized and is used to mark the function's state. If not set, then it's the first call. If set, then the function has been called before. (This will have to be reset to zero externally, in order to execute multiple test cases in a row from main())
  • s? checks if s is zero, if so execute qsort and then print.
  • qsort uses the very same function f as its own callback to save a function. This abuses the fact that const void* and char* likely have the same representation. And the function returns int so it fulfils the requirements of a qsort callback. Curiously, qsort didn't raise a compiler error when passed the wrong type of function pointer (this doesn't sound conforming at all, gcc...).
  • When qsort is called, the size of one item parameter also sets the state s to 1.
  • f gets called by qsort many times and now the s? evaluates to 1. It calls the helper function e to evaluate both parameters (i and o are now the qsort callback parameters).
  • e sets a global variable r to value 5,4,3,2 or 1 depending on how which set of characters the passed character belongs to.
  • No benefit from typing out decimal values of these characters since they are all above ASCII value 100, might as well use 'a' character constants then.
  • The result r from the first call to e is stored in c. Then after the next call r-c is returned as the result of qsort callback. This weighs 'j' as lowest, swap to c-r to get the other way around.
  • The function actually always returns r-c but when s==0 nobody cares since the result is placed in the i buffer anyway.
  • When qsort is done, we are back to the first execution of the function, so puts is called once before finishing.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »