Skip to content

Instantly share code, notes, and snippets.

@mark-d-holmberg

mark-d-holmberg/solver.rb

Last active Oct 30, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.