xonecas.io
387
Pe

python-exercices / commit 6b5c453

Add ex_17

The solution on the prompt, was not to my liking, as it depends on
feeding each pair of elements of the transaction log, for each
iteration.  My solution simply accepts the whole log and parses it.

xonecas2020-08-24 6b5c453c4aeddf8e13b27dc67aa592efc856e1fa parent 4aca21e

1 file changed

level_two/ex_17.py+35−0
@@ -0,0 +1,35 @@
1+#!/usr/bin/env python3
2+
3+# Question 17
4+# Level 2
5+
6+# Question: Write a program that computes the net amount of a bank account based a transaction
7+# log from console input. The transaction log format is shown as following: D 100 W 200
8+
9+# D means deposit while W means withdrawal. Suppose the following input is supplied to the
10+# program: D 300 D 300 W 200 D 100 Then, the output should be: 500
11+
12+# Hints: In case of input data being supplied to the question,
13+# it should be assumed to be a console input.
14+
15+
16+total_deposit = 0
17+total_withdrawal = 0
18+
19+transactions = input('Enter the transaction log:\n').split()
20+
21+# We can assume the even indexes: 0, 2, 4, etc are the type of transaction. And that the idx + 1
22+# is the value of the transaction.
23+pointer = 0
24+while pointer < len(transactions):
25+ transaction_type = transactions[pointer]
26+ transaction_value = int(transactions[pointer + 1])
27+
28+ if transaction_type == 'W':
29+ total_withdrawal = total_withdrawal + transaction_value
30+ elif transaction_type == 'D':
31+ total_deposit = total_deposit + transaction_value
32+
33+ pointer = pointer + 2 # 2 because we want to jump from even to even number.
34+
35+print(total_deposit - total_withdrawal)