Compare commits

...
9 Commits
Author SHA1 Message Date
davide fe4883cde8 aggiunge esercizio 48 2026-05-04 18:49:57 +02:00
davide c048527f38 aggiunge esercizio 47 2026-05-04 18:08:56 +02:00
davide 2aeaadfe18 Aggiunge esercizio 46 2026-04-30 15:48:08 +02:00
davide cc8ee1615b Aggiunge esercizio 45 2026-04-29 13:32:32 +02:00
davide 07d1e9dd75 aggiunge esercizio 44 2026-04-29 12:29:58 +02:00
davide 722e008efa aggiunge esercizio 43 2026-04-29 12:25:28 +02:00
davide ed29b5907c aggiunge esercizio 42 2026-04-13 15:59:41 +02:00
davide e420bae5d5 aggiunge esercizio 41 2026-04-13 15:56:49 +02:00
davide 1200160f1c aggiunge esercizio 40 2026-04-13 15:47:19 +02:00
11 changed files with 315 additions and 0 deletions
+36
View File
@@ -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.")
+28
View File
@@ -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))
+35
View File
@@ -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())
+31
View File
@@ -0,0 +1,31 @@
from os import strerror
file_name = "lab43.txt"
# Il dizionario conterra' una coppia chiave/valore per ogni lettera trovata:
# chiave = lettera, valore = quante volte appare nel file.
letters = {}
try:
stream = open(file_name, "rt", encoding="utf-8")
ch = stream.read(1)
while ch != "":
ch = ch.lower()
if "a" <= ch <= "z":
if ch in letters:
letters[ch] += 1
else:
letters[ch] = 1
ch = stream.read(1)
stream.close()
for letter in sorted(letters.keys()):
print(letter, "->", letters[letter])
except IOError as e:
print("Errore di I/O:", strerror(e.errno))
+1
View File
@@ -0,0 +1 @@
aBc
+36
View File
@@ -0,0 +1,36 @@
from os import strerror
file_name = "lab43.txt"
letters = {}
try:
source = open(file_name, "rt", encoding="utf-8")
ch = source.read(1)
while ch != "":
ch = ch.lower()
if "a" <= ch <= "z":
if ch in letters:
letters[ch] += 1
else:
letters[ch] = 1
ch = source.read(1)
source.close()
target = open(file_name + ".hist", "wt", encoding="utf-8")
for letter, counter in sorted(letters.items(), key=lambda item: item[1], reverse=True):
target.write(letter + " -> " + str(counter) + "\n")
target.close()
print("Istogramma salvato in:", file_name + ".hist")
except IOError as e:
print("Errore di I/O:", strerror(e.errno))
+64
View File
@@ -0,0 +1,64 @@
from os import strerror
class StudentsDataException(Exception):
pass
class BadLine(StudentsDataException):
def __init__(self, line_number, line):
message = "Riga " + str(line_number) + ": " + line.rstrip()
super().__init__(message)
class FileEmpty(StudentsDataException):
def __init__(self):
super().__init__("il file esiste, ma e' vuoto")
file_name = input("Inserisci il nome del file del Prof. Jekyll: ")
students = {}
try:
stream = open(file_name, "rt", encoding="utf-8")
lines = stream.readlines()
stream.close()
if len(lines) == 0:
raise FileEmpty()
for line_number, line in enumerate(lines, 1):
parts = line.split()
# Ogni riga deve avere esattamente: nome, cognome, punti.
if len(parts) != 3:
raise BadLine(line_number, line)
first_name = parts[0]
last_name = parts[1]
try:
# Accettiamo sia il punto sia la virgola come separatore decimale.
points = float(parts[2].replace(",", "."))
except ValueError:
raise BadLine(line_number, line)
student = first_name + " " + last_name
if student in students:
students[student] += points
else:
students[student] = points
if len(students) == 0:
raise FileEmpty()
for student in sorted(students.keys()):
print(student, students[student])
except FileEmpty as e:
print("Errore:", e)
except BadLine as e:
print("Errore nei dati di input:", e)
except IOError as e:
print("Errore di I/O:", strerror(e.errno))
+5
View File
@@ -0,0 +1,5 @@
John Smith 5
Anna Bolena 4,5
John Smith 2
Anna Bolena 11
Andrew Cox 1,5
+22
View File
@@ -0,0 +1,22 @@
import os
def find(path, dir):
if not os.path.isdir(path):
return
for name in os.listdir(path):
full_path = os.path.join(path, name)
if os.path.isdir(full_path):
if name == dir:
print(os.path.abspath(full_path))
find(full_path, dir)
if __name__ == "__main__":
start_path = input("Percorso di partenza: ")
directory_name = input("Directory da cercare: ")
find(start_path, directory_name)
+28
View File
@@ -0,0 +1,28 @@
from datetime import datetime
d = datetime(year=2020, month=11, day=4, hour=14, minute=53, second=0)
# Direttive usate con strftime:
# %Y anno con 4 cifre -> 2020
# %y anno con 2 cifre -> 20
# %m mese numerico -> 11
# %B nome mese completo -> November
# %b nome mese abbreviato -> Nov
# %d giorno del mese -> 04
# %H ora formato 24h -> 14
# %M minuti -> 53
# %S secondi -> 00
# %p AM/PM -> PM
# %a giorno abbreviato -> Wed
# %A giorno completo -> Wednesday
# %w giorno settimana -> 3
# %j giorno dell'anno -> 309
# %W numero settimana -> 44
print(d.strftime("%Y/%m/%d %H:%M:%S"))
print(d.strftime("%y/%B/%d %H:%M:%S %p"))
print(d.strftime("%a, %Y %b %d"))
print(d.strftime("%A, %Y %B %d"))
print(d.strftime("Weekday: %w"))
print(d.strftime("Day of the year: %j"))
print(d.strftime("Week number of the year: %W"))
+29
View File
@@ -0,0 +1,29 @@
from calendar import Calendar
class MyCalendar(Calendar):
def count_weekday_in_year(self, year, weekday):
count = 0
for month in range(1, 13):
month_calendar = self.monthdays2calendar(year, month)
for week in month_calendar:
for day_number, day_weekday in week:
if day_number != 0 and day_weekday == weekday:
count += 1
return count
my_calendar = MyCalendar()
result = my_calendar.count_weekday_in_year(2000, 6)
print(result)