Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit 9cdb0f2

Browse files
committed
ThirdLatestDate algorithm
1 parent d674e2f commit 9cdb0f2

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

src/main/java/Date.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author medany
3+
*/
4+
5+
public class Date {
6+
7+
int year;
8+
int month;
9+
int day;
10+
11+
public Date(String date) {
12+
String[] d = date.split("-");
13+
this.day = Integer.parseInt(d[0]);
14+
this.month = Integer.parseInt(d[1]);
15+
this.year = Integer.parseInt(d[2]);
16+
}
17+
18+
@Override
19+
public String toString() {
20+
return day + "-" + month + "-" + year;
21+
}
22+
}

src/main/java/ThirdLatestDate.java

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.text.ParseException;
2+
import java.text.SimpleDateFormat;
3+
import java.util.Comparator;
4+
import java.util.Map;
5+
import java.util.Set;
6+
import java.util.TreeMap;
7+
import java.util.TreeSet;
8+
9+
/**
10+
* @author medany
11+
*/
12+
13+
/*
14+
* find the latest thied date among array of custom Date object of day, month
15+
* and year integers
16+
*/
17+
18+
public class ThirdLatestDate {
19+
20+
public Date solve(Date[] dates) {
21+
22+
Map<Integer, Set<Date>> sortedDates = new TreeMap<Integer, Set<Date>>(new Comparator<Integer>() {
23+
@Override
24+
public int compare(Integer i, Integer j) {
25+
return j.compareTo(i);
26+
}
27+
28+
});
29+
30+
for (Date date : dates) {
31+
if (sortedDates.containsKey(date.year)) {
32+
sortedDates.get(date.year).add(date);
33+
} else {
34+
Set<Date> yearDates = new TreeSet<Date>(new Comparator<Date>() {
35+
36+
SimpleDateFormat f = new SimpleDateFormat("dd-MM-yyyy");
37+
38+
public int compare(Date d1, Date d2) {
39+
40+
java.util.Date date1 = null, date2 = null;
41+
try {
42+
date1 = f.parse(d1.day + "-" + d1.month + "-" + d1.year);
43+
date2 = f.parse(d2.day + "-" + d2.month + "-" + d2.year);
44+
} catch (ParseException e) {
45+
}
46+
return date2.compareTo(date1);
47+
}
48+
});
49+
50+
yearDates.add(date);
51+
sortedDates.put(date.year, yearDates);
52+
}
53+
}
54+
55+
Date thirdLatesDate = null;
56+
int i = 0;
57+
for (Set<Date> sd : sortedDates.values()) {
58+
for (Date d : sd) {
59+
thirdLatesDate = d;
60+
if (i == 2) {
61+
return d;
62+
} else {
63+
i++;
64+
continue;
65+
}
66+
}
67+
}
68+
69+
return thirdLatesDate;
70+
71+
}
72+
}
+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import java.text.ParseException;
2+
3+
import org.junit.Assert;
4+
import org.junit.ComparisonFailure;
5+
import org.junit.Test;
6+
7+
/**
8+
* @author medany
9+
*/
10+
11+
public class ThirdLatestDateTest {
12+
13+
private ThirdLatestDate alg = new ThirdLatestDate();
14+
private Date[] dates = null;
15+
Date actual = null, expected = null;
16+
17+
@Test
18+
public void Test_1() throws ParseException {
19+
20+
dates = getDates(
21+
new String[] { "14-04-2001", "29-12-2061", "21-10-2019", "07-01-1973", "19-07-2014", "11-03-1992" });
22+
23+
actual = alg.solve(dates);
24+
expected = new Date("19-07-2014");
25+
26+
test(expected, actual);
27+
28+
}
29+
30+
@Test
31+
public void Test_2() {
32+
33+
dates = getDates(new String[] { "14-04-2001", "29-12-2061", "21-10-2019", "07-01-1973", "19-07-2014",
34+
"11-03-1992", "21-10-2019" });
35+
36+
actual = alg.solve(dates);
37+
expected = new Date("19-07-2014");
38+
39+
test(expected, actual);
40+
}
41+
42+
@Test
43+
public void Test_3() {
44+
45+
dates = getDates(new String[] { "14-04-2001", "29-12-2061", "29-12-2061", "21-10-2019", "07-01-1973",
46+
"19-07-2014", "19-07-2014", "11-03-1992" });
47+
48+
actual = alg.solve(dates);
49+
expected = new Date("19-07-2014");
50+
51+
test(expected, actual);
52+
}
53+
54+
@Test
55+
public void Test_4() {
56+
57+
dates = getDates(new String[] { "01-10-2015", "01-09-2015", "01-08-2015", "01-07-2015", "01-06-2015" });
58+
59+
actual = alg.solve(dates);
60+
expected = new Date("01-08-2015");
61+
62+
test(expected, actual);
63+
}
64+
65+
@Test
66+
public void Test_5() {
67+
68+
dates = getDates(
69+
new String[] { "25-01-2015", "24-01-2015", "23-01-2015", "22-01-2015", "21-01-2015", "20-01-2015" });
70+
71+
actual = alg.solve(dates);
72+
expected = new Date("23-01-2015");
73+
74+
test(expected, actual);
75+
}
76+
77+
@Test
78+
public void Test_6() {
79+
80+
dates = getDates(new String[] { "01-08-2011", "01-09-2012", "01-10-2015", "01-11-2014", "01-12-2013" });
81+
82+
actual = alg.solve(dates);
83+
expected = new Date("01-12-2013");
84+
85+
test(expected, actual);
86+
}
87+
88+
@Test
89+
public void Test_7() {
90+
91+
dates = getDates(new String[] { "18-01-1998", "19-01-1999", "20-01-2002", "21-01-2001", "22-01-2000" });
92+
93+
actual = alg.solve(dates);
94+
expected = new Date("22-01-2000");
95+
96+
test(expected, actual);
97+
}
98+
99+
private Date[] getDates(String[] datesStrings) {
100+
Date[] dates = new Date[datesStrings.length];
101+
for (int i = 0; i < datesStrings.length; i++) {
102+
dates[i] = new Date(datesStrings[i]);
103+
}
104+
return dates;
105+
}
106+
107+
private void test(Date expected, Date actual) {
108+
try {
109+
Assert.assertEquals(expected.toString(), actual.toString());
110+
} catch (ComparisonFailure failed) {
111+
System.out.println(failed.getStackTrace()[3].getMethodName() + " failed, " + failed.getMessage());
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)