From 8f9bc942e0d13ee0bf1d128649f984d20af4c2ff Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 20 Oct 2022 20:03:11 +0100 Subject: - Added solutions to the week 187. --- .../java/theweeklychallenge/DaysTogether.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java (limited to 'challenge-187/mohammad-anwar/java/theweeklychallenge') diff --git a/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java b/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java new file mode 100644 index 0000000000..36652ac0a8 --- /dev/null +++ b/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java @@ -0,0 +1,90 @@ +package theweeklychallenge; + +/* + +Week 187: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-187 + +Task #1: Days Together + + Two friends, Foo and Bar gone on holidays seperately to the same + city. You are given their schedule i.e. start date and end date. + + To keep the task simple, the date is in the form 'DD-MM' and all + dates belong to the same calendar year i.e. between '01-01' and + '31-12'. + + Also the year is non-leap year and both dates are inclusive. + +Compile and Run: + + mohammad-anwar/java$ javac theweeklychallenge/DaysTogether.java + mohammad-anwar/java$ java theweeklychallenge.DaysTogether + +*/ + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import junit.framework.TestCase; +import static junit.framework.Assert.*; + +public class DaysTogether extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run( + theweeklychallenge.DaysTogether.class + ); + } + + public void testDaysTogether() { + assertEquals(4, daysTogether("12-01","20-01","15-01","18-01")); + assertEquals(0, daysTogether("02-03","12-03","13-03","14-03")); + assertEquals(2, daysTogether("02-03","12-03","11-03","15-03")); + assertEquals(4, daysTogether("30-03","05-04","28-03","02-04")); + } + + public static int daysTogether( + String sd1, String ed1, String sd2, String ed2) { + LocalDate _sd1 = _date(sd1); + LocalDate _ed1 = _date(ed1); + LocalDate _sd2 = _date(sd2); + LocalDate _ed2 = _date(ed2); + + int days = 0; + + if (_ed1.compareTo(_sd2) < 0) { + return days; + } + + if (_sd2.compareTo(_ed1) > 0) { + days = _daysTogether(_ed1, _ed2, null); + } + else if (_sd1.compareTo(_sd2) > 0) { + days = _daysTogether(_sd1, _ed1, _ed2); + } + else if (_ed2.compareTo(_sd2) > 0) { + days = _daysTogether(_sd2, _ed1, _ed2); + } + + return days; + } + + public static LocalDate _date(String dateStr) { + String[] splitDate = dateStr.split("-"); + int day = Integer.parseInt(splitDate[0]); + int month = Integer.parseInt(splitDate[1]); + + return LocalDate.of(2022, month, day); + } + + public static int _daysTogether( + LocalDate from, LocalDate to, LocalDate _to) { + if ((_to != null) && (to.compareTo(_to) > 0)) { + to = _to; + } + + long days = ChronoUnit.DAYS.between(from, to); + return (int)days + 1; + } +} -- cgit From c9d4aa109f05ded505b5d9bbb318809e924acc04 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 20 Oct 2022 20:18:23 +0100 Subject: - Tidied up solutions. --- .../mohammad-anwar/java/theweeklychallenge/DaysTogether.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'challenge-187/mohammad-anwar/java/theweeklychallenge') diff --git a/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java b/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java index 36652ac0a8..0c0d0869fb 100644 --- a/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java +++ b/challenge-187/mohammad-anwar/java/theweeklychallenge/DaysTogether.java @@ -57,10 +57,7 @@ public class DaysTogether extends TestCase { return days; } - if (_sd2.compareTo(_ed1) > 0) { - days = _daysTogether(_ed1, _ed2, null); - } - else if (_sd1.compareTo(_sd2) > 0) { + if (_sd1.compareTo(_sd2) > 0) { days = _daysTogether(_sd1, _ed1, _ed2); } else if (_ed2.compareTo(_sd2) > 0) { @@ -80,7 +77,7 @@ public class DaysTogether extends TestCase { public static int _daysTogether( LocalDate from, LocalDate to, LocalDate _to) { - if ((_to != null) && (to.compareTo(_to) > 0)) { + if (to.compareTo(_to) > 0) { to = _to; } -- cgit