Created
July 25, 2013 13:28
-
-
Save vjdhama/6079600 to your computer and use it in GitHub Desktop.
Edx Class 169.1x HW 1-1
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
#!/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