Python2017/labs04/task03.py
2017-12-09 23:14:23 +01:00

75 lines
1.5 KiB
Python

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
def is_numeric(x):
ile = len(x)
licznik = 0
for i in x:
if isinstance(i, (int, float)):
licznik += 1
if ile == licznik:
return True
else:
return False
def add_coordinates(c1, c2):
new = []
if len(c1) == len(c2):
for i in range(0, len(c1)):
new.append(c1[i]+c2[i])
return new
class DimensionError(Exception):
def __init__(self, text):
self.text = text
def __str__(self):
return self.text
class Point:
def __init__(self, coordinates):
if not is_numeric(coordinates):
raise Exception('Lista zawiera wartosci nieliczbowe')
self.coordinates = coordinates
self.coordinates_to_string = 'Koordynaty punktu: ['
for c in coordinates:
self.coordinates_to_string += str(c) + ', '
self.coordinates_to_string = self.coordinates_to_string[:-2]
self.coordinates_to_string += ']'
def to_string(self):
print(self.coordinates_to_string)
def __str__(self):
return self.coordinates_to_string
def __add__(self, other):
if len(self.coordinates) != len(other.coordinates):
raise DimensionError('Punkty maja rozne wymiary')
return Point(add_coordinates(self.coordinates, other.coordinates))
def __len__(self):
return len(self.coordinates)
a = Point([1, 1, 5])
b = Point([2, 4, 4])
print(a)
b.to_string()
print(len(b))
c = a + b
print(c)