Created
August 29, 2016 03:29
-
-
Save khpatel4991/df8d4a8840a6f8eda58534c2c121aa49 to your computer and use it in GitHub Desktop.
Add Whitespace based on validity o the words
This file contains 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
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