Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Every Person is Unique by Sillte
class Person:
def __init__(self, *args):
attrs = ("first_name", "last_name", "birth_date", "job", "working_years", "salary", "country", "city")
args = iter(args)
for attr, arg in zip(attrs, args): # the order is important.
setattr(self, attr, arg)
self.gender = next(args, "unknown")
def name(self):
return " ".join(map(lambda a: getattr(self, a) ,("first_name", "last_name")))
def age(self):
day, month, year = map(int, self.birth_date.split("."))
return 2018 - year - 1
def work(self):
gender_dict = {"male":"he ", "female":"she ", "unknown":""}
sentence = "{p}is a {w}".format(p=gender_dict[self.gender], w=self.job).capitalize()
return sentence
def money(self):
value = self.salary * self.working_years * 12
value = "{:,}".format(value).replace(",", " ")
return value
def home(self):
return "Lives in {0.city}, {0.country}".format(self)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
def json_dict(d):
return dict((k, list(v)) for k, v in d.items())
p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600, "Canada", "Vancouver", "male")
p2 = Person("Hanna Rose", "May", "05.12.1995", "designer", 2.2, 2150, "Austria", "Vienna")
assert p1.name() == "John Smith", "Name"
assert p1.age() == 38, "Age"
assert p2.work() == "Is a designer", "Job"
assert p1.money() == "648 000", "Money"
assert p2.home() == "Lives in Vienna, Austria", "Home"
June 8, 2018
Comments: