Last active
October 30, 2019 03:45
-
-
Save mark-d-holmberg/fcd29e4540c9f5fc2c47283d33110882 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Solver | |
def self.solve!(my_array, my_string) | |
# my_array.each { |k| my_string.gsub!(k, " #{k} ") } | |
# my_string.split.join(' ') | |
# Working: | |
my_string.gsub(/(#{my_array.sort_by(&:length).reverse.join("|")})\s*/, ' \1').split.join(' ') | |
# my_string.gsub(/(#{my_array.join("|")})\s*/, ' \1') | |
# my_array.sort_by(&:length).each { |k| my_string.gsub!(/#{k}\s*/, " #{k}") } | |
end | |
end | |
[ | |
[ | |
%w[therapy fun is], | |
'therapyisfunfunfunfunis' | |
], | |
[ | |
%w[hello world good bye], | |
'worldgoodgoodbye' | |
], | |
[ | |
%w[foo bar baz the therapy], | |
"oofrabzabtherapy" | |
] | |
].map do |my_array, my_string| | |
Solver.solve!(my_array, my_string) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment