Skip to content

Instantly share code, notes, and snippets.

@johan--
Forked from pibako/reverse.rb
Last active August 29, 2015 14:26
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 johan--/58fc33f6f2b0f81c4347 to your computer and use it in GitHub Desktop.
Save johan--/58fc33f6f2b0f81c4347 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