From 2d220e69c6875dd597396bef8778039c06bd04fa Mon Sep 17 00:00:00 2001 From: s45157 Date: Sat, 16 Dec 2017 02:44:01 +0100 Subject: [PATCH] labs04 - after review --- labs04/task01.py | 16 ++++++++++++++++ labs04/task02.py | 35 +++++++++++++++++++++++++++++++++++ labs04/task03.py | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/labs04/task01.py b/labs04/task01.py index 88741a4..80b3297 100644 --- a/labs04/task01.py +++ b/labs04/task01.py @@ -1,3 +1,19 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +def is_numeric(xs): + return all(isinstance(x, (int, float)) for x in xs) +assert is_numeric([1,2,3,4,5,6,6]) +assert is_numeric([1.0,2.0,3.0,4.0,5.0,6.0]) +assert is_numeric([1]) +assert is_numeric([1.0]) +assert is_numeric([1.0, 1]) +assert is_numeric([1, 1.0]) +assert is_numeric([]) #zgodnie z ustaleniami z zajęć +assert not is_numeric([1,2,3,'a',4,5,6,6]) +assert not is_numeric([1.0,2.0,3.0,'a',4.0,5.0,6.0]) +assert not is_numeric([1, 'a']) +assert not is_numeric([1.0, 'a']) +assert not is_numeric(['a', 1.0, 1]) +assert not is_numeric([1, 1.0, 'a']) +assert not is_numeric(['a']) \ No newline at end of file diff --git a/labs04/task02.py b/labs04/task02.py index 88741a4..9e52d8f 100644 --- a/labs04/task02.py +++ b/labs04/task02.py @@ -1,3 +1,38 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +class Employee(object): + NEXT_ID = 0 + + def __init__(self, name, surname): + self.name = name + self.surname = surname + self.id = Employee.NEXT_ID + Employee.NEXT_ID = Employee.NEXT_ID + 1 + + def get_id(self): + return self.id + + +class Recruiter(Employee): + def __init__(self, name, surname): + super(Recruiter, self).__init__(name, surname) + self.recruited = [] + + def recruit(self, employee): + self.recruited.append(employee.get_id()) + + +class Programmer(Employee): + def __init__(self, name, surname, recruiter): + super(Programmer, self).__init__(name, surname) + self.recruiter = recruiter.get_id() + + +empl1 = Employee('a', 'a') +empl2 = Employee('b', 'b') +rec1 = Recruiter('c', 'c') +prog1 = Programmer('d', 'd', rec1) +rec1.recruit(prog1) +assert prog1.recruiter == rec1.get_id() +assert any( recruitee_id == prog1.get_id() for recruitee_id in rec1.recruited ) \ No newline at end of file diff --git a/labs04/task03.py b/labs04/task03.py index 88741a4..ed78e08 100644 --- a/labs04/task03.py +++ b/labs04/task03.py @@ -1,3 +1,41 @@ #!/usr/bin/env python2 # -*- coding: utf-8 -*- +class Point(object): + def __init__(self, coords): + from labs04.task01 import is_numeric + assert is_numeric(coords) + self.coords = coords + + def add(self, that): + if len(self.coords) != len(that.coords): + raise DimensionError('Niezgodne wymiary') + else: + pass + return Point(map(lambda(a,b): a+b, zip(self.coords, that.coords))) + + def to_string(self): + return type(self).__name__+'('+((',').join(map(str, self.coords)))+')' + + def len(self): + return len(self.coords) + + def __len__(self): + return len(self.coords) + + def __str__(self): + return self.to_string() + + def __add__(self, other): + return self.add(other) + +class DimensionError(Exception): + def __init__(self, msg): + self.msg = msg + def __str__(self): + return self.msg + +pt1 = Point([1,2,3.0,4.0]) +pt2 = Point([-1, -2.0, -3, -4.0]) +print(pt1 + pt2) +