forked from tdwojak/Python2017
task03 done
This commit is contained in:
parent
c8afe1950d
commit
b986a45879
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user