Last active
June 12, 2020 14:44
-
-
Save mengwong/d885afe274c2b54ddd962b2df1339f2b to your computer and use it in GitHub Desktop.
tax Credit A and B, Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/opt/local/bin/python | |
# https://twitter.com/mengwong/status/1271434036976603136 | |
from abc import ABC, abstractmethod | |
class Person: | |
def __init__(self, children=[]): | |
self.children = children | |
class TaxCredit(ABC): | |
@abstractmethod | |
def rate(self, person): | |
pass | |
class TaxCreditB: | |
def rate(self, person): | |
return 100 | |
class TaxCreditA(TaxCreditB): | |
def rate(self, person): | |
return super().rate(person) / 2 if len(person.children) == 2 else super().rate(person) | |
alice = Person() | |
bob = Person() | |
carol = Person(children = [alice, bob]) | |
taxcB = TaxCreditB() | |
print ("tax credit B: alice:", taxcB.rate(alice)) | |
print ("tax credit B: bob:", taxcB.rate(bob)) | |
print ("tax credit B: carol:", taxcB.rate(carol)) | |
taxcA = TaxCreditA() | |
print ("tax credit A: alice:", taxcA.rate(alice)) | |
print ("tax credit A: bob:", taxcA.rate(bob)) | |
print ("tax credit A: carol:", taxcA.rate(carol)) | |
# 20200612-21:47:23 mengwong@venice4:~/tmp/python/taxc% python taxc.py | |
# tax credit B: alice: 100 | |
# tax credit B: bob: 100 | |
# tax credit B: carol: 100 | |
# tax credit A: alice: 100 | |
# tax credit A: bob: 100 | |
# tax credit A: carol: 50.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment