forked from tdwojak/Python2017
in progress
This commit is contained in:
parent
8d38a7ddb4
commit
aed06ae658
@ -1,23 +1,64 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
class DimensionError(Exception):
|
||||||
|
"""Discreapncies in dimensions. We cannot add points from different dimension"""
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(self)
|
||||||
|
self.msg='Discreapncies in dimensions. We cannot add points from different dimension'
|
||||||
|
|
||||||
|
class IsNumericError(Exception):
|
||||||
|
"""Point contains other chars than numbers"""
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(self)
|
||||||
|
self.msg='Point contains other chars than numbers'
|
||||||
|
|
||||||
|
|
||||||
class Point():
|
class Point():
|
||||||
def __init__(self,list):
|
def __init__(self,list):
|
||||||
self.points=list
|
try:
|
||||||
if len(list)==Point.is_numeric(self.points):
|
if Point.is_numeric(list)!=len(list):
|
||||||
self.isnumeric=True
|
self.points=[]
|
||||||
else:
|
|
||||||
self.isnumeric = False
|
self.isnumeric = False
|
||||||
|
raise IsNumericError()
|
||||||
|
else:
|
||||||
|
self.points=list
|
||||||
|
self.isnumeric=True
|
||||||
|
except IsNumericError as e:
|
||||||
|
print(e.msg)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Point %s, is numeric: %s>' %(self.points,self.isnumeric)
|
return '<Point %s, is numeric: %s>' %(self.points,self.isnumeric)
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
try:
|
||||||
new = []
|
new = []
|
||||||
|
if self.isnumeric==False or other.isnumeric==False:
|
||||||
|
raise IsNumericError()
|
||||||
|
elif len(self.points) != len(other.points):
|
||||||
|
raise DimensionError()
|
||||||
|
else:
|
||||||
for index in range(len(self.points)):
|
for index in range(len(self.points)):
|
||||||
new.append(self.points[index]+other.points[index])
|
new.append(self.points[index]+other.points[index])
|
||||||
|
except IsNumericError as e:
|
||||||
|
print(e.msg)
|
||||||
|
except DimensionError as e:
|
||||||
|
print(e.msg)
|
||||||
return new
|
return new
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
len=0
|
||||||
|
try:
|
||||||
|
if self.isnumeric==False:
|
||||||
|
raise IsNumericError()
|
||||||
|
else:
|
||||||
|
for element in self.points:
|
||||||
|
len+=1
|
||||||
|
except IsNumericError as e:
|
||||||
|
print(e.msg)
|
||||||
|
return len
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def is_numeric(point):
|
def is_numeric(point):
|
||||||
value=0
|
value=0
|
||||||
@ -30,7 +71,9 @@ class Point():
|
|||||||
|
|
||||||
|
|
||||||
def TestFunction():
|
def TestFunction():
|
||||||
punkt1=Point([12,12,2])
|
punkt1=Point(['a',2,2])
|
||||||
punkt2=Point([1,3,7])
|
punkt2=Point([1,7,25])
|
||||||
|
print(len(punkt1))
|
||||||
|
print(len(punkt2))
|
||||||
print(punkt1,punkt2,punkt1+punkt2)
|
print(punkt1,punkt2,punkt1+punkt2)
|
||||||
TestFunction()
|
TestFunction()
|
Loading…
Reference in New Issue
Block a user