Post History
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...
Answer
#4: Post edited
# [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
- # [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
- # [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
# [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}} ```