Skip to content

Instantly share code, notes, and snippets.

@Deviad
Last active January 27, 2023 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deviad/d5a9d7b19ac0a70b6cf07a0422c77acd to your computer and use it in GitHub Desktop.
Save Deviad/d5a9d7b19ac0a70b6cf07a0422c77acd to your computer and use it in GitHub Desktop.
Interviews in Switzerland
package com.adobe.prep;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
public class GetYourGuide {
public static void main(String[] args) {
String s = "photo.jpg, Warsaw, 2013-09-05 14:08:15\n" +
"john.png, London, 2015-06-20 15:13:22\n" +
"myFriends.png, Warsaw, 2013-09-05 14:07:13\n" +
"Eiffel.jpg, Paris, 2015-07-23 08:03:02\n" +
"pisatower.jpg, Paris, 2015-07-22 23:59:59\n" +
"BOB.jpg, London, 2015-08-05 00:02:03\n" +
"notredame.png, Paris, 2015-09-01 12:00:00\n" +
"me.jpg, Warsaw, 2013-09-06 15:40:22\n" +
"a.png, Warsaw, 2016-02-13 13:33:50\n" +
"b.jpg, Warsaw, 2016-01-02 15:12:22\n" +
"c.jpg, Warsaw, 2016-01-02 14:34:30\n" +
"d.jpg, Warsaw, 2016-01-02 15:15:01\n" +
"e.png, Warsaw, 2016-01-02 09:49:09\n" +
"f.png, Warsaw, 2016-01-02 10:55:32\n" +
"g.jpg, Warsaw, 2016-02-29 22:13:11";
System.out.println(solve(s));
}
public static String solve(String s) {
String[] lines = s.split("\n");
HashMap<String, String> numberedEntries = new HashMap<>();
Set<String> cities = new HashSet<>();
for (String l : lines) {
String[] lineComponents = l.split(", ");
String city = lineComponents[1];
cities.add(city);
}
for (String city : cities) {
List<String> sorted = Arrays.stream(lines).filter(l -> l.contains(city))
.sorted((s1, s2) -> {
String[] str = s1.split(", ");
String[] str2 = s2.split(", ");
String date1 = str[2];
String date2 = str2[2];
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return LocalDateTime.parse(date1, formatter).
compareTo(LocalDateTime.parse(date2, formatter));
}).collect(Collectors.toList());
int counter = 1;
long count = String.valueOf(sorted.size()).length();
String c = "%0" + count + "d";
for (String str : sorted) {
numberedEntries.put(str, String.format(c, counter++));
}
}
ArrayList<String> result = new ArrayList<>();
for (String line : lines) {
String[] lineComponents = line.split(", ");
String fileName = lineComponents[0];
String extension = fileName.split("\\.")[1];
String city = lineComponents[1];
String newFileName = city + numberedEntries.get(line) + "." + extension;
result.add(newFileName);
}
return String.join("\n", result);
}
}
package sonarsource.application;
import sonarsource.domain.CommandType;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class InputService {
List<String> input;
HashMap<LocalDate, Long> state = new HashMap<>();
ThreadLocal<Boolean> processed = ThreadLocal.withInitial(() -> false);
InputService() {
this.input = initInput();
}
private List<String> initInput() {
return Stream.of(
"INCOME 2020/01/01 gift 500",
"PRINT YEAR 2021",
"PRINT YEAR 2020",
"EXPENSE 2020/10/12 coffee 5",
"EXPENSE 2020/10/11 lunch 25",
"PRINT YEAR 2020",
"INCOME 2020/10/25 stock-dividend 40",
"PRINT MONTH 2020/11")
.collect(Collectors.toList());
}
private void leftFold(Callable<InputService>... callables) {
Arrays.stream(callables).forEach(r -> {
handleCallable(r);
});
}
private void handleCallable(Callable<InputService> r) {
try {
r.call();
} catch (Throwable t) {
throw new RuntimeException();
}
}
private void doStuff() {
for (String str : input) {
leftFold(() -> handleIsExpense(str),
() -> handleIsIncome(str),
() -> handlePrintDay(str),
() -> handlePrintMonth(str),
() -> handlePrintYear(str));
}
if (Boolean.FALSE.equals(processed.get())) {
throw new RuntimeException();
}
}
private InputService handlePrintDay(String x) {
String[] s = getTokens(x);
String command = getPrintCase(s);
if (!isPrintDay(command)) {
return this;
}
processed.set(true);
String dateAsString = getDateAsString(s);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate parsedDate = LocalDate.parse(dateAsString, formatter);
long count = 0;
for (Map.Entry<LocalDate, Long> entry : state.entrySet()) {
if (entry.getKey().getYear() == parsedDate.getYear() &&
entry.getKey().getMonth() == parsedDate.getMonth() &&
entry.getKey().getDayOfMonth() == parsedDate.getDayOfMonth()) {
count = entry.getValue() + count;
}
}
System.out.printf(" balance for day %s is %s%n", dateAsString, count);
return this;
}
private InputService handlePrintYear(String x) {
String[] s = getTokens(x);
if (!isPrintYear(getPrintCase(s))) {
return this;
}
processed.set(true);
String dateAsString = getDateAsString(s);
LocalDate parsedDate = LocalDate.of(Integer.parseInt(dateAsString.split("/")[0]), 1, 1);
long count = 0;
for (Map.Entry<LocalDate, Long> entry : state.entrySet()) {
if (entry.getKey().getYear() == parsedDate.getYear()) {
count = entry.getValue() + count;
}
}
System.out.printf(" balance for year %s is %s%n", dateAsString, count);
return this;
}
private String getPrintCase(String[] s) {
return s[0] + " " + s[1];
}
private InputService handlePrintMonth(String x) {
String[] s = getTokens(x);
if (!isPrintMonth(getPrintCase(s))) {
return this;
}
processed.set(true);
String printCommand = getPrintCase(s);
if (isPrintMonth(printCommand)) {
String dateAsString = getDateAsString(s);
LocalDate parsedDate = LocalDate.of(
Integer.parseInt(dateAsString.split("/")[0]),
Integer.parseInt(dateAsString.split("/")[1]), 1);
long count = state.entrySet().stream()
.filter(entry -> entry.getKey().getYear() == parsedDate.getYear() && entry.getKey().getMonth() == parsedDate.getMonth())
.map(Map.Entry::getValue)
.reduce(Long::sum).orElse(0L);
System.out.printf(" balance for month %s is %s%n", dateAsString, count);
}
return this;
}
private String getDateAsString(String[] s) {
return s[2];
}
private String getCommand(String[] s) {
return s[0];
}
private String[] getTokens(String x) {
return x.split(" ");
}
private InputService handleIsIncome(String x) {
String[] s = getTokens(x);
String command = getCommand(s);
if (!isIncome(command)) {
return this;
}
processed.set(true);
String dateAsString = s[1];
Long amount = getAmount(s);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate parsedDate = LocalDate.parse(dateAsString, formatter);
state.put(parsedDate, state.getOrDefault(parsedDate, 0L) + amount);
return this;
}
private Long getAmount(String[] s) {
return Long.valueOf(s[3]);
}
private InputService handleIsExpense(String x) {
String[] s = getTokens(x);
String command = getCommand(s);
if (!isExpense(command)) {
return this;
}
processed.set(true);
String dateAsString = s[1];
Long amount = getAmount(s);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate parsedDate = LocalDate.parse(dateAsString, formatter);
state.put(parsedDate, state.getOrDefault(parsedDate, 0L) - amount);
return this;
}
public static void main(String[] args) {
PrintStream oldSystemOut = System.out;
ByteArrayOutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
new InputService().doStuff();
System.setOut(oldSystemOut);
assert out.toString().contains("balance for year 2021 is 0");
assert out.toString().contains("balance for year 2020 is 500");
assert out.toString().contains("balance for year 2020 is 470");
assert out.toString().contains("balance for month 2020/11 is 0");
}
private boolean isPrintYear(String printCommand) {
return CommandType.getCommandTypeFromString(printCommand) == CommandType.PRINT_YEAR;
}
private boolean isIncome(String command) {
return CommandType.valueOf(command) == CommandType.INCOME;
}
private boolean isPrintDay(String command) {
return CommandType.getCommandTypeFromString(command) == CommandType.PRINT_DAY;
}
private boolean isPrintMonth(String command) {
return CommandType.getCommandTypeFromString(command) == CommandType.PRINT_MONTH;
}
private boolean isExpense(String command) {
return CommandType.valueOf(command) == CommandType.EXPENSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment