Write an account class. An account object has a name, a balance and a refusal count as fields, and its balance changes only through deposit, withdraw and transfer methods. The first line gives the number of accounts n, the next n lines give "name starting_balance", the next line gives the number of commands q, and the next q lines give one command each. There are three commands: "deposit name x" puts x into that account. "withdraw name x" takes x out of that account. "transfer from_name to_name x" takes x out of the sending account and puts it into the receiving account. If the amount x is greater than the balance, that withdrawal or transfer is refused: no balance changes, and only the refusal count of the account money was to come out of (the sender, for a transfer) goes up by 1. If x equals the balance it is not refused (the balance becomes 0). After processing all commands in order, print "name balance refusals" for each account, one per line, in the order the accounts appear in the input.
Input
1 ≤ n ≤ 1000, 1 ≤ q ≤ 5000. Names are 1 to 10 lowercase English letters or digits and are distinct. 0 ≤ starting_balance ≤ 1,000,000,000, 1 ≤ x ≤ 1,000,000,000 (integers). Every name in a command is an existing account, and the two names of a transfer differ
Output
n lines, each with name, balance and refusal count separated by one space (a balance can grow to about 5 trillion)
Example 1 · stdin
3
mina 100
jun 50
seo 0
5
withdraw mina 100
transfer jun seo 30
withdraw jun 21
deposit mina 7
transfer seo mina 31
stdout
mina 7 0
jun 20 1
seo 30 1
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 6 languages
This problem takes only the languages with classes: Python, JavaScript, TypeScript, Java, C# and C++ (C has no classes, and Craykon cannot define classes yet). The first trap is the refusal boundary: an amount equal to the balance can be taken out, so refuse only when amount > balance; writing >= is wrong. The second is the balance size: putting in 1 billion 5000 times into one account gives about 5 trillion, so Java int, C# int and C++ int (up to about 2.1 billion) silently overflow. Use long in Java and C# and long long in C++; Python int has no limit and JS/TS number is exact up to 2^53, so they are fine. The third is whether taking an object out makes a copy. Python, JS, TS and Java objects and C# class instances are handled by reference, so changing an account you got from a map changes the original. But in C++ Account acc = accounts[i]; copies the whole object, so take it as a reference Account& acc; and in C#, making it a struct instead of a class gives a value type, so every lookup hands back a copy and the deposit disappears. Having transfer call deposit only when withdraw succeeds keeps the refusal rule in one place.