2017-12-15 17:13:29 +01:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-07 19:10:20 +01:00
|
|
|
<<<<<<< HEAD
|
2017-12-15 20:41:08 +01:00
|
|
|
'''
|
|
|
|
**cwiczenie 3 (zadanie domowe) **
|
|
|
|
Stwórz klase ``Point``, która bedzie reprezentowac punkt w przestrzeni wielowymiarowej:
|
|
|
|
* Konstruktor ma przyjac tylko 1 parametr: liste wspólrzednych. Wykorzystaj funkcje z pierwszego zadania, zeby sprawdzic, czy lista zawiera wylacznie liczby.
|
|
|
|
* Napisz metode add, która dida dwa punkty (dwa wektory) po wspólrzednych i zwróci obiekt typu ``Punkt``. Zaimplementuj wlasny wyjatek ``DimensionError``, który zostaje wyrzucony, jezeli dodawany punkt ma inny wymiar.
|
|
|
|
* Napisz metode ``to\_string``, która zwróci lancuch znakowy, który w czytelny sposób przedstawi punkt.
|
|
|
|
* Napisz metode __len__, która zwróci liczbe wspólrzednych punktu. Zobacz, czy mozesz teraz wywolac funkcje len na obiekcie typy punkt.
|
|
|
|
* Napisz metode __str__, która bedzie dzialac dokladnie tak samo jak metoda ``to_string``. Wyswietl obiekt typy Point korzystajac z funkcji print.
|
|
|
|
'''
|
2017-12-15 17:13:29 +01:00
|
|
|
|
2017-12-15 20:41:08 +01:00
|
|
|
class DimensionError(Exception):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(self)
|
|
|
|
self.msg="Inny wymiar"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Point():
|
|
|
|
def __init__(self,list):
|
|
|
|
if Point.is_numeric(list)!=len(list):
|
|
|
|
self.points=[]
|
|
|
|
self.isnumeric = False
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.points=list
|
|
|
|
self.isnumeric=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __add__(self, other):
|
|
|
|
new = []
|
|
|
|
if self.isnumeric==False or other.isnumeric==False:
|
|
|
|
return "To nie sa liczby!"
|
|
|
|
elif len(self.points) != len(other.points):
|
|
|
|
return DimensionError()
|
|
|
|
else:
|
|
|
|
for i in range(len(self.points)):
|
|
|
|
new.append(self.points[i]+other.points[i])
|
|
|
|
return new
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
len=0
|
|
|
|
for element in self.points:
|
|
|
|
len+=1
|
|
|
|
return len
|
|
|
|
|
|
|
|
|
|
|
|
def to_string(self):
|
|
|
|
point_tuple = tuple(self.points)
|
|
|
|
return str(point_tuple)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return (self.to_string())
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_numeric(point):
|
|
|
|
value=0
|
|
|
|
for i in point:
|
|
|
|
if isinstance(i, int) or isinstance(i, float):
|
|
|
|
value+=1
|
|
|
|
return value
|
2018-01-07 19:10:20 +01:00
|
|
|
=======
|
|
|
|
|
|
|
|
>>>>>>> 576d5ba943f3ccfb753c717350d0c31010bdab1d
|