Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Read 2 lines and parse dates.
Substract actual year, month, day and expected year, month and day.
Compare values and calculate fee:
– if year difference
is greater than 0
theb return 10000
– if year difference
is equal to 0
and month difference
greater than 0
then return 45 * month difference
– if if year difference
is equal to 0
and month difference
equals to 0
and day diff
greater than 0
then return 15 * day diff
– otherwise return 0
.
I created solution in 4 languages:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
object LibraryFine extends App { val consoleReader = io.Source.stdin.bufferedReader() val actualDate = consoleReader.readLine().split(" ").map(_.toInt) val expectedDate = consoleReader.readLine().split(" ").map(_.toInt) val yearDiff = actualDate(2) - expectedDate(2) val monthDiff = actualDate(1) - expectedDate(1) val dayDiff = actualDate(0) - expectedDate(0) def fee(year: Int, month: Int, day: Int): Int = { if(year > 0) return 10000 if(year == 0 && month > 0) return month * 500 if(year == 0 && month == 0 && day > 0) return day * 15 return 0 } println(fee(yearDiff, monthDiff, dayDiff)) } |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; public class LibraryFine { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int actualDay = stdin.nextInt(); int actualMonth = stdin.nextInt(); int actualYear = stdin.nextInt(); int expectedDay = stdin.nextInt(); int expectedMonth = stdin.nextInt(); int expectedYear = stdin.nextInt(); int yearDiff = actualYear - expectedYear; int monthDiff = actualMonth - expectedMonth; int dayDiff = actualDay - expectedDay; System.out.println(fee(yearDiff, monthDiff, dayDiff)); } private static int fee(int year, int month, int day) { if(year > 0) return 10000; if(year == 0 && month > 0) return month * 500; if(year == 0 && month == 0 && day > 0) return day * 15; return 0; } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function processData(input) { var lines = input.split("\n"); var actualDate = lines[0].split(" "); var expectedDate = lines[1].split(" "); var yearDiff = parseInt(actualDate[2]) - parseInt(expectedDate[2]); var monthDiff = parseInt(actualDate[1]) - parseInt(expectedDate[1]); var dayDiff = parseInt(actualDate[0]) - parseInt(expectedDate[0]); console.log(fee(yearDiff, monthDiff, dayDiff)); } function fee(year, month, day) { if(year > 0) return 10000; if(year == 0 && month > 0) return month * 500; if(year == 0 && month == 0 && day > 0) return day * 15; return 0; } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
(actual_day, actual_month, actual_year) = gets.to_s.split(" ").map { |s| s.to_i } (expected_day, expected_month, expected_year) = gets.to_s.split(" ").map { |s| s.to_i } year_diff = actual_year - expected_year month_diff = actual_month - expected_month day_diff = actual_day - expected_day def fee(year, month, day) if year > 0 return 10000 end if year == 0 && month > 0 return month * 500 end if year == 0 && month == 0 && day > 0 return day * 15 end return 0 end puts fee(year_diff, month_diff, day_diff) |