Created
November 8, 2012 15:58
-
-
Save koleksiuk/4039692 to your computer and use it in GitHub Desktop.
This file contains 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 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 |
This file contains 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 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 |
This file contains 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 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