Skip to content

Instantly share code, notes, and snippets.

@khpatel4991
Created August 29, 2016 03:29
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 khpatel4991/df8d4a8840a6f8eda58534c2c121aa49 to your computer and use it in GitHub Desktop.
Save khpatel4991/df8d4a8840a6f8eda58534c2c121aa49 to your computer and use it in GitHub Desktop.
Add Whitespace based on validity o the words
def repair_whitespace(str)
buff = []
final_words = []
str.each_char do |char|
buff << char
if valid_word?(buff.join)
final_words << buff.join
buff = []
end
end
final_words.join(' ') end
def valid_word?(word)
%w(this is a valid string).include?(word)
end
puts repair_whitespace('thisis')
puts repair_whitespace('thisisavalidstring')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment