Senior
Input data is provided OperationId TransactionId ISIN RcvID SndId Quantity OperationId - int, operation ID TransactionId - int, transaction ID ISIN - String, instrument ID RcvID - int, recipient client ID, -1 if withdrawal SndId - int, sender client ID, -1 if deposit Quantity - double, instrument quantity OperDate - String, date in [phone] format Input lines, for example 1 1 RUB [phone] USD [phone] USD [phone] RUB [phone] Calculate the quantity of each instrument for each client on the latest date [phone] USD [phone] USD [phone] RUB 100
sobes.tech AI
Answer from AI
To solve the task of counting the number of each instrument for each client on the last date, you need to:
- Read all operations, parse the fields, including the date.
- Filter operations by the maximum date (the latest).
- For each operation, update the client's balance:
- If RcvID != -1, increase the client's instrument count by Quantity.
- If SndId != -1, decrease the client's instrument count by Quantity.
Here's an example code in Java using a Map to store balances:
import java.util.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
class Operation {
int operationId;
int transactionId;
String isin;
int rcvId;
int sndId;
double quantity;
LocalDate operDate;
public static Operation parse(String line) {
String[] parts = line.split(" ");
Operation op = new Operation();
op.operationId = Integer.parseInt(parts[0]);
op.transactionId = Integer.parseInt(parts[1]);
op.isin = parts[2];
op.rcvId = Integer.parseInt(parts[3]);
op.sndId = Integer.parseInt(parts[4]);
op.quantity = Double.parseDouble(parts[5]);
op.operDate = LocalDate.parse(parts[6], DateTimeFormatter.ISO_DATE); // date format should be ISO
return op;
}
}
public class BalanceCalculator {
public static void main(String[] args) {
List<Operation> operations = new ArrayList<>();
// Read input data and parse into operations
// Find the maximum date
LocalDate maxDate = operations.stream()
.map(op -> op.operDate)
.max(LocalDate::compareTo)
.orElse(LocalDate.MIN);
// Filter operations by the maximum date
List<Operation> lastDateOps = new ArrayList<>();
for (Operation op : operations) {
if (op.operDate.equals(maxDate)) {
lastDateOps.add(op);
}
}
// Map<Client, Map<Instrument, Quantity>>
Map<Integer, Map<String, Double>> balances = new HashMap<>();
for (Operation op : lastDateOps) {
if (op.rcvId != -1) {
balances.computeIfAbsent(op.rcvId, k -> new HashMap<>())
.merge(op.isin, op.quantity, Double::sum);
}
if (op.sndId != -1) {
balances.computeIfAbsent(op.sndId, k -> new HashMap<>())
.merge(op.isin, -op.quantity, Double::sum);
}
}
// Output balances
for (var clientEntry : balances.entrySet()) {
int clientId = clientEntry.getKey();
for (var isinEntry : clientEntry.getValue().entrySet()) {
System.out.printf("Client %d has %.2f of %s\n", clientId, isinEntry.getValue(), isinEntry.getKey());
}
}
}
}
It's important that the date is in a parseable format (e.g., ISO 8601). If the format differs, adjust the date parser accordingly.
This way, we correctly account for input (sndId = -1) and output (rcvId = -1), updating clients' balances on the last date.