Microsoft Online Assessment (OA) - Day of week that is K days later
Given current day as day
of the week and an integer K
, the task is to find the day of the week after K
days.
Example 1:
Input:
day
= “Monday”
K
= 3
Output: Thursday
Example 2:
Input:
day
= “Tuesday”
K
= 101
Output: Friday
Try it yourself
Implementation
1def day_of_week(day: str, k: int) -> str:
2 days = [
3 "Monday",
4 "Tuesday",
5 "Wednesday",
6 "Thursday",
7 "Friday",
8 "Saturday",
9 "Sunday",
10 ]
11 index = 0
12 for i in range(len(days)):
13 if days[i] == day:
14 index = i
15 return days[(index + k) % 7]
16
17if __name__ == "__main__":
18 day = input()
19 k = int(input())
20 res = day_of_week(day, k)
21 print(res)
22
1import java.util.List;
2import java.util.Scanner;
3
4class Solution {
5 public static String dayOfWeek(String day, int k) {
6 List<String> days = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
7 int index = days.indexOf(day);
8 return days.get((index + k) % 7);
9 }
10
11 public static void main(String[] args) {
12 Scanner scanner = new Scanner(System.in);
13 String day = scanner.nextLine();
14 int k = Integer.parseInt(scanner.nextLine());
15 scanner.close();
16 String res = dayOfWeek(day, k);
17 System.out.println(res);
18 }
19}
20
1"use strict";
2
3function dayOfWeek(day, k) {
4 const days = [
5 "Monday",
6 "Tuesday",
7 "Wednesday",
8 "Thursday",
9 "Friday",
10 "Saturday",
11 "Sunday",
12 ];
13 const index = days.indexOf(day);
14 return days[(index + k) % 7];
15}
16
17function* main() {
18 const day = yield;
19 const k = parseInt(yield);
20 const res = dayOfWeek(day, k);
21 console.log(res);
22}
23
24class EOFError extends Error {}
25{
26 const gen = main();
27 const next = (line) => gen.next(line).done && process.exit();
28 let buf = "";
29 next();
30 process.stdin.setEncoding("utf8");
31 process.stdin.on("data", (data) => {
32 const lines = (buf + data).split("\n");
33 buf = lines.pop();
34 lines.forEach(next);
35 });
36 process.stdin.on("end", () => {
37 buf && next(buf);
38 gen.throw(new EOFError());
39 });
40}
41