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

Post History

66%
+2 −0
Challenges Sort letters by height

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...

posted 10mo ago by Taeir‭  ·  edited 10mo ago by Taeir‭

Answer
#4: Post edited by user avatar Taeir‭ · 2023-07-17T20:37:10Z (10 months ago)
Removed variable
  • # [Ruby](https://www.ruby-lang.org/en/), 53 bytes
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.
  • Works in Ruby 2.7 and Ruby 3.
  • ---
  • ### Explanation
  • * `s` is a variable (the function you can call)
  • * `->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:
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/en/), ~~53~~ 51 bytes
  • ```ruby
  • ->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • 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:
  • ```ruby
  • ->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
#3: Post edited by user avatar Taeir‭ · 2023-07-17T19:55:18Z (10 months ago)
  • # [Ruby](https://www.ruby-lang.org/en/), 53 bytes
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.
  • Works in Ruby 2.7 and Ruby 3.
  • ---
  • ### Explanation
  • * `s` is a variable (the function you can call)
  • * `->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 falsey, make it `-1` instead.
  • #### 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:
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/en/), 53 bytes
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.
  • Works in Ruby 2.7 and Ruby 3.
  • ---
  • ### Explanation
  • * `s` is a variable (the function you can call)
  • * `->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:
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
#2: Post edited by user avatar Taeir‭ · 2023-07-17T19:54:20Z (10 months ago)
  • # [Ruby](https://www.ruby-lang.org/en/), 53 bytes
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.
  • Works in Ruby 2.7 and Ruby 3.
  • ---
  • ### Explanation
  • * `s` is a variable (the function you can call)
  • * `->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 `nil`, make it `-1` instead.
  • #### 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:
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
  • # [Ruby](https://www.ruby-lang.org/en/), 53 bytes
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index(_1)||-1}}
  • ```
  • [Try it online!](https://jdoodle.com/ia/Kcq)
  • You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.
  • Works in Ruby 2.7 and Ruby 3.
  • ---
  • ### Explanation
  • * `s` is a variable (the function you can call)
  • * `->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 falsey, make it `-1` instead.
  • #### 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:
  • ```ruby
  • s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
  • ```
#1: Initial revision by user avatar Taeir‭ · 2023-07-17T19:54:00Z (10 months ago)
# [Ruby](https://www.ruby-lang.org/en/), 53 bytes

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

[Try it online!](https://jdoodle.com/ia/Kcq)

You can call it as `s.(<input here>)`, e.g. `s.("tick")` gives `["c", "t", "i", "k"]`.

Works in Ruby 2.7 and Ruby 3.

---
### Explanation
* `s` is a variable (the function you can call)
* `->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 `nil`, make it `-1` instead.

#### 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:

```ruby
s=->i{i.chars.sort_by{"tibdfghklpqyj".index _1 or-1}}
```