Last active
October 6, 2015 13:56
-
-
Save drhuffman12/a5ad839b71e8ca334a2a 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
=begin | |
https://gist.github.com/drhuffman12/a5ad839b71e8ca334a2a | |
Problem statement: | |
Create a class named PalindromeDetector. Within that class, implement a | |
static function named GetPalindromeYears that takes a start and end date as parameters and | |
returns a distinct set of Dates in chronological order(inclusive of start and end dates) | |
that, when formatted as MMddyyyy, are palindromes. | |
A palindrome is a string that reads the same forwards and backwards, | |
for example: Nov 2, 2011 (11022011). You may add additional methods | |
to this class if you choose, however this method's signature should | |
remain unmodified. | |
Example Usage: | |
In code: | |
start_date_str = 'Nov 2, 2001' | |
end_date_str = 'Nov 2, 2014' | |
PalindromeDetector.palindrome_years(start_date_str, end_date_str) #=> ["01022010", "11022011"] | |
Via Command line: | |
ruby palindrome_detector.rb "Nov 2, 2001" "Nov 2, 2014" #=> Palindrome Dates from 'Nov 2, 2001' to 'Nov 2, 2014' are: ["01022010", "11022011"] | |
=end | |
require 'date' | |
class PalindromeDetector | |
def self.palindrome_years(start_date_str, end_date_str) | |
start_date = Date.parse(start_date_str) | |
end_date = Date.parse(end_date_str) | |
palindromes = [] | |
(start_date..end_date).each do |_date| | |
_date_formatted = _date.strftime('%m%d%Y') | |
palindromes << _date_formatted if _date_formatted == _date_formatted.reverse | |
end | |
palindromes | |
end | |
def self.GetPalindromeYears(start_date_str, end_date_str) # Java method name syntax, since this came from a Java context. | |
palindrome_years(start_date_str, end_date_str) | |
end | |
end | |
if __FILE__ == $0 | |
start_date_str = ARGV[0] | |
end_date_str = ARGV[1] | |
palindromes = PalindromeDetector.palindrome_years(start_date_str, end_date_str) | |
puts "Palindrome Dates from '#{start_date_str}' to '#{end_date_str}' are: #{palindromes.inspect}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment