Epic Systems Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
public static void main(String[] args) {
String startDate = "01012000";
String endDate = "12122090";
generateDates(startDate, endDate);
}
public static void generateDates(String startDate, String endDate) {
Calendar calStart = new GregorianCalendar(Integer.parseInt(startDate.substring(4)), Integer.parseInt(startDate
.substring(2, 4)) - 1, Integer.parseInt(startDate.substring(0, 2)));
Calendar calEnd = new GregorianCalendar(Integer.parseInt(endDate.substring(4)), Integer.parseInt(endDate
.substring(2, 4)) - 1, Integer.parseInt(endDate.substring(0, 2)));
for (int i = 1; i <= 12; i++) {
String month = String.valueOf(i);
if (month.length() == 1) {
month = "0" + month;
}
for (int j = 1; j <= 31; j++) {
String day = String.valueOf(j);
if (day.length() == 1) {
day = "0" + day;
}
String monthDay = month + day;
String year = new StringBuilder(monthDay).reverse().toString();
Calendar currentDate = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, 1);
if (Integer.parseInt(day) > checkMaxDays(currentDate)) {
continue;
}
if (currentDate.getTimeInMillis() < calStart.getTimeInMillis()
|| currentDate.getTimeInMillis() > calEnd.getTimeInMillis()) {
continue;
}
System.out.println(monthDay + year);
}
}
}
private static int checkMaxDays(Calendar currentDate) {
return currentDate.getActualMaximum(Calendar.DAY_OF_MONTH);
}
You can also look at this: question?id=3236729
// Generate all palindrom date in the form of MMDDYYYY
bool bPalindrom( char* s)
{
return s[0] == s[7] && s[1] == s[6] && s[2] == s[5] && s[3] == s[4];
}
void genpalindromDate()
{
char date[9];
int numofMon[] = { 31, 30, 29, 30, 31, 31, 30, 31, 30, 31};
for ( int m = 1 ; m <= 12 ; m++)
for ( int d = 1 ; d <= numofMon[m-1] ; d++)
for ( int y = 1000 ; y <= 2001 ; y++)
{
sprintf( date,"%02d%02d%04d", m, d, y);
if (bPalindrom(date) )
printf("%s :", date);
}
}
Just some thoughts.
1. You can only have one palindrome date in one year.
2. You might need more information from the interviewer, such as:
2.1 Do you need to consider leap year?
2.2 Do you need to consider 30/31 days per month?
2.3 Do you need to consider BC years?
2.4 Do you need to consider missing 0s? such as April 10, 2014 (4102014) or February 10, 2012 (2102012)?
2.5 Can year input ranges from 0000 to 9999? What is the lower and upper bound?
My brute force practice:
ideone.com/5MhTaq
I only outputed all palindrome date in 21st century without considering cases like "2102012".
What is the constrain on the year?
We can limit both day and month to these
days 01,02,03,10,11,12,20,21,22,30,31,13
month 01,10,11
Here is the Java implementation method
==============================
private void pallendromeDates(int start, int end){
GregorianCalendar calstart = new GregorianCalendar();
calstart.set(Calendar.YEAR, start);
calstart.set(Calendar.MONTH, 1); // 1 Jan
calstart.set(Calendar.DAY_OF_MONTH, 1); // new years eve
GregorianCalendar calend =new GregorianCalendar(TimeZone.getDefault());
calend.set(Calendar.YEAR, end);
calend.set(Calendar.MONTH, 12); // 1 Jan
calend.set(Calendar.DAY_OF_MONTH, 31); // new years eve
for( ;!calstart.after(calend); calstart.add(Calendar.DATE, 1)) {
Date current = calstart.getTime();
String pattern = "MMddyyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
String dstr= format.format(current);
String reverse = new StringBuffer(dstr).reverse().toString();
if(dstr.equalsIgnoreCase( reverse)){
System.out.println("Pallendrome="+dstr);
}
}
}
Assume, MMDDYYYY and mmddyyyy are start and end dates respectively. Take MMDDYYYY, find the YYYY part. Let value = YYYY - MMDD. If (value == 0) palindrome. Else if (value > 0) then MMDD = MMDD + value; else YYYY = YYYY - value. Keep adding 1 to the DD, when the last day of the month if reached reset the DD for the beginning of the next month, add one of MM....an so on and so forth
if the format is MMDDYYYY...every year there will be only one palindrome rite....
this is my algorithm for the same:
1. store start date in sd;
2. store end date in ed;
3. store the start year in sy;
4. store the end year in ey;
5. count = ey - sy;
6. for(i=0;i<count;i++) {
7. int *ptr = &sy[n-1]; //(where n = 4)
8. while(n>=0) {
9. cout<< *ptr;
10. ptr--; }
11. cout<<sy;
12. sy++; }
correct me if i m wrong
Not the most efficient solution but does a pretty decent job.
#include <iostream>
using namespace std;
int days_in_month[] = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
class Date {
public:
int year;
int month;
int date;
Date () {}
Date (int y, int m, int d) {
year = y;
month = m;
date = d;
}
bool operator < (const Date& obj) {
if (year == obj.year && month == obj.month)
return date < obj.date ? true : false;
if (year == obj.year)
return month < obj.month ? true : false;
return year < obj.year ? true : false;
}
bool operator > (const Date& obj) {
return ! (*this < obj);
}
};
ostream& operator << (ostream& out, Date& obj) {
if (obj.month < 10) out << "0";
out << obj.month;
if (obj.date < 10) out << "0";
out << obj.date;
out << obj.year;
return out;
}
Date ProcessDate (string& start) {
int y, m, d;
y = m = d = 0;
for (int i = 0; i < 2; i++) {
m = m * 10 + (start[i] - '0');
}
if (m > 12) {
throw "Invalid month entered.";
}
for (int i = 2; i < 4; i++) {
d = d * 10 + (start[i] - '0');
}
if (d > days_in_month[m]) {
throw "Invalid date entered.";
}
for (int i = 4; i < 8; i++) {
y = y * 10 + (start[i] - '0');
}
return Date (y, m, d);
}
Date ProcessDate (int num) {
int y = num % 10000;
int m = num / 1000000;
int d = (num / 10000) % 100;
return Date (y, m, d);
}
int Reverse (int num) {
int rev = 0;
while (num > 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}
int main ( ) {
string start, end;
Date Dstart, Dend;
try {
cout << "Enter start date (mmddyyyy): ";
cin >> start;
Dstart = ProcessDate (start);
cout << "Enter end date (mmddyyyy):";
cin >> end;
Dend = ProcessDate (end);
}
catch (const char* ex) {
cout << ex << endl;
return 0;
}
while ((Dstart < Dend) == true) {
int rev_month = Reverse (Dstart.year % 100);
rev_month = Dstart.year % 100 < 10 ? rev_month * 10 : rev_month;
if (rev_month > 12) { /* No palindrome for this year */
Dstart.year++;
continue;
}
int rev_date = Reverse (Dstart.year / 100);
if (rev_date > days_in_month[rev_month]) {
Dstart.year++;
continue;
}
int palindrome_date = Reverse(Dstart.year) * 10000 + Dstart.year;
Date Palin_date = ProcessDate (palindrome_date);
if (Palin_date < Dend) {
cout << Palin_date << endl;
}
Dstart.year++;
}
return 0;
}
package programs;
import java.util.Scanner;
import java.util.ArrayList;
public class program5 {
public static String [] days={"","31","28","31","30","31","30","31","31","30","31","30","31"};
public static ArrayList<String> dates = new ArrayList<String>();
public static int dateGenerator(String s, String e,int ind)
{
String temp1,temp2,temp3;
int i=ind;
temp1 = s;
System.out.println("Start date is "+temp1);
temp2 = e;
System.out.println("End date is "+temp2);
dates.add(i,temp1);
while(!(dates.get(i).equals(temp2)))
{
String day,month,year;
int dayi,monthi,yeari;
day=month=year=temp3="";
month = month+dates.get(i).substring(0,2);
day = day+dates.get(i).substring(2,4);
year = year+dates.get(i).substring(4,dates.get(i).length());
dayi = Integer.parseInt(day);
monthi = Integer.parseInt(month);
yeari = Integer.parseInt(year);
if((yeari%400!=0)||(yeari%4!=0))
{
if(dayi<(Integer.parseInt(days[monthi])))
{
dayi++;
i++;
if(monthi<10)
{
month = "0"+Integer.toString(monthi);
}
else
{
month =""+Integer.toString(monthi);
}
if(dayi<10)
{
day="0"+Integer.toString(dayi);
}
else
{
day=""+Integer.toString(dayi);
}
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
else
{
if(monthi<12)
{
day = "01";
monthi++;
i++;
if(monthi<10)
{
month = "0"+Integer.toString(monthi);
}
else
{
month =""+Integer.toString(monthi);
}
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
else
{
day = "01";
month = "01";
yeari++;
i++;
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
}
}
else
{
if(dayi<(Integer.parseInt(days[monthi])))
{
dayi++;
i++;
if(monthi<10)
{
month = "0"+Integer.toString(monthi);
}
else
{
month =""+Integer.toString(monthi);
}
if(dayi<10)
{
day="0"+Integer.toString(dayi);
}
else
{
day=""+Integer.toString(dayi);
}
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
else
{
if(monthi<12 && monthi!=2)
{
day = "01";
monthi++;
i++;
if(monthi<10)
{
month = "0"+Integer.toString(monthi);
}
else
{
month =""+Integer.toString(monthi);
}
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
else if(monthi==2)
{
day = "29";
//monthi++;
i++;
month = "0"+Integer.toString(monthi);
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
else
{
day = "01";
month = "01";
yeari++;
i++;
dates.add(i,temp3+month+day+Integer.toString(yeari));
}
}
}
}
return i+1;
}
public static boolean palindrome(String a)
{
String temp = new String();
temp="";
int i=a.length()-1;
while(i>=0)
{
temp=temp+a.charAt(i);
i--;
}
if(temp.equals(a))
{
System.out.println("Palindrome found");
return true;
}
else
return false;
}
public static boolean validation(String b)
{
if(b.length()==8 && (Integer.parseInt(b.substring(0,2))>0 && Integer.parseInt(b.substring(0,2))<=12) && (Integer.parseInt(b.substring(2,4))>0 && Integer.parseInt(b.substring(0,2))<=31))
{
if(Integer.parseInt(b.substring(0,2))==2 && Integer.parseInt(b.substring(2,4))==29)
{
if((Integer.parseInt(b.substring(4,b.length()))%400==0) && (Integer.parseInt(b.substring(4,b.length()))%400==0))
{
return true;
}
else
return false;
}
else if(Integer.parseInt(b.substring(0,2))==2 && Integer.parseInt(b.substring(2,4))>29)
{
return false;
}
else
return true;
}
else
return false;
}
public static boolean validation(String a,String b)
{
if((Integer.parseInt(a.substring(4,a.length()))<=Integer.parseInt(b.substring(4,b.length())))&&(Integer.parseInt(a.substring(2,4))<=Integer.parseInt(b.substring(2,4))) && (Integer.parseInt(a.substring(0,4))<=Integer.parseInt(b.substring(0,4))))
return true;
else
return false;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int ind=0;
while(true)
{
String start,end,temp[];
System.out.print("Enter a start date in MMDDYYYY format: ");
start = in.next();
System.out.println();
System.out.print("Enter a end date in MMDDYYYY format: ");
end = in.next();
if(!validation(start))
{
System.out.println("Enter a valid start date!!!!");
continue;
}
if(!validation(end))
{
System.out.println("Enter a valid end date!!!!");
continue;
}
if(!validation(start,end))
{
System.out.println("Enter a end date either equal to start date or bigger than start date!!!!");
continue;
}
int index = dateGenerator(start,end,ind);
for(int i=ind;i<index;i++)
{
if(palindrome(dates.get(i)))
{
System.out.println(dates.get(i)+" is a palindrome!!!!!!!!!!!!");
}
else
{
System.out.println(dates.get(i)+" is not a palindrome");
}
}
String c;
System.out.print("Do you execute this application again?(y/n):");
c = in.next();
System.out.println(c);
if(c.equals("yes".toString()))
{
ind = index;
continue;
}
else
{
break;
}
}
}
}
I guess, the following solution would be the most efficient one. Since it will run year2-year1 times.
If you have any comments, let me know.
#include <stdio.h>
#include <stdlib.h>
const int n[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int process_date (int year, int input_month, int input_day) {
int t, month, day;
t = year % 100;
month = (t % 10) * 10 + (t / 10);
if ( month < 1 || month > 12 )
return 1;
if ( input_month && month > input_month)
return 1;
t = year / 100;
day = (t % 10) * 10 + (t / 10);
if ( input_day && day > input_day)
return 1;
int max_day;
if ( (year % 4 == 0) && ( month == 2) ) {
max_day = n[month-1] + 1;
}
else
max_day = n[month-1];
if (day <= max_day) {
printf ("palindrome year %d\n", year);
}
return 0;
}
int main (int args, char *argv[]) {
int m1 = 1, d1 = 8, y1 = 9020;
int m2 = 1, d2 = 2, y2 = 9033;
int i;
process_date(y1, m1, d1);
for ( i = y1+1; i < y2; i++) {
process_date(i, 0, 0);
}
process_date(y2, m2, d2);
return 0;
}
public class palindrome {
public static void main(String[] args) {
String[] YYYY={"2000","2001","2003","9220","2020","2021"};
for (String s:YYYY){
palin(s);
}
}
private static void palin(String year) {
String mon=getmon(year);
String day=getday(year);
boolean check=checkmonthday(mon,day,year);
if(check){
String date=mon+day+year;
System.out.println("palindrom day for year "+year+"is:"+date);
}else{
System.out.println("there is no palindrom for year "+year); }
}
private static boolean checkmonthday(String mon,String day,String year) {
boolean check=false;
int m=Integer.parseInt(mon);
int d=Integer.parseInt(day);
int y=Integer.parseInt(year);
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12){
if (d>0&&d<=31){
check=true;
}
}else if (m==2){
if((y%100!=0&&y%4==0)||y%400==0){
if(d>0&&d<=29)
check=true;
}else{
if(d>0&&d<=28)
check=true;
}
}else if (m==4||m==6||m==9||m==11){
if (d>0&&d<=30){
check=true;
}
}else{
check=false;
}
return check;
}
private static String getday(String year) {
char fir=year.charAt(1);
char sec=year.charAt(0);
String d=Character.toString(fir)+Character.toString(sec);
return d;
}
private static String getmon(String year) {
char fir=year.charAt(3);
char sec=year.charAt(2);
String m=Character.toString(fir)+Character.toString(sec);
return m;
}
}
Python working code. correct me if you find bugs ;)
Algo: for each YYYY in range, reverse it and assign to MMDD, validate if MMDD is valid format. Also validate the special case on Feb in leap years.
"""
2:30
@Python 2.7
Write a program to generate all palindrome dates by taking the beginning and the ending dates as an input from the user. The format of the date is given as MMDDYYYY.
"""
class PaDates(object):
def __init__(self, startDates, endDates):
if startDates is None or endDates is None:
print 'Invalid Dates'
raise SystemExit
self._startDates = startDates
self._endDates = endDates
self._startYYYY = self._startDates[-4:]
self._endYYYY = self._endDates[-4:]
self._output = []
def validateDates(self, mmdd, yyyy):
flag = False
mm = int(mmdd[:2])
dd = int(mmdd[-2:])
monthArray = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if 0 < mm < 13 and 0 < dd <= monthArray[mm - 1]:
flag = True
# exclude special cases that we have invalid 29 on Feb.
if mm == 2 and dd == 29 and not ((yyyy % 400 == 0) or (yyyy % 100 != 0 and yyyy % 4 == 0)):
flag = False
return flag
def gen(self):
for i in range(int(self._startYYYY), int(self._endYYYY) + 1):
mmdd = str('%04d' % i)[::-1]
if self.validateDates(mmdd, i):
tmp = mmdd + str('%04d' % i)
self._output.append(tmp)
def printAll(self):
print ', '.join(self._output)
if __name__ == '__main__':
pd = PaDates('04020001', '04023000')
pd.gen()
pd.printAll()
Let me know if anything is wrong
#include <stdio.h>
void palindate(int year, char *pdate)
{
int mon[] = {31, 29,31, 30, 31, 30, 31,31,30,31,30,31};
int ret = 0;
int d1 = year%10;
int d2 = (year%100)/10;
int m1 = (year%1000)/100;
int m2 = year/1000;
int d = 10*d1 + d2;
int m = 10*m1 + m2;
if (m > 0 && m <13)
{
if (d >0 && d <= mon[m-1])
{
ret = 1;
if (year%4 != 0 && m == 2 && d == 29)
ret = 0;
}
}
sprintf(pdate, "%d%d%d%d%d", d1,d2,m1,m2,year);
if (ret)
printf("%s\n", pdate);
}
int main()
{
char arr[5];
int syr = 2001;
int eyr = 2040;
for (int i = syr;i<=eyr;i++)
palindate(i, arr);
}
If I miss something, please let me know
public static void palidromeDate(String beginDate, String endDate){
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int beginMonth = Integer.parseInt(beginDate.substring(0, 2));
int beginDay = Integer.parseInt(beginDate.substring(2, 4));
int beginYear = Integer.parseInt(beginDate.substring(4));
int endMonth = Integer.parseInt(endDate.substring(0, 2));
int endDay = Integer.parseInt(endDate.substring(2, 4));
int endYear = Integer.parseInt(endDate.substring(4));
String result = "";
for(int year = beginYear; year <= endYear; year++){
int month;
if(year == beginYear)
month = beginMonth;
else
month = 1;
for(; month <= 12; month++){
int day;
if(year == beginYear && month == beginMonth)
day = beginDay;
else
day = 1;
for(; day < days[month - 1]; day++){
if(year == endYear && month == endMonth && day > endDay)
return;
String currentMonth = "";
String currentDay = "";
if(month < 10)
currentMonth = "0" + month;
else
currentMonth = "" + month;
if(day < 10){
currentDay = "0" + day;
}
else
currentDay = "" + day;
result = currentMonth + currentDay + year;
int i = 0;
int j = result.length() - 1;
while(i < j){
if(result.charAt(i) == result.charAt(j)){
i++;
j--;
continue;
}
else
break;
}
if(i > j)
System.out.println(result);
}
}
}
}
public static void palidromeDate(String beginDate, String endDate){
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int beginMonth = Integer.parseInt(beginDate.substring(0, 2));
int beginDay = Integer.parseInt(beginDate.substring(2, 4));
int beginYear = Integer.parseInt(beginDate.substring(4));
int endMonth = Integer.parseInt(endDate.substring(0, 2));
int endDay = Integer.parseInt(endDate.substring(2, 4));
int endYear = Integer.parseInt(endDate.substring(4));
String result = "";
for(int year = beginYear; year <= endYear; year++){
int month;
if(year == beginYear)
month = beginMonth;
else
month = 1;
for(; month <= 12; month++){
int day;
if(year == beginYear && month == beginMonth)
day = beginDay;
else
day = 1;
for(; day < days[month - 1]; day++){
if(year == endYear && month == endMonth && day > endDay)
return;
String currentMonth = "";
String currentDay = "";
if(month < 10)
currentMonth = "0" + month;
else
currentMonth = "" + month;
if(day < 10){
currentDay = "0" + day;
}
else
currentDay = "" + day;
result = currentMonth + currentDay + year;
int i = 0;
int j = result.length() - 1;
while(i < j){
if(result.charAt(i) == result.charAt(j)){
i++;
j--;
continue;
}
else
break;
}
if(i > j)
System.out.println(result);
}
}
}
}
package p1;
public class datepalindrome {
public static void main(String args[])
{
String sdate="01012001";
String edate="02288220";
String str=new String();
String str1=new String();
int syear=Integer.parseInt(sdate.substring(4,8));
int eyear=Integer.parseInt(edate.substring(4,8));
for(int i=syear;i<=eyear;i++)
{
str=String.valueOf(i);
for(int j=3;j>=0;j--){
str1=str1.concat(str.substring(j, j+1));
}
str1=str1.concat(str);
int n1=Integer.parseInt((String) str1.subSequence(0, 2));
int n2=Integer.parseInt((String) str1.subSequence(2, 4));
//System.out.println(n1+" "+n2);
if(n2 <= 31 && (n1==1||n1==3||n1==5||n1==7||n1==8||n1==10||n1==12)||n2 <= 30 && (n1==4||n1==6||n1==9||n1==11)||n2 <= 28 && n1==2||n2 <= 29 && n1==2 && i%4==0){
System.out.println(str1);
}
str1="";
}
}
}
public class ValidPalindromeDate {
public static void main(String[] args) {
StringBuffer sb;
int sYear = 1001;
int eYear = 1201;
while(sYear <= eYear){
sb = new StringBuffer(String.valueOf(sYear));
sb.reverse().append(String.valueOf(sYear));
isValid(sb.toString());
sYear ++;
sb = null;
}
}
private static void isValid(String date) {
int[] numDaysinmonth = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int year = Integer.parseInt(date.substring(4));
int month;
int day;
if(Integer.parseInt(date.substring(0))==0)
month = Integer.parseInt(date.substring(1,2));
else
month = Integer.parseInt(date.substring(0,2));
if(Integer.parseInt(date.substring(2))==0)
day = Integer.parseInt(date.substring(3,4));
else
day = Integer.parseInt(date.substring(2,4));
//System.out.println("day:" + day);
//System.out.println("month" + month);
//System.out.println("year" + year);
if(!(year%100 == 0 && year%4 == 0)){//leap year
if(month == 2){//feb
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month] + 1)
System.out.println("This date is valid! " + date);
else
System.out.println("Not a valid date " + date);
}
else{//not feb
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month])
System.out.println("This date is valid! " + date);
else
System.out.println("Not a valid date " + date);
}
}
else{// not a leap year
if(month > 0 && month <= 12 && day > 0 && day <= numDaysinmonth[month])
System.out.println("This date is valid! "+ date);
else
System.out.println("Not a valid date " + date);
}
}
}
Hi,
for a date int the format MMDDYYYY to be palindrome, YYYY must be equal to DDMM. So get the start year STARTYYYY, and the end year ENDYYYY.
Then for each year in the range STARTYYYY -> ENDYYYY generate the date composed by the Strings rev(YYYY) and YYYY.
For each date generated in this way check for the month MM to be included in the interval [01,12]
(revYYYY.charAt(0)=='0' && revYYYY.charAt(1)!='0') || (revYYYY.charAt(0)=='1' && (revYYYY.charAt(1)=='2' || revYYYY.charAt(1)=='1' || revYYYY.charAt(1)=='0'))
and for the day DD to be included in the interval [01-31]
(revYYYY.charAt(2)=='0' || revYYYY.charAt(2)=='1' || revYYYY.charAt(2)=='2') || (revYYYY.charAt(2)=='3' && (revYYYY.charAt(3)=='0' || revYYYY.charAt(2)=='1'))
Example:
Start Date = 01012000
End Date = 31122100
Output = [10022001, 01022010, 11022011, 02022020, 12022021, 03022030, 04022040, 05022050, 06022060, 07022070, 08022080, 09022090]
This is the code that implement this solution:
import java.util.*;
public class PalindromeDatesInterval {
public static String rev(String s) {
String rev = "";
for(int i=s.length()-1;i>=0;i--) {
rev = rev+s.charAt(i);
}
return rev;
}
public static List<String> palindromeDates(String start, String end) {
List<String> dates = new ArrayList<String>();
//System.out.println(start+" "+end);
if(start.length()!=8 || end.length()!=8) {
System.out.println(start+" "+end);
System.out.println("Warning: Wrong Dates Format!");
return null;
}
String startYYYY = start.substring(4,8);
String endYYYY = end.substring(4,8);
for(int i=Integer.parseInt(startYYYY);i<=Integer.parseInt(endYYYY);i++) {
String revYYYY = rev(String.valueOf(i));
String YYYY = String.valueOf(i);
if(((revYYYY.charAt(0)=='0' && revYYYY.charAt(1)!='0') ||
(revYYYY.charAt(0)=='1' && (revYYYY.charAt(1)=='2' || revYYYY.charAt(1)=='1' || revYYYY.charAt(1)=='0')))
&&
((revYYYY.charAt(2)=='0' || revYYYY.charAt(2)=='1' || revYYYY.charAt(2)=='2')
||
(revYYYY.charAt(2)=='3' && (revYYYY.charAt(3)=='0' || revYYYY.charAt(2)=='1')))
) {
String pdate = revYYYY+YYYY;
dates.add(pdate);
}
}
return dates;
}
public static void main(String[] args) {
List<String> pdates = new ArrayList<String>();
String start = "01012000";
String end = "31122100";
pdates = palindromeDates(start,end);
System.out.println(pdates);
}
}
public class palindromedate {
public static boolean palindrom_check(String a)
{
int i=0;
int j=a.length() - 1;
while(i<=j)
{
if(a.charAt(i)!=a.charAt(j))
return false;
i++;
j--;
}
return true;
}
public static void main(String[] args)
{
String begin_date = "01012001";
String end_date="12312050";
int begin_month = Integer.parseInt(begin_date.substring(0,2));
int end_month = Integer.parseInt(end_date.substring(0,2));
int begin_day = Integer.parseInt(begin_date.substring(2,4));
int end_day = Integer.parseInt(end_date.substring(2,4));
int begin_year = Integer.parseInt(begin_date.substring(4,8));
int end_year = Integer.parseInt(end_date.substring(4,8));
for(int year=begin_year; year<=end_year; year++)
{
int month = 1;
if(year==begin_year)
month = begin_month;
int month_check = 12;
if(year==end_year)
month_check = end_month;
for(;month<=month_check;month++)
{
int day=1;
int day_check = 31;
if(year==begin_year && month==begin_month)
day= begin_day;
if(year==end_year && month==end_month)
day_check = end_day;
else if(month==2)
day_check = 28;
else if(month==8)
day_check = 31;
else if(month%2==0)
day_check = 30;
for(;day<=day_check;day++)
{
String tmp="";
if(month<10)
tmp=tmp+"0"+month;
else
tmp=tmp+month;
if(day<10)
tmp=tmp+"0"+day;
else
tmp=tmp+day;
tmp=tmp+year;
if(palindrom_check(tmp))
System.out.println(tmp);
}
}
}
}
}
if the format is MMDDYYYY...every year there will be only one palindrome rite....so store mmdd in 1 array....reverse of yyyy in another array...if both the arrays are equal: palindrome; else (endYYYY) - (startYYYY) = count; use a while loop for count times and increase start yyyy everytime...and print yyyy in rrevesre order and then actual order.....so for example if the year 2001....reverse this and print...10/02...and den print correct order 2001....
- XXX March 29, 2012correct me if i m wrong