Created
October 31, 2022 16:48
-
-
Save NoFishLikeIan/ca81e3b566245d0498ffa0705416560d to your computer and use it in GitHub Desktop.
Inspired by [this](https://twitter.com/robinhouston/status/1569302339508396033?s=20&t=jM-WoWc9Z9SfL1--xVhX8g) tweet, pairing of strings!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Pair" | |
function ↑(a::String, b::String) | |
a * '0' * b * a * '1' * b | |
end | |
""" | |
Unpair one step | |
""" | |
function ↓(p::String) | |
mid = length(p) ÷ 2 | |
diff = findfirst(i -> p[i] != p[i + mid], 1:mid) | |
if p[diff] ∉ ('0', '1') | |
return (p, "") | |
end | |
return (p[1:diff - 1], p[diff + 1:mid]) | |
end | |
"Unpair recursively till bottom of tree" | |
function ⤈(p::String) | |
p′, word = ↓(p) | |
if p′ == p return p′ end | |
return (⤈(p′), word) | |
end | |
encoded = ("Hello" ↑ "world") ↑ "!" | |
⤈(encoded) # (("Hello", "world"), "!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment