forked from tdwojak/Python2017
labs04 - after review
This commit is contained in:
parent
932a6637e0
commit
2d220e69c6
@ -1,3 +1,19 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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'])
|
@ -1,3 +1,38 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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 )
|
@ -1,3 +1,41 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf-8 -*-
|
# -*- 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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user