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 Weave Strings Together

Ruby, 51 bytes ->a{(0..a.map(&:size).max).map{|n|a.map{_1[n]}}*""} ->a{ } # lambda taking array `a` (0..a.map(&:size).max) ...

posted 3y ago by snail_‭  ·  edited 3y ago by snail_‭

Answer
#2: Post edited by user avatar snail_‭ · 2021-09-19T04:46:04Z (about 3 years ago)
explanation
  • # [Ruby], 51 bytes
  • ```ruby
  • ->a{(0..a.map(&:size).max).map{|n|a.map{_1[n]}}*""}
  • ```
  • This exploits the following behaviors:
  • - Indexing past the bound of a string is `nil`
  • ```irb
  • "abc"[5]
  • => nil
  • ```
  • - `nil.to_s` is the empty string
  • ```irb
  • nil.to_s
  • => ""
  • ```
  • - `Array#join` joins nested arrays recursively
  • ```irb
  • [1,2,[3,4],5,[6,[7]]]*""
  • => "1234567"
  • ```
  • [Try it online!][TIO-ktpbbal1]
  • [Ruby]: https://www.ruby-lang.org/
  • [TIO-ktpbbal1]: https://tio.run/##KypNqvyfZvtf1y6xWsNATy9RLzexQEPNqjizKlUTyK4AEQXVNXk1iRBGcU1xdF5sba2WklLt/5zMvNRiWxsbV383veKCnMwSruzMvHyuZKBwbiJXbn5ZZioXUI6roLSkWCFNTwOsXvM/AA "Ruby – Try It Online"
  • # [Ruby], 51 bytes
  • ```ruby
  • ->a{(0..a.map(&:size).max).map{|n|a.map{_1[n]}}*""}
  • ->a{ } # lambda taking array `a`
  • (0..a.map(&:size).max) # range of 0..length of longest string
  • .map{|n| } # map over indices range
  • a.map{ } # map over strings
  • _1[n] # nth character in string (or nil)
  • *"" # recursive join to string
  • ```
  • This exploits the following behaviors:
  • - Indexing past the bound of a string is `nil`
  • ```irb
  • "abc"[5]
  • => nil
  • ```
  • - `nil.to_s` is the empty string
  • ```irb
  • nil.to_s
  • => ""
  • ```
  • - `Array#join` joins nested arrays recursively
  • ```irb
  • [1,2,[3,4],5,[6,[7]]]*""
  • => "1234567"
  • ```
  • [Try it online!][TIO-ktpbbal1]
  • [Ruby]: https://www.ruby-lang.org/
  • [TIO-ktpbbal1]: https://tio.run/##KypNqvyfZvtf1y6xWsNATy9RLzexQEPNqjizKlUTyK4AEQXVNXk1iRBGcU1xdF5sba2WklLt/5zMvNRiWxsbV383veKCnMwSruzMvHyuZKBwbiJXbn5ZZioXUI6roLSkWCFNTwOsXvM/AA "Ruby – Try It Online"
#1: Initial revision by user avatar snail_‭ · 2021-09-18T05:00:50Z (about 3 years ago)
# [Ruby], 51 bytes

```ruby
->a{(0..a.map(&:size).max).map{|n|a.map{_1[n]}}*""}
```

This exploits the following behaviors:
- Indexing past the bound of a string is `nil`
```irb
"abc"[5]
=> nil
```
- `nil.to_s` is the empty string
```irb
nil.to_s
=> ""
```
- `Array#join` joins nested arrays recursively
```irb
[1,2,[3,4],5,[6,[7]]]*""
=> "1234567"
```
[Try it online!][TIO-ktpbbal1]

[Ruby]: https://www.ruby-lang.org/
[TIO-ktpbbal1]: https://tio.run/##KypNqvyfZvtf1y6xWsNATy9RLzexQEPNqjizKlUTyK4AEQXVNXk1iRBGcU1xdF5sba2WklLt/5zMvNRiWxsbV383veKCnMwSruzMvHyuZKBwbiJXbn5ZZioXUI6roLSkWCFNTwOsXvM/AA "Ruby – Try It Online"