Skip to content

Commit 2d6d30f

Browse files
committed
Add some examples
1 parent d29bd2b commit 2d6d30f

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class BankAccount:
2+
""" Create a new bank account """
3+
def __init__(self, name, hasCommissionDiscount):
4+
self._name = name
5+
self._hasCommissionDiscount = hasCommissionDiscount
6+
self._balance = 0
7+
self._commission_rate = BankAccount._calc_commission_rate(hasCommissionDiscount)
8+
9+
def info(self):
10+
""" Account information """
11+
return {
12+
"name": self._name,
13+
"current_balance": self._balance,
14+
}
15+
16+
def deposit(self, amount):
17+
""" deposit money """
18+
if amount > 0:
19+
self._balance += amount - self._calc_commission_rate(self._hasCommissionDiscount)
20+
else:
21+
raise ValueError("deposit amount must be larger than 0")
22+
23+
def balance(self):
24+
return self._balance
25+
26+
def withdraw(self, amount):
27+
""" withdraw money """
28+
if self._balance >= amount > 0:
29+
self._balance -= (amount + self._calc_commission_rate(self._hasCommissionDiscount))
30+
else:
31+
raise ValueError("Insufficient funds for withdraw")
32+
33+
def can_withdraw(self, amount):
34+
return self._balance >= amount > 0
35+
36+
def transfer_to_other_account(self, amount, other_account):
37+
""" transfer money """
38+
if amount <= 0:
39+
raise ValueError("Transfer amount must be larger than 0")
40+
amount_including_commission = amount + self._commission_rate
41+
if self._balance >= amount_including_commission > 0:
42+
self._balance -= amount_including_commission
43+
other_account._balance += amount
44+
else:
45+
raise ValueError("Insufficient funds for transfer")
46+
47+
@staticmethod
48+
def _calc_commission_rate(hasCommisionDiscount):
49+
""" Get the rate of commission for this account """
50+
if hasCommisionDiscount:
51+
return 2.5
52+
else:
53+
return 9

examples/bulleted_list/parse_bulleted_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ def parse_bullet_list(list_str: str) -> Optional[List[str]]:
77
return []
88
if list_str.strip() == "":
99
return []
10+
list_str = list_str.replace("\n*", "\n-").replace("\n%", "\n-")
1011
items = list_str.split("\n-")
11-
items = [line.strip().strip("- \n\t") for line in items]
12+
items = [line.strip().strip("- \n\t").strip("* \n\t").strip("% \n\t") for line in items]
1213
items = [line for line in items if line != ""]
1314
if len(items) == 0:
1415
return []

examples/bulleted_list/parse_bulleted_list_modified.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ def parse_bullet_list(list_str: str) -> Optional[List[str]]:
77
return []
88
if list_str.strip() == "":
99
return []
10-
list_str = list_str.replace("\n*", "\n-")
10+
list_str = list_str.replace("\n*", "\n-").replace("\n%", "\n-")
1111
items = list_str.split("\n-")
12-
items = [line.strip().strip("- \n\t").strip("* \n\t") for line in items]
12+
items = [line.strip().strip("- \n\t").strip("* \n\t").strip("% \n\t") for line in items]
1313
items = [line for line in items if line != ""]
1414
if len(items) == 0:
1515
return []

0 commit comments

Comments
 (0)