Compare commits
4 Commits
ed29b5907c
...
2aeaadfe18
| Author | SHA1 | Date | |
|---|---|---|---|
| 2aeaadfe18 | |||
| cc8ee1615b | |||
| 07d1e9dd75 | |||
| 722e008efa |
@@ -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)
|
||||
Reference in New Issue
Block a user