Skip to content

Instantly share code, notes, and snippets.

@koleksiuk
Created November 8, 2012 15:58
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 koleksiuk/4039692 to your computer and use it in GitHub Desktop.
Save koleksiuk/4039692 to your computer and use it in GitHub Desktop.
def isAnagramOfPalindrome (s)
return 0 if s.length < 2 # no need to check 1 or 0 character words
# # returns letters in hash
# f.e. "aabcba" ===> {"c"=>["c"], "b"=>["b", "b"], "a"=>["a", "a", "a"]}
letters = s.split("").group_by{|x| x}
# selects these arrays where size is odd and checks if it's empty
return 1 if letters.select{|key,value| value.length % 2 != 0 }.empty?
return 0 # return for clarity
end
puts isAnagramOfPalindrome("addddaaa") # >> 1
puts isAnagramOfPalindrome("x") # >> 0
def binary_gap(n)
n.to_s(2).scan(/[0]+/).group_by(&:size).max.first rescue 0
end
puts 15.to_s(2) # >> 1111
puts binary_gap(15) # >> 0
puts 1041.to_s(2) # >> 10000010001
puts binary_gap(1041) # >> 5
def symmetryPoint (s)
len = s.length
return 0 if len <= 1
return -1 if len % 2 == 0
if s[0..len/2-1] == s[len/2+1..len].reverse
len/2
else
-1
end
end
puts symmetryPoint("xoxox") # >> 2
puts symmetryPoint("xoxoxg") # >> -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment