Skip to content

Instantly share code, notes, and snippets.

@vjdhama
Created July 25, 2013 13:28
Show Gist options
  • Save vjdhama/6079600 to your computer and use it in GitHub Desktop.
Save vjdhama/6079600 to your computer and use it in GitHub Desktop.
Edx Class 169.1x HW 1-1
#!/usr/bin/env ruby
def palindrome?(str)
str = str.downcase
r = /[\W]/ #/\W/ - A non-word character ([^a-zA-Z0-9_])
str = str.gsub(r,'')
return str == str.reverse
end
def count_words(str)
str = str.downcase
r = /[\W]/
str = str.gsub(r,' ')
arr =str.split(' ')
h = Hash.new
arr.each {|item| h[item] = 0}
arr.each {|item| h[item] += 1}
h
end
test_str = "there goes the neighborhood"
if palindrome? test_str
puts test_str + " is a palindrome!"
else
puts test_str + " is NOT a palindrome!"
end
test_str = "Madam, I'm Adam"
if palindrome? test_str
puts test_str + " is a palindrome!"
else
puts test_str + " is NOT a palindrome!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment