Skip to content

Instantly share code, notes, and snippets.

@worace
Created February 24, 2015 22:51
Show Gist options
  • Save worace/c70dec125f756dde7b4b to your computer and use it in GitHub Desktop.
Save worace/c70dec125f756dde7b4b to your computer and use it in GitHub Desktop.
rotations
def rotated?(word, original)
if word == original
true
else
(0..word.chars.length).map do |i|
word = (word.chars[1..-1] + [word.chars[0]]).join
return true if word == original
end
false
end
end
require "timeout"
def rotated?(word, original)
Timeout::timeout(5) do
loop do
return true if word.chars.shuffle.join == original
end
end
rescue Timeout::Error
false
end
def rotated?(word, original)
(word + word).include?(original)
end
def rotated?(word, original)
(0..word.chars.length).map do |i|
word[i..-1] + word[0...i]
end.any? do |w|
w == original
end
end
def rotations(word)
(0...word.chars.length).map do |i|
word[i..-1] + word[0...i]
end
end
def rotated?(word, original)
rotations(word).include?(original)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment