Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- import datetime
- def creeaza_pachet(data_i, data_s, dest, pret):
- # functie care creeaza un pachet de calatorie cu data de inceput data_i de tip data, data de sfarsit de tip data,
- # dest o destinatie string si pret de tip float > 0
- # input: data_i = data de inceput - data
- # data_s = data de sfarsit - data
- # dest = destinatie - string
- # pret = pret - float > 0
- return {
- "data_i": data_i,
- "data_s": data_s,
- "dest": dest,
- "pret": pret
- }
- def get_data_i(pachet):
- # functie care returneaza data de inceput a pachetului pachet
- # input: pachet - un pachet
- # output: data - o data
- return pachet["data_i"]
- def get_data_s(pachet):
- # functie care returneaza data data de sfarsit a pachetului pachet
- # input: pachet - un pachet
- # output: data - o data
- return pachet["data_s"]
- def get_destinatie(pachet):
- # functie care returneaza destinatia string a pachetului pachet
- # input: pachet - un pachet
- # output: destinatia string a pachetului
- return pachet["dest"]
- def get_pret(pachet):
- # functie care returneaza pretul float a pachetului pachet
- # input: pachet - un pachet
- # output: pretul float > 0 al pachetului
- return pachet["pret"]
- def valideaza_pachet(pachet):
- # functie care valideaza daca un pachet pachet este introdus corect sau nu
- # input : pachet - pachet
- # output: -, daca pachetul a fost introdus corect
- # raises Exception cu mesajul
- # "date gresite!"
- # "destinatie gresita!"
- # "pret gresit!"
- err = ""
- if get_data_i(pachet) > get_data_s(pachet):
- err += "date gresite!\n"
- if get_destinatie(pachet) == " ":
- err += "destinatie gresita!\n"
- if get_pret(pachet) <= 0:
- err += "pret gresit!\n"
- if len(err) > 0:
- raise Exception(err)
- def egale_pret(p0, p1):
- return get_pret(p0) == get_pret(p1)
- def egale_dest(p0, p1):
- return get_destinatie(p0) == get_destinatie(p1)
- def egale_data_i(p0, p1):
- return get_data_i(p0) == get_data_i(p1)
- def egale_data_s(p0, p1):
- return get_data_s(p0) == get_data_s(p1)
- def adauga_pachet_in_lista(l, pachet):
- # functie care adauga un pachet intr-o lista
- # input: l - lista
- # pachet - pachet
- # output: l' - lista cu pachetul
- # raises Exception cu mesajul
- # "pachet cu aceleasi date introduse!\n"
- # daca aceleasi date au fost introduse din nou
- for _pachet in l:
- if egale_pret(_pachet, pachet) and egale_dest(_pachet, pachet) and egale_data_i(_pachet,
- pachet) and egale_data_s(
- _pachet, pachet):
- raise Exception("pachet cu aceleasi date introduse!\n")
- l.append(pachet)
- def srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret):
- # functie care creeaza un pachet si incearca sa il introduca in lista l
- # input: l - lista
- # pachet - pachet
- # output: -, daca totul e ok
- # raises Exception cu mesajele din functiile anterioare
- pachet = creeaza_pachet(data_i, data_s, dest, pret)
- valideaza_pachet(pachet)
- adauga_pachet_in_lista(l, pachet)
- def test_srv_adauga_pachet_in_lista():
- l = []
- assert (len(l) == 0)
- srv_adauga_pachet_in_lista(l, datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
- assert (len(l) == 1)
- try:
- srv_adauga_pachet_in_lista(l, datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", 9000.1)
- assert False
- except Exception as ex:
- assert (str(ex) == "date gresite!\ndestinatie gresita!\n")
- def test_adauga_pachet_in_lista():
- l = []
- assert (len(l) == 0)
- pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9000.1)
- adauga_pachet_in_lista(l, pachet)
- assert (len(l) == 1)
- assert (get_data_i(pachet) == get_data_i(l[0]))
- assert (get_data_s(pachet) == get_data_s(l[0]))
- assert (get_destinatie(pachet) == get_destinatie(l[0]))
- assert (abs(get_pret(pachet) - get_pret(l[0]) < 0.0001))
- alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
- try:
- adauga_pachet_in_lista(l, alt_pachet)
- assert False
- except Exception as ex:
- assert (str(ex) == "pachet cu aceleasi date introduse!\n")
- def test_valideaza_pachet():
- pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9000.1)
- valideaza_pachet(pachet)
- pachet_gresit = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
- try:
- valideaza_pachet(pachet_gresit)
- except Exception as ex:
- assert (str(ex) == "date gresite!\n")
- alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", -904)
- try:
- valideaza_pachet(alt_pachet)
- assert False
- except Exception as ex:
- assert (str(ex) == "date gresite!\ndestinatie gresita!\npret gresit!\n")
- def test_creeaza_pachet():
- data_i_string = '24/08/2021'
- data_i = datetime.datetime.strptime(data_i_string, "%d/%m/%Y")
- data_s_string = '26/08/2021'
- data_s = datetime.datetime.strptime(data_s_string, "%d/%m/%Y")
- dest = "Galati"
- pret = 9000.1
- pachet = creeaza_pachet(data_i, data_s, dest, pret)
- assert (get_data_i(pachet) == data_i)
- assert (get_data_s(pachet) == data_s)
- assert (get_destinatie(pachet) == dest)
- assert (abs(get_pret(pachet) - pret) < 0.0001)
- def test_numar_oferte():
- l = []
- pachet_1 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9000.1)
- pachet_2 = creeaza_pachet(datetime.datetime.strptime('23/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- "Galati", 9200.1)
- pachet_3 = creeaza_pachet(datetime.datetime.strptime('19/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"),
- "Galati", 90040.1)
- pachet_4 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Braila", 9020.1)
- adauga_pachet_in_lista(l, pachet_1)
- adauga_pachet_in_lista(l, pachet_2)
- adauga_pachet_in_lista(l, pachet_3)
- adauga_pachet_in_lista(l, pachet_4)
- nr_oferte = 0
- for _pachet in l:
- if get_destinatie(_pachet) == "Galati":
- nr_oferte += 1
- assert nr_oferte == 3
- def test_medie_pret():
- l = []
- pachet_1 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9000.1)
- pachet_2 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9011)
- pachet_3 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
- datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
- "Galati", 9211.2)
- adauga_pachet_in_lista(l, pachet_1)
- adauga_pachet_in_lista(l, pachet_2)
- adauga_pachet_in_lista(l, pachet_3)
- for _pachet in l:
- try:
- medie_pret(l, "Galati")
- except ZeroDivisionError:
- assert medie_pret(l, "Galati") == "Media pretului este ", 9074.1
- def run_teste():
- test_creeaza_pachet()
- test_valideaza_pachet()
- test_adauga_pachet_in_lista()
- test_srv_adauga_pachet_in_lista()
- test_numar_oferte()
- test_medie_pret()
- def to_str_pachet(pachet):
- return str(
- "Data de inceput: " + str(pachet["data_i"]) + "\n" + "Data de sfarsit: " + str(
- pachet["data_s"]) + "\n" + "Destinatia: " +
- pachet["dest"] + "\n" + "Pretul: " + str(pachet["pret"]))
- def to_int_data_i(pachet):
- # functie care returneaza int-ul din data de inceput
- # e.g. daca avem data de inceput data_i = 25-10-2021, functia va returna 20211025
- return 10000 * get_data_i(pachet).year + 100 * get_data_i(pachet).month + get_data_i(pachet).day
- def to_int_data_s(pachet):
- # functie care returneaza int-ul din data de sfarsit
- # e.g. daca avem data de sfarsit data_i = 25-10-2021, functia va returna 20211025
- return 10000 * get_data_s(pachet).year + 100 * get_data_s(pachet).month + get_data_s(pachet).day
- def verifica_zile_int(pachet):
- # functie care verifica numarul de zile pt 2 date de de inceput si de sfarsit
- return to_int_data_s(pachet) - to_int_data_i(pachet)
- def medie_pret(l, dest_user_i):
- # functie ce returneaza media de pret pentru o destinatie data
- suma_pret = 0
- nr_dest = 0
- for _pachet in l:
- if get_destinatie(_pachet) == dest_user_i:
- suma_pret += get_pret(_pachet)
- nr_dest += 1
- if nr_dest == 0:
- return get_pret(_pachet)
- try:
- return float(suma_pret / nr_dest)
- except ZeroDivisionError:
- print("Date introduse gresit!\n")
- def numar_oferte(l):
- # functie care calculeaza numarul de oferte pentru o locatie data
- nr_oferte = 0
- dest_input = str(input("Introduceti destinatia pe care vreti o sa cautati aici: "))
- for _pachet in l:
- if get_destinatie(_pachet) == dest_input:
- nr_oferte += 1
- print("Numarul de oferte pentru destinatia ", dest_input, "este ", nr_oferte)
- def sterge_pret(l,user_input,lst):
- #functie care sterge pachetele care au un pret mai mare decat user_input
- for el,pachet in enumerate(l):
- if get_pret(pachet) > user_input:
- lst.append(l[el])
- l.remove(l[el])
- def sterge_pachet_dest(l, user_input,lst):
- # functie care sterge pachetele cu aceeasi destinatie ca si user_input
- for el, pachet in enumerate(l):
- if get_destinatie(pachet) == user_input:
- lst.append(l[el])
- l.remove(l[el])
- def sterge_zile(l, user_input,lst):
- # functie care sterge pachetele cu acelasi numar de zile ca si user_input
- for el, pachet in enumerate(l):
- if verifica_zile_int(pachet) == user_input:
- lst.append(l[el])
- l.remove(l[el])
- def eliminare_luna(l, luna_user_i,lst):
- # functie care sterge pachetele cu aceeasi luna ca si luna_user_i
- for el, pachet in enumerate(l):
- if get_data_i(pachet).month == luna_user_i and get_data_i(pachet).month == luna_user_i or get_data_s(
- pachet).month == luna_user_i and get_data_s(pachet) == luna_user_i:
- lst.append(l[el])
- l.remove(l[el])
- def undo(l,lst):
- for el,pachet in enumerate(l):
- l.append(lst[el])
- l.pop()
- def ui_adauga_pachet(l):
- try:
- data_i = input("Introduceti data de inceput in formatul dd-mm-yyyy: ")
- data_i = datetime.datetime.strptime(data_i, "%d-%m-%Y").date()
- data_s = input("Introduceti data de sfarsit in formatul dd-mm-yyyy: ")
- data_s = datetime.datetime.strptime(data_s, "%d-%m-%Y").date()
- except ValueError:
- print("date numerice invalide!")
- return
- dest = input("Introduceti destinatia aici: ")
- try:
- pret = float(input("Introduceti pretul aici: "))
- except ValueError:
- print("pret invalid!")
- return
- srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret)
- def ui_print_pachet(l):
- for pachet in l:
- print(to_str_pachet(pachet))
- if len(l) == 0:
- print("Nu a fost introdus niciun pachet!")
- def ui_modifica_date(l):
- print("Pachete introduse pana acum: ")
- for el, pachet in enumerate(l, start=1):
- print(f"{el}.{pachet}")
- alegere_mod = int(input("alegeti ce pachet vreti sa modificati: "))
- print("Se va modifica pachetul: ", alegere_mod, l[alegere_mod - 1])
- l.remove(l[alegere_mod - 1])
- ui_adauga_pachet(l)
- def ui_sterge_pachet_dest(l,lst):
- if len(l) == 0:
- print("nu exista niciun pachet de sters")
- else:
- user_input = str(input("Introduceti destinatia pachetului pe care vreti sa il stergeti: "))
- print("Se vor sterge pachetele cu destinatia: ", user_input)
- sterge_pachet_dest(l, user_input,lst)
- def ui_sterge_zile(l,lst):
- if len(l) == 0:
- print("nu exista niciun pachet de sters")
- else:
- user_input = int(input("Introduceti zilele pachetului pe care vreti sa il stergeti: "))
- print("Se vor sterge pachetele cu destinatia: ", user_input)
- sterge_zile(l, user_input,lst)
- def ui_sterge_pret(l,lst):
- if len(l) == 0:
- print("nu exista niciun pachet de sters")
- else:
- user_input = float(input("Introduceti pretul pachetului pe care vreti sa il stergeti: "))
- print("Se vor sterge pachetele care au pretul mai mare decat: ", user_input)
- sterge_pret(l, user_input,lst)
- def ui_numar_oferte(l):
- if len(l) == 0:
- print("Nu a fost introdus niciun pachet!")
- else:
- numar_oferte(l)
- def ui_medie_pret(l):
- if len(l) == 0:
- print("Nu a fost introdus niciun pachet!")
- else:
- dest_user_i = str(input("Introduceti destinatia pe care vreti sa o cautati aici: "))
- print("Media pretului este ", medie_pret(l, dest_user_i))
- def ui_eliminare_luna(l):
- if len(l) == 0:
- print("Nu a fost introdus niciun pachet!")
- else:
- luna_user_i = int(input("Introduceti luna pachetelor pe care vreti sa le stergeti: "))
- eliminare_luna(l, luna_user_i)
- print("Au fost sters(e) pachetele")
- def ui_undo(l):
- pass
- def run():
- l = []
- lst = []
- while True:
- cmd = input(">>>")
- if cmd == "exit":
- return
- if cmd == " ":
- continue
- if cmd == "add_pachet":
- try:
- ui_adauga_pachet(l)
- except Exception as ex:
- print(ex)
- elif cmd == "modifica_pachet":
- try:
- ui_modifica_date(l)
- except ValueError:
- print("date numerice invalide!")
- elif cmd == "print_pachete":
- ui_print_pachet(l)
- elif cmd == "sterge_dest":
- ui_sterge_pachet_dest(l,lst)
- elif cmd == "sterge_zile":
- ui_sterge_zile(l,lst)
- elif cmd == "sterge_pret":
- ui_sterge_pret(l,lst)
- elif cmd == "numar_oferte":
- ui_numar_oferte(l)
- elif cmd == "medie_pret":
- ui_medie_pret(l)
- elif cmd == "eliminare_luna":
- ui_eliminare_luna(l)
- elif cmd == "undo":
- undo(l,lst)
- else:
- print("comanda invalida!")
- def main():
- run()
- run_teste()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement