1
0
forked from tdwojak/Python2017
Python2017/labs04/task03.py

50 lines
1.2 KiB
Python
Raw Normal View History

2017-12-03 13:05:05 +01:00
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
2017-12-16 11:02:48 +01:00
class Point:
def __init__(self, coords):
if is_numeric(coords):
self.coords = coords
print(self.coords)
def to_string(self):
print("coords:")
print(self.coords)
def __len__(self):
return len(self.coords)
def __str__(self):
return "coords: [" + ','.join([str(c) for c in self.coords]) + "]"
class DimensionError(Exception):
def __init__(self, message):
super(DimensionError, self).__init__(message)
def add(point1, point2):
if len(point1.coords) != len(point2.coords) or len(point1.coords) == 0 or len(point2.coords) == 0:
raise DimensionError('Wrong dimensions!')
newCoords = []
for dim in zip(point1.coords, point2.coords):
newCoords.append(sum(dim))
return Point(newCoords)
def is_numeric(coords):
for el in coords:
if not isinstance(el, (int, float)):
return False
return True
if __name__ == '__main__':
point1 = Point([1,2,3,4,5])
point2 = Point([1,2,3,4,5])
point = add(point1, point2)
# point.to_string()
print(len(point))
print(point)
2017-12-03 13:05:05 +01:00