-
-
Save johan--/58fc33f6f2b0f81c4347 to your computer and use it in GitHub Desktop.
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
input = "abc cba" # => "abc cba" | |
input = "cB!a This is a valid\n input, you have to read the specification carefully! abc" # => "cB!a This is a valid\n input, you have to read the specification carefully! abc" | |
input = input.downcase # => "cb!a this is a valid\n input, you have to read the specification carefully! abc" | |
input = input.gsub(/\W/) { |c| # => "cb!a this is a valid\n input, you have to read the specification carefully! abc" | |
c =~ /\s/ ? " " : "" # => "", " ", " ", " ", " ", " ", " ", "", " ", " ", " ", " ", " ", " ", " ", "", " " | |
} # => "cba this is a valid input you have to read the specification carefully abc" | |
arr = input.split(" ") # => ["cba", "this", "is", "a", "valid", "input", "you", "have", "to", "read", "the", "specification", "carefully", "abc"] | |
result = [] # => [] | |
while !arr.empty? # => true, true, true, true, true, true, true, true, true, true, true, true, true, true, false | |
v = arr.shift # => "cba", "this", "is", "a", "valid", "input", "you", "have", "to", "read", "the", "specification", "carefully", "abc" | |
if arr.include?(v.reverse) && v != v.reverse # => true, false, false, false, false, false, false, false, false, false, false, false, false, false | |
result << v # => ["cba"] | |
result << v.reverse # => ["cba", "abc"] | |
# arr.delete v.reverse # => "abc" | |
end # => ["cba", "abc"], nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil | |
end # => nil | |
puts result.join(",") # => nil | |
# >> cba,abc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment