Skip to content

Instantly share code, notes, and snippets.

@pibako
Created November 14, 2013 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pibako/7472932 to your computer and use it in GitHub Desktop.
Save pibako/7472932 to your computer and use it in GitHub Desktop.
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