Perl Weekly Challenge 226
After a long hiatus from the Perl Weekly Challenges, I found a bit of respite in week to pull one through. It is only for the first task, as I didn't have to invest too many neurons when I read it during my flight. Therefore, I chose Ruby to solve it this time, since I am currently on a Ruby project, and the language solves in near "english-sentence" time (lol). [See Task 1]:
def main(s, indices)
raise "Error: Length of the arguments do not match!" unless s.length == indices.length
hash = indices.zip(s.chars).to_h
result = hash.sort_by { |k, v| k }.map { |v| v[1] }
result.join
end
This solution is doggone near self-explanatory:
- Raise an error if the lengths of the strings do not match
- Build a "hash/map/dict" by
zip
ing the string'schars
- Take the hash; sort it by its
k
ey and pull thev
alue out - Assign the expression's result to ...
result
(lol) - Join the entire thing together into a string
I miss doing these challenges. Perhaps, I'll get cracking on [Task 2] sometime this week. I may even catch up and do it all in Ruby and NodeJS (*since those are the tools I'm currently using for a client ;))
Happy hacking!
Subscribe to my blog via RSS.