Created
February 24, 2015 22:51
-
-
Save worace/c70dec125f756dde7b4b to your computer and use it in GitHub Desktop.
rotations
This file contains hidden or 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 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