Compare commits
34
Commits
e7d9cddf4a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe4883cde8 | ||
|
|
c048527f38 | ||
|
|
2aeaadfe18 | ||
|
|
cc8ee1615b | ||
|
|
07d1e9dd75 | ||
|
|
722e008efa | ||
|
|
ed29b5907c | ||
|
|
e420bae5d5 | ||
|
|
1200160f1c | ||
|
|
ca2901f4e3 | ||
|
|
d5a61c15b6 | ||
|
|
411696fc15 | ||
|
|
8a3b5fb43b | ||
|
|
f56ffb7776 | ||
|
|
1a2c044fb9 | ||
|
|
ec89c94373 | ||
|
|
934f3096e7 | ||
|
|
510d65e2ba | ||
|
|
23ee9ea2bd | ||
|
|
2b0f8a20d0 | ||
|
|
54f0947b5e | ||
|
|
b838d9bb42 | ||
|
|
d719078b72 | ||
|
|
752b04b4fe | ||
|
|
f35db81e06 | ||
|
|
722a209308 | ||
|
|
f73731350e | ||
|
|
73bcfd2e0e | ||
|
|
8db7d11975 | ||
|
|
afcbed5d16 | ||
|
|
a9a5c4b7cb | ||
|
|
80e5d42c7a | ||
|
|
3e220720fa | ||
|
|
f05f115ccb |
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,17 @@
|
||||
user_word = input("Inserisci una parola: ")
|
||||
user_word = user_word.upper()
|
||||
|
||||
for letter in user_word:
|
||||
if letter == "A":
|
||||
continue
|
||||
elif letter == "E":
|
||||
continue
|
||||
elif letter == "I":
|
||||
continue
|
||||
elif letter == "O":
|
||||
continue
|
||||
elif letter == "U":
|
||||
continue
|
||||
else:
|
||||
print(letter)
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
word_without_vowels =""
|
||||
|
||||
parola_utente = input("Inserisci una parola: ")
|
||||
parola_utente = parola_utente.upper()
|
||||
|
||||
for letter in parola_utente:
|
||||
if letter == "A":
|
||||
continue
|
||||
elif letter == "E":
|
||||
continue
|
||||
elif letter == "I":
|
||||
continue
|
||||
elif letter == "O":
|
||||
continue
|
||||
elif letter == "U":
|
||||
continue
|
||||
|
||||
word_without_vowels += letter
|
||||
|
||||
print(word_without_vowels)
|
||||
@@ -0,0 +1,10 @@
|
||||
blocchi = int(input("Quanti blocchi hai? "))
|
||||
|
||||
strati = 0
|
||||
blocchi_usati = 0
|
||||
|
||||
while blocchi_usati + strati + 1 <= blocchi:
|
||||
strati += 1
|
||||
blocchi_usati += strati
|
||||
|
||||
print("L'altezza della piramide è", strati, "usando", blocchi_usati, "blocchi")
|
||||
@@ -0,0 +1,14 @@
|
||||
c0 = int(input("Inserire un numero intero positivo: "))
|
||||
passi=0
|
||||
|
||||
while c0 > 1:
|
||||
|
||||
if c0 % 2 == 0:
|
||||
c0 /= 2
|
||||
|
||||
else:
|
||||
c0 = c0*3+1
|
||||
|
||||
passi += 1
|
||||
|
||||
print("passi", passi)
|
||||
@@ -0,0 +1,15 @@
|
||||
hat_list = [1, 2, 3, 4, 5]
|
||||
|
||||
# passo 1
|
||||
|
||||
hat_list[2] = int(input("Inserisci il nuovo numero centrale: "))
|
||||
|
||||
# passo 2
|
||||
|
||||
del hat_list[-1]
|
||||
|
||||
# passo 3
|
||||
|
||||
print("Lunghezza: ",len(hat_list))
|
||||
|
||||
print(hat_list)
|
||||
@@ -0,0 +1,36 @@
|
||||
# passo 1
|
||||
|
||||
beatles = []
|
||||
|
||||
print("Passo 1:", beatles)
|
||||
|
||||
# passo 2
|
||||
|
||||
beatles.append("John")
|
||||
beatles.append("Lennon")
|
||||
beatles.append("Paul")
|
||||
beatles.append("McCartney")
|
||||
beatles.append("Geaorge Harrison")
|
||||
|
||||
print("passo 2:", beatles)
|
||||
|
||||
# passo 3
|
||||
|
||||
for i in range(2):
|
||||
j = input("Inserici un altro membro: ")
|
||||
beatles.append(j)
|
||||
|
||||
print("Passo 3", beatles)
|
||||
|
||||
# passo 4
|
||||
|
||||
for i in range(2):
|
||||
del beatles[-1]
|
||||
|
||||
print("Passo 4", beatles)
|
||||
|
||||
# passo 5
|
||||
|
||||
beatles.insert(0, "Ringo Starr")
|
||||
|
||||
print("Passo 5", beatles)
|
||||
@@ -0,0 +1,12 @@
|
||||
my_list = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
|
||||
|
||||
unique_list = []
|
||||
|
||||
for number in my_list:
|
||||
if number not in unique_list:
|
||||
unique_list.append(number)
|
||||
|
||||
my_list = unique_list
|
||||
|
||||
print("The list with unique elements only:")
|
||||
print(my_list)
|
||||
@@ -0,0 +1,19 @@
|
||||
def is_year_leap(year):
|
||||
if year % 4 == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
test_data = [1901, 2000, 2016, 1987]
|
||||
|
||||
test_result = [False, True, True, False]
|
||||
|
||||
for i in range(len(test_data)):
|
||||
yr = test_data[i]
|
||||
print(yr, "->", end="")
|
||||
resutl = is_year_leap(yr)
|
||||
|
||||
if resutl == test_result[i]:
|
||||
print("OK")
|
||||
else:
|
||||
print("Failed")
|
||||
@@ -0,0 +1,31 @@
|
||||
# from lab23 import is_year_leap
|
||||
|
||||
def is_year_leap(year):
|
||||
if year % 4 == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def days_in_month(year, month):
|
||||
|
||||
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
|
||||
if is_year_leap(year) == True:
|
||||
days[1] = 29
|
||||
|
||||
return days[month - 1]
|
||||
|
||||
test_years = [1901, 2000, 2016, 1987]
|
||||
test_months = [2, 2, 1, 11]
|
||||
test_results = [28, 29, 31, 30]
|
||||
|
||||
for i in range(len(test_years)):
|
||||
yr = test_years[i]
|
||||
mo = test_months[i]
|
||||
|
||||
print(yr, mo, "->", end="")
|
||||
result = days_in_month(yr, mo)
|
||||
if result == test_results[i]:
|
||||
print("OK")
|
||||
else:
|
||||
print("Failed")
|
||||
@@ -0,0 +1,35 @@
|
||||
def is_year_leap(year):
|
||||
if year % 4 == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def days_in_month(year, month):
|
||||
|
||||
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
|
||||
|
||||
if is_year_leap(year) == True:
|
||||
days[1] = 29
|
||||
|
||||
return days[month - 1]
|
||||
|
||||
def day_of_year(year, month, day):
|
||||
if month < 1 or month > 12:
|
||||
return None
|
||||
elif day < 1 or day > 31:
|
||||
return None
|
||||
elif day > 29 and is_year_leap(year) == True and month == 2:
|
||||
return None
|
||||
elif day > 28 and is_year_leap(year) == False and month == 2:
|
||||
return None
|
||||
|
||||
day_ = day
|
||||
|
||||
for i in range(1,month):
|
||||
mo = days_in_month(year,i)
|
||||
day_ += mo
|
||||
|
||||
return day_
|
||||
|
||||
print(day_of_year(2001,2,29))
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
def is_prime(num):
|
||||
div = []
|
||||
|
||||
if num is float:
|
||||
return None
|
||||
|
||||
for i in range(1,num+1):
|
||||
if num % i == 0:
|
||||
div.append(i)
|
||||
|
||||
if len(div) == 2:
|
||||
return num
|
||||
|
||||
for i in range(1,20):
|
||||
if is_prime(i+1):
|
||||
print(i+1,end=" ")
|
||||
|
||||
print()
|
||||
@@ -0,0 +1,41 @@
|
||||
# conversioni
|
||||
|
||||
# 1 miglio = 1609.344 metri
|
||||
# 1 gallone = 3.785411784 litri
|
||||
|
||||
|
||||
def liters_100km_to_miles_gallon(litres):
|
||||
"""converte l/100km in miglia per gallone"""
|
||||
if litres < 0:
|
||||
return None
|
||||
|
||||
gallons_k = litres / 3.785411784 # per 100 km
|
||||
|
||||
gallons_m = gallons_k / 100 # per 1 km
|
||||
|
||||
gallons_m = gallons_m * 1.609344 # per 1 mi
|
||||
|
||||
gallons = gallons_m ** (-1)
|
||||
|
||||
return gallons
|
||||
|
||||
def miles_gallon_to_liters_100km(miles):
|
||||
"""converte miglia per gallone in l/100km"""
|
||||
|
||||
chilometers = miles * 1.609344 # con un gallone
|
||||
|
||||
chilometers_l = chilometers / 3.785411784 # con un litro
|
||||
|
||||
chilometers_100 = chilometers_l / 100 # 100 km per L
|
||||
|
||||
liters_100 = chilometers_100 ** (-1)
|
||||
|
||||
return liters_100
|
||||
|
||||
print(liters_100km_to_miles_gallon(3.9))
|
||||
print(liters_100km_to_miles_gallon(7.5))
|
||||
print(liters_100km_to_miles_gallon(10.))
|
||||
print(miles_gallon_to_liters_100km(60.3))
|
||||
print(miles_gallon_to_liters_100km(31.4))
|
||||
print(miles_gallon_to_liters_100km(23.5))
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
def mysplit(string):
|
||||
words = []
|
||||
word = ""
|
||||
for char in string:
|
||||
if char != " ":
|
||||
word += char
|
||||
else:
|
||||
if word:
|
||||
words.append(word)
|
||||
word = ""
|
||||
if word:
|
||||
words.append(word)
|
||||
return words
|
||||
|
||||
|
||||
print(mysplit("To be or not to be, that is the question"))
|
||||
print(mysplit("To be or not to be,that is the question"))
|
||||
print(mysplit(" "))
|
||||
print(mysplit(" abc "))
|
||||
print(mysplit(""))
|
||||
@@ -0,0 +1,22 @@
|
||||
number = input("Inserisci un numero: ")
|
||||
|
||||
digits = [
|
||||
["###", "# #", "# #", "# #", "###"], # 0
|
||||
[" #", " #", " #", " #", " #"], # 1
|
||||
["###", " #", "###", "# ", "###"], # 2
|
||||
["###", " #", "###", " #", "###"], # 3
|
||||
["# #", "# #", "###", " #", " #"], # 4
|
||||
["###", "# ", "###", " #", "###"], # 5
|
||||
["###", "# ", "###", "# #", "###"], # 6
|
||||
["###", " #", " #", " #", " #"], # 7
|
||||
["###", "# #", "###", "# #", "###"], # 8
|
||||
["###", "# #", "###", " #", "###"], # 9
|
||||
]
|
||||
|
||||
for row in range(5):
|
||||
line = ""
|
||||
for ch in number:
|
||||
digit_index = int(ch)
|
||||
segment = digits[digit_index][row]
|
||||
line = line + segment + " "
|
||||
print(line)
|
||||
@@ -0,0 +1,47 @@
|
||||
def cifra(messaggio, shift):
|
||||
"""
|
||||
Cifra il messaggio usando il cifrario di Cesare con lo spostamento dato.
|
||||
- Lettere minuscole rimangono minuscole
|
||||
- Lettere maiuscole rimangono maiuscole
|
||||
- Caratteri non alfabetici (spazi, numeri, punteggiatura) rimangono invariati
|
||||
"""
|
||||
risultato = ""
|
||||
|
||||
for c in messaggio:
|
||||
if c.islower():
|
||||
base = ord('a')
|
||||
codice = ord(c)
|
||||
nuovo_codice = (codice - base + shift) % 26 + base
|
||||
cifrato = chr(nuovo_codice)
|
||||
calcolo = f"({codice} - {base} + {shift}) % 26 + {base} = {nuovo_codice}"
|
||||
|
||||
elif c.isupper():
|
||||
base = ord('A')
|
||||
codice = ord(c)
|
||||
nuovo_codice = (codice - base + shift) % 26 + base
|
||||
cifrato = chr(nuovo_codice)
|
||||
calcolo = f"({codice} - {base} + {shift}) % 26 + {base} = {nuovo_codice}"
|
||||
|
||||
else:
|
||||
cifrato = c
|
||||
|
||||
risultato += cifrato
|
||||
|
||||
return risultato
|
||||
|
||||
|
||||
|
||||
messaggio = input("Inserire un messaggio da cifrare: ")
|
||||
|
||||
shift = 0
|
||||
while shift < 1 or shift > 25:
|
||||
try:
|
||||
shift = int(input("Inserire il valore di spostamento (1-25): "))
|
||||
if shift < 1 or shift > 25:
|
||||
print("Valore fuori intervallo. Inserire un numero tra 1 e 25.")
|
||||
except ValueError:
|
||||
print("Input non valido. Inserire un numero intero.")
|
||||
|
||||
testo_cifrato = cifra(messaggio, shift)
|
||||
|
||||
print(f"Messaggio cifrato: {testo_cifrato}")
|
||||
@@ -0,0 +1,15 @@
|
||||
iban = input("Inserisci un iban valido: ")
|
||||
|
||||
iban_ruotato = iban[4:]+iban[:4]
|
||||
|
||||
iban_numerico = ""
|
||||
for c in iban_ruotato:
|
||||
if c.isalpha():
|
||||
iban_numerico += str(ord(c)-ord("A") + 10)
|
||||
else:
|
||||
iban_numerico += c
|
||||
|
||||
if int(iban_numerico) % 97 == 1:
|
||||
print("Iban valido")
|
||||
else:
|
||||
print("Iban non valido")
|
||||
@@ -0,0 +1,14 @@
|
||||
def anagramma(parola1, parola2):
|
||||
parola_1 = list(parola1)
|
||||
parola_2 = list(parola2)
|
||||
|
||||
if sorted(parola_1) == sorted(parola_2):
|
||||
print("Anagrami")
|
||||
else:
|
||||
print("Non anagrammi")
|
||||
|
||||
parola_uno = input("Inserisci 1 parola: ")
|
||||
parola_due = input("Inserisci 2 parola: ")
|
||||
|
||||
|
||||
anagramma(parola_uno, parola_due)
|
||||
@@ -0,0 +1,17 @@
|
||||
data = input("Inserisci la tua data di nascita (formato AAAA MM GG): ")
|
||||
|
||||
data_raw = data.replace(" ","")
|
||||
|
||||
def somma_data(data_da_sommare):
|
||||
data = 0
|
||||
for ch in data_da_sommare:
|
||||
data += int(ch)
|
||||
|
||||
return data
|
||||
|
||||
a = somma_data(data_raw)
|
||||
|
||||
while a >= 10:
|
||||
a = somma_data(str(a))
|
||||
|
||||
print("La cifra della vita è:", a)
|
||||
@@ -0,0 +1,15 @@
|
||||
str1 = input("Inserire la prima stringa: ")
|
||||
str2 = input("Inserire la seconda stringa: ")
|
||||
|
||||
pos = 0
|
||||
|
||||
for ch in str1:
|
||||
lettera = str2.find(ch, pos)
|
||||
|
||||
if lettera == -1:
|
||||
print("No")
|
||||
break
|
||||
pos = lettera + 1
|
||||
else:
|
||||
print("Si")
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
print("Inserire le righe per verificare la validità del sudoku")
|
||||
|
||||
S = []
|
||||
|
||||
for i in range(9):
|
||||
riga = input("Inserire riga: ")
|
||||
riga_n = [int(c) for c in riga]
|
||||
S.append(riga_n)
|
||||
|
||||
status = True
|
||||
|
||||
for j in range(9):
|
||||
riga = S[j]
|
||||
for n in range(1, 10):
|
||||
if n not in riga:
|
||||
status = False
|
||||
|
||||
for k in range(9):
|
||||
colonna = [S[r][k] for r in range(9)]
|
||||
for n in range(1, 10):
|
||||
if n not in colonna:
|
||||
status = False
|
||||
|
||||
for r in range(0, 9, 3):
|
||||
for c in range(0, 9, 3):
|
||||
quadrato = [S[r+dr][c+dc] for dr in range(3) for dc in range(3)]
|
||||
for n in range(1, 10):
|
||||
if n not in quadrato:
|
||||
status = False
|
||||
|
||||
if status == True:
|
||||
print("Si")
|
||||
else:
|
||||
print("No")
|
||||
@@ -0,0 +1,32 @@
|
||||
class Stack:
|
||||
def __init__(self):
|
||||
self.__stack_list = []
|
||||
|
||||
def push(self, val):
|
||||
self.__stack_list.append(val)
|
||||
|
||||
def pop(self):
|
||||
val = self.__stack_list[-1]
|
||||
del self.__stack_list[-1]
|
||||
return val
|
||||
|
||||
class CountingStack(Stack):
|
||||
def __init__(self):
|
||||
Stack.__init__(self)
|
||||
self.__j = 0
|
||||
|
||||
def get_counter(self):
|
||||
return self.__j
|
||||
|
||||
def pop(self):
|
||||
self.__j += 1
|
||||
return Stack.pop(self)
|
||||
|
||||
|
||||
stk = CountingStack()
|
||||
|
||||
for i in range(100):
|
||||
stk.push(i)
|
||||
stk.pop()
|
||||
|
||||
print(stk.get_counter())
|
||||
@@ -0,0 +1,27 @@
|
||||
class QueueError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Queue:
|
||||
def __init__(self):
|
||||
self.__queue_list = []
|
||||
|
||||
def put(self, val):
|
||||
self.__queue_list.insert(0, val)
|
||||
|
||||
def get(self):
|
||||
if len(self.__queue_list) < 1:
|
||||
raise QueueError("Errore di coda")
|
||||
return self.__queue_list.pop()
|
||||
|
||||
que = Queue()
|
||||
|
||||
que.put(1)
|
||||
que.put("cane")
|
||||
que.put(False)
|
||||
|
||||
try:
|
||||
for i in range(4):
|
||||
print(que.get())
|
||||
except:
|
||||
print("Errore di coda")
|
||||
@@ -0,0 +1,34 @@
|
||||
class QueueError(Exception):
|
||||
pass
|
||||
|
||||
class Queue:
|
||||
def __init__(self):
|
||||
self.__queue_list = []
|
||||
|
||||
def put(self, val):
|
||||
self.__queue_list.insert(0, val)
|
||||
|
||||
def get(self):
|
||||
if len(self.__queue_list) < 1:
|
||||
raise QueueError("Errore di coda")
|
||||
return self.__queue_list.pop()
|
||||
|
||||
class SuperQueue(Queue):
|
||||
def __init__(self):
|
||||
Queue.__init__(self)
|
||||
|
||||
def isempty(self):
|
||||
return len(self._Queue__queue_list) == 0
|
||||
|
||||
|
||||
que = SuperQueue()
|
||||
|
||||
que.put(1)
|
||||
que.put("cane")
|
||||
que.put(False)
|
||||
|
||||
for i in range(4):
|
||||
if not que.isempty():
|
||||
print(que.get())
|
||||
else:
|
||||
print("Coda vuota")
|
||||
@@ -0,0 +1,41 @@
|
||||
def format_time(hh, mm, ss):
|
||||
return f"{hh:02d}:{mm:02d}:{ss:02d}"
|
||||
|
||||
class Timer:
|
||||
def __init__(self, hours, minutes, seconds):
|
||||
self.__hours = hours
|
||||
self.__minutes = minutes
|
||||
self.__seconds = seconds
|
||||
|
||||
def __str__(self):
|
||||
return format_time(self.__hours, self.__minutes, self.__seconds)
|
||||
|
||||
def next_second(self):
|
||||
self.__seconds += 1
|
||||
if self.__seconds > 59:
|
||||
self.__seconds = 0
|
||||
self.__minutes += 1
|
||||
if self.__minutes > 59:
|
||||
self.__minutes = 0
|
||||
self.__hours += 1
|
||||
if self.__hours > 23:
|
||||
self.__hours = 0
|
||||
|
||||
def prev_second(self):
|
||||
self.__seconds -= 1
|
||||
if self.__seconds < 0:
|
||||
self.__seconds = 59
|
||||
self.__minutes -= 1
|
||||
if self.__minutes < 0:
|
||||
self.__minutes = 59
|
||||
self.__hours -= 1
|
||||
if self.__hours < 0:
|
||||
self.__hours = 23
|
||||
|
||||
|
||||
timer = Timer(23, 59, 59)
|
||||
print(timer)
|
||||
timer.next_second()
|
||||
print(timer)
|
||||
timer.prev_second()
|
||||
print(timer)
|
||||
@@ -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())
|
||||
@@ -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))
|
||||
@@ -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))
|
||||
@@ -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))
|
||||
@@ -0,0 +1,5 @@
|
||||
John Smith 5
|
||||
Anna Bolena 4,5
|
||||
John Smith 2
|
||||
Anna Bolena 11
|
||||
Andrew Cox 1,5
|
||||
@@ -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)
|
||||
@@ -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"))
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user