Skip to content

Instantly share code, notes, and snippets.

@mark-d-holmberg
Last active October 30, 2019 03:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark-d-holmberg/fcd29e4540c9f5fc2c47283d33110882 to your computer and use it in GitHub Desktop.
Save mark-d-holmberg/fcd29e4540c9f5fc2c47283d33110882 to your computer and use it in GitHub Desktop.
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