From b986a4587964981705fcfca0544eaeab2fc44869 Mon Sep 17 00:00:00 2001 From: s45147 Date: Sat, 9 Dec 2017 23:14:23 +0100 Subject: [PATCH] task03 done --- labs04/task03.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/labs04/task03.py b/labs04/task03.py index 88741a4..7bdac90 100644 --- a/labs04/task03.py +++ b/labs04/task03.py @@ -1,3 +1,74 @@ #!/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) +