Compare commits
3
Commits
d3c2b63b5e
...
ed29b5907c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ed29b5907c | ||
|
|
e420bae5d5 | ||
|
|
1200160f1c |
@@ -0,0 +1,36 @@
|
||||
class WeekDayError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Weeker:
|
||||
__GIORNI = ['lun', 'mar', 'mer', 'gio', 'ven', 'sab', 'dom']
|
||||
|
||||
def __init__(self, day):
|
||||
if day not in Weeker.__GIORNI:
|
||||
raise WeekDayError("Giorno non valido: " + day)
|
||||
self.__day_index = Weeker.__GIORNI.index(day)
|
||||
|
||||
def __str__(self):
|
||||
return Weeker.__GIORNI[self.__day_index].capitalize()
|
||||
|
||||
def add_days(self, n):
|
||||
self.__day_index = (self.__day_index + n) % 7
|
||||
|
||||
def subtract_days(self, n):
|
||||
self.__day_index = (self.__day_index - n) % 7
|
||||
|
||||
|
||||
# --- TEST ---
|
||||
try:
|
||||
weekday = Weeker('lun')
|
||||
print(weekday)
|
||||
|
||||
weekday.add_days(15)
|
||||
print(weekday)
|
||||
|
||||
weekday.subtract_days(23)
|
||||
print(weekday)
|
||||
|
||||
weekday = Weeker('lunedì')
|
||||
except WeekDayError:
|
||||
print("Mi dispiace, non posso soddisfare la sua richiesta.")
|
||||
@@ -0,0 +1,28 @@
|
||||
import math
|
||||
|
||||
|
||||
class Point:
|
||||
def __init__(self, x, y):
|
||||
self.__x = float(x)
|
||||
self.__y = float(y)
|
||||
|
||||
def getx(self):
|
||||
return self.__x
|
||||
|
||||
def gety(self):
|
||||
return self.__y
|
||||
|
||||
def distance_from_xy(self, x, y):
|
||||
return math.hypot(self.__x - x, self.__y - y)
|
||||
|
||||
def distance_from_point(self, point):
|
||||
return self.distance_from_xy(point.getx(), point.gety())
|
||||
|
||||
|
||||
# --- TEST ---
|
||||
point1 = Point(0, 0)
|
||||
point2 = Point(1, 1)
|
||||
|
||||
print(point1.distance_from_point(point2))
|
||||
|
||||
print(point2.distance_from_xy(2, 0))
|
||||
@@ -0,0 +1,35 @@
|
||||
import math
|
||||
|
||||
class Point:
|
||||
|
||||
def __init__(self, x, y):
|
||||
self.__x = float(x)
|
||||
self.__y = float(y)
|
||||
|
||||
def getx(self):
|
||||
return self.__x
|
||||
|
||||
def gety(self):
|
||||
return self.__y
|
||||
|
||||
def distance_from_xy(self, x, y):
|
||||
return math.hypot(self.__x - x, self.__y - y)
|
||||
|
||||
def distance_from_point(self, point):
|
||||
return self.distance_from_xy(point.getx(), point.gety())
|
||||
|
||||
|
||||
class Triangle:
|
||||
|
||||
def __init__(self, vertice1, vertice2, vertice3):
|
||||
self.__vertici = [vertice1, vertice2, vertice3]
|
||||
|
||||
def perimeter(self):
|
||||
v = self.__vertici
|
||||
lato1 = v[0].distance_from_point(v[1])
|
||||
lato2 = v[1].distance_from_point(v[2])
|
||||
lato3 = v[2].distance_from_point(v[0])
|
||||
return lato1 + lato2 + lato3
|
||||
|
||||
triangle = Triangle(Point(0, 0), Point(1, 0), Point(0, 1))
|
||||
print(triangle.perimeter())
|
||||
Reference in New Issue
Block a user