Advertisement
bruh1214

Untitled

Oct 19th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.94 KB | None | 0 0
  1. from datetime import datetime
  2. import datetime
  3.  
  4.  
  5. def creeaza_pachet(data_i, data_s, dest, pret):
  6. # functie care creeaza un pachet de calatorie cu data de inceput data_i de tip data, data de sfarsit de tip data,
  7. # dest o destinatie string si pret de tip float > 0
  8. # input: data_i = data de inceput - data
  9. # data_s = data de sfarsit - data
  10. # dest = destinatie - string
  11. # pret = pret - float > 0
  12. return {
  13. "data_i": data_i,
  14. "data_s": data_s,
  15. "dest": dest,
  16. "pret": pret
  17. }
  18.  
  19.  
  20. def get_data_i(pachet):
  21. # functie care returneaza data de inceput a pachetului pachet
  22. # input: pachet - un pachet
  23. # output: data - o data
  24. return pachet["data_i"]
  25.  
  26.  
  27. def get_data_s(pachet):
  28. # functie care returneaza data data de sfarsit a pachetului pachet
  29. # input: pachet - un pachet
  30. # output: data - o data
  31. return pachet["data_s"]
  32.  
  33.  
  34. def get_destinatie(pachet):
  35. # functie care returneaza destinatia string a pachetului pachet
  36. # input: pachet - un pachet
  37. # output: destinatia string a pachetului
  38. return pachet["dest"]
  39.  
  40.  
  41. def get_pret(pachet):
  42. # functie care returneaza pretul float a pachetului pachet
  43. # input: pachet - un pachet
  44. # output: pretul float > 0 al pachetului
  45. return pachet["pret"]
  46.  
  47.  
  48. def valideaza_pachet(pachet):
  49. # functie care valideaza daca un pachet pachet este introdus corect sau nu
  50. # input : pachet - pachet
  51. # output: -, daca pachetul a fost introdus corect
  52. # raises Exception cu mesajul
  53. # "date gresite!"
  54. # "destinatie gresita!"
  55. # "pret gresit!"
  56. err = ""
  57. if get_data_i(pachet) > get_data_s(pachet):
  58. err += "date gresite!\n"
  59. if get_destinatie(pachet) == " ":
  60. err += "destinatie gresita!\n"
  61. if get_pret(pachet) <= 0:
  62. err += "pret gresit!\n"
  63. if len(err) > 0:
  64. raise Exception(err)
  65.  
  66.  
  67. def egale_pret(p0, p1):
  68. return get_pret(p0) == get_pret(p1)
  69.  
  70.  
  71. def egale_dest(p0, p1):
  72. return get_destinatie(p0) == get_destinatie(p1)
  73.  
  74.  
  75. def egale_data_i(p0, p1):
  76. return get_data_i(p0) == get_data_i(p1)
  77.  
  78.  
  79. def egale_data_s(p0, p1):
  80. return get_data_s(p0) == get_data_s(p1)
  81.  
  82.  
  83. def adauga_pachet_in_lista(l, pachet):
  84. # functie care adauga un pachet intr-o lista
  85. # input: l - lista
  86. # pachet - pachet
  87. # output: l' - lista cu pachetul
  88. # raises Exception cu mesajul
  89. # "pachet cu aceleasi date introduse!\n"
  90. # daca aceleasi date au fost introduse din nou
  91. for _pachet in l:
  92. if egale_pret(_pachet, pachet) and egale_dest(_pachet, pachet) and egale_data_i(_pachet,
  93. pachet) and egale_data_s(
  94. _pachet, pachet):
  95. raise Exception("pachet cu aceleasi date introduse!\n")
  96. l.append(pachet)
  97.  
  98.  
  99. def srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret):
  100. # functie care creeaza un pachet si incearca sa il introduca in lista l
  101. # input: l - lista
  102. # pachet - pachet
  103. # output: -, daca totul e ok
  104. # raises Exception cu mesajele din functiile anterioare
  105. pachet = creeaza_pachet(data_i, data_s, dest, pret)
  106. valideaza_pachet(pachet)
  107. adauga_pachet_in_lista(l, pachet)
  108.  
  109.  
  110. def test_srv_adauga_pachet_in_lista():
  111. l = []
  112. assert (len(l) == 0)
  113. srv_adauga_pachet_in_lista(l, datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  114. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
  115. assert (len(l) == 1)
  116. try:
  117. srv_adauga_pachet_in_lista(l, datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  118. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", 9000.1)
  119. assert False
  120. except Exception as ex:
  121. assert (str(ex) == "date gresite!\ndestinatie gresita!\n")
  122.  
  123.  
  124. def test_adauga_pachet_in_lista():
  125. l = []
  126. assert (len(l) == 0)
  127. pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  128. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  129. "Galati", 9000.1)
  130. adauga_pachet_in_lista(l, pachet)
  131. assert (len(l) == 1)
  132. assert (get_data_i(pachet) == get_data_i(l[0]))
  133. assert (get_data_s(pachet) == get_data_s(l[0]))
  134. assert (get_destinatie(pachet) == get_destinatie(l[0]))
  135. assert (abs(get_pret(pachet) - get_pret(l[0]) < 0.0001))
  136. alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  137. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
  138. try:
  139. adauga_pachet_in_lista(l, alt_pachet)
  140. assert False
  141. except Exception as ex:
  142. assert (str(ex) == "pachet cu aceleasi date introduse!\n")
  143.  
  144.  
  145. def test_valideaza_pachet():
  146. pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  147. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  148. "Galati", 9000.1)
  149. valideaza_pachet(pachet)
  150. pachet_gresit = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  151. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), "Galati", 9000.1)
  152. try:
  153. valideaza_pachet(pachet_gresit)
  154. except Exception as ex:
  155. assert (str(ex) == "date gresite!\n")
  156. alt_pachet = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  157. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"), " ", -904)
  158. try:
  159. valideaza_pachet(alt_pachet)
  160. assert False
  161. except Exception as ex:
  162. assert (str(ex) == "date gresite!\ndestinatie gresita!\npret gresit!\n")
  163.  
  164.  
  165. def test_creeaza_pachet():
  166. data_i_string = '24/08/2021'
  167. data_i = datetime.datetime.strptime(data_i_string, "%d/%m/%Y")
  168. data_s_string = '26/08/2021'
  169. data_s = datetime.datetime.strptime(data_s_string, "%d/%m/%Y")
  170. dest = "Galati"
  171. pret = 9000.1
  172. pachet = creeaza_pachet(data_i, data_s, dest, pret)
  173. assert (get_data_i(pachet) == data_i)
  174. assert (get_data_s(pachet) == data_s)
  175. assert (get_destinatie(pachet) == dest)
  176. assert (abs(get_pret(pachet) - pret) < 0.0001)
  177.  
  178.  
  179. def test_numar_oferte():
  180. l = []
  181. pachet_1 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  182. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  183. "Galati", 9000.1)
  184. pachet_2 = creeaza_pachet(datetime.datetime.strptime('23/08/2021', "%d/%m/%Y"),
  185. datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  186. "Galati", 9200.1)
  187. pachet_3 = creeaza_pachet(datetime.datetime.strptime('19/08/2021', "%d/%m/%Y"),
  188. datetime.datetime.strptime('22/08/2021', "%d/%m/%Y"),
  189. "Galati", 90040.1)
  190. pachet_4 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  191. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  192. "Braila", 9020.1)
  193. adauga_pachet_in_lista(l, pachet_1)
  194. adauga_pachet_in_lista(l, pachet_2)
  195. adauga_pachet_in_lista(l, pachet_3)
  196. adauga_pachet_in_lista(l, pachet_4)
  197. nr_oferte = 0
  198. for _pachet in l:
  199. if get_destinatie(_pachet) == "Galati":
  200. nr_oferte += 1
  201. assert nr_oferte == 3
  202.  
  203.  
  204. def test_medie_pret():
  205. l = []
  206. pachet_1 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  207. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  208. "Galati", 9000.1)
  209. pachet_2 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  210. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  211. "Galati", 9011)
  212. pachet_3 = creeaza_pachet(datetime.datetime.strptime('24/08/2021', "%d/%m/%Y"),
  213. datetime.datetime.strptime('26/08/2021', "%d/%m/%Y"),
  214. "Galati", 9211.2)
  215. adauga_pachet_in_lista(l, pachet_1)
  216. adauga_pachet_in_lista(l, pachet_2)
  217. adauga_pachet_in_lista(l, pachet_3)
  218. for _pachet in l:
  219. try:
  220. medie_pret(l, "Galati")
  221. except ZeroDivisionError:
  222. assert medie_pret(l, "Galati") == "Media pretului este ", 9074.1
  223.  
  224.  
  225. def run_teste():
  226. test_creeaza_pachet()
  227. test_valideaza_pachet()
  228. test_adauga_pachet_in_lista()
  229. test_srv_adauga_pachet_in_lista()
  230. test_numar_oferte()
  231. test_medie_pret()
  232.  
  233.  
  234. def to_str_pachet(pachet):
  235. return str(
  236. "Data de inceput: " + str(pachet["data_i"]) + "\n" + "Data de sfarsit: " + str(
  237. pachet["data_s"]) + "\n" + "Destinatia: " +
  238. pachet["dest"] + "\n" + "Pretul: " + str(pachet["pret"]))
  239.  
  240.  
  241. def to_int_data_i(pachet):
  242. # functie care returneaza int-ul din data de inceput
  243. # e.g. daca avem data de inceput data_i = 25-10-2021, functia va returna 20211025
  244. return 10000 * get_data_i(pachet).year + 100 * get_data_i(pachet).month + get_data_i(pachet).day
  245.  
  246.  
  247. def to_int_data_s(pachet):
  248. # functie care returneaza int-ul din data de sfarsit
  249. # e.g. daca avem data de sfarsit data_i = 25-10-2021, functia va returna 20211025
  250. return 10000 * get_data_s(pachet).year + 100 * get_data_s(pachet).month + get_data_s(pachet).day
  251.  
  252.  
  253. def verifica_zile_int(pachet):
  254. # functie care verifica numarul de zile pt 2 date de de inceput si de sfarsit
  255. return to_int_data_s(pachet) - to_int_data_i(pachet)
  256.  
  257.  
  258. def medie_pret(l, dest_user_i):
  259. # functie ce returneaza media de pret pentru o destinatie data
  260. suma_pret = 0
  261. nr_dest = 0
  262. for _pachet in l:
  263. if get_destinatie(_pachet) == dest_user_i:
  264. suma_pret += get_pret(_pachet)
  265. nr_dest += 1
  266. if nr_dest == 0:
  267. return get_pret(_pachet)
  268. try:
  269. return float(suma_pret / nr_dest)
  270. except ZeroDivisionError:
  271. print("Date introduse gresit!\n")
  272.  
  273.  
  274. def numar_oferte(l):
  275. # functie care calculeaza numarul de oferte pentru o locatie data
  276. nr_oferte = 0
  277. dest_input = str(input("Introduceti destinatia pe care vreti o sa cautati aici: "))
  278. for _pachet in l:
  279. if get_destinatie(_pachet) == dest_input:
  280. nr_oferte += 1
  281. print("Numarul de oferte pentru destinatia ", dest_input, "este ", nr_oferte)
  282.  
  283.  
  284. def sterge_pret(l,user_input,lst):
  285. #functie care sterge pachetele care au un pret mai mare decat user_input
  286. for el,pachet in enumerate(l):
  287. if get_pret(pachet) > user_input:
  288. lst.append(l[el])
  289. l.remove(l[el])
  290.  
  291. def sterge_pachet_dest(l, user_input,lst):
  292. # functie care sterge pachetele cu aceeasi destinatie ca si user_input
  293. for el, pachet in enumerate(l):
  294. if get_destinatie(pachet) == user_input:
  295. lst.append(l[el])
  296. l.remove(l[el])
  297.  
  298.  
  299. def sterge_zile(l, user_input,lst):
  300. # functie care sterge pachetele cu acelasi numar de zile ca si user_input
  301. for el, pachet in enumerate(l):
  302. if verifica_zile_int(pachet) == user_input:
  303. lst.append(l[el])
  304. l.remove(l[el])
  305.  
  306.  
  307. def eliminare_luna(l, luna_user_i,lst):
  308. # functie care sterge pachetele cu aceeasi luna ca si luna_user_i
  309. for el, pachet in enumerate(l):
  310. if get_data_i(pachet).month == luna_user_i and get_data_i(pachet).month == luna_user_i or get_data_s(
  311. pachet).month == luna_user_i and get_data_s(pachet) == luna_user_i:
  312. lst.append(l[el])
  313. l.remove(l[el])
  314.  
  315.  
  316. def undo(l,lst):
  317. if len(l) == 0:
  318. print("Nu exista pachete")
  319. else:
  320. for el,pachet in enumerate(l):
  321. l.pop()
  322. l.append(lst[el])
  323.  
  324.  
  325.  
  326. def ui_adauga_pachet(l):
  327. try:
  328. data_i = input("Introduceti data de inceput in formatul dd-mm-yyyy: ")
  329. data_i = datetime.datetime.strptime(data_i, "%d-%m-%Y").date()
  330. data_s = input("Introduceti data de sfarsit in formatul dd-mm-yyyy: ")
  331. data_s = datetime.datetime.strptime(data_s, "%d-%m-%Y").date()
  332. except ValueError:
  333. print("date numerice invalide!")
  334. return
  335. dest = input("Introduceti destinatia aici: ")
  336. try:
  337. pret = float(input("Introduceti pretul aici: "))
  338. except ValueError:
  339. print("pret invalid!")
  340. return
  341. srv_adauga_pachet_in_lista(l, data_i, data_s, dest, pret)
  342.  
  343.  
  344. def ui_print_pachet(l):
  345. for pachet in l:
  346. print(to_str_pachet(pachet))
  347. if len(l) == 0:
  348. print("Nu a fost introdus niciun pachet!")
  349.  
  350.  
  351. def ui_modifica_date(l):
  352. print("Pachete introduse pana acum: ")
  353. for el, pachet in enumerate(l, start=1):
  354. print(f"{el}.{pachet}")
  355. alegere_mod = int(input("alegeti ce pachet vreti sa modificati: "))
  356. print("Se va modifica pachetul: ", alegere_mod, l[alegere_mod - 1])
  357. l.remove(l[alegere_mod - 1])
  358. ui_adauga_pachet(l)
  359.  
  360.  
  361. def ui_sterge_pachet_dest(l,lst):
  362. if len(l) == 0:
  363. print("nu exista niciun pachet de sters")
  364. else:
  365. user_input = str(input("Introduceti destinatia pachetului pe care vreti sa il stergeti: "))
  366. print("Se vor sterge pachetele cu destinatia: ", user_input)
  367. sterge_pachet_dest(l, user_input,lst)
  368.  
  369.  
  370. def ui_sterge_zile(l,lst):
  371. if len(l) == 0:
  372. print("nu exista niciun pachet de sters")
  373. else:
  374. user_input = int(input("Introduceti zilele pachetului pe care vreti sa il stergeti: "))
  375. print("Se vor sterge pachetele cu destinatia: ", user_input)
  376. sterge_zile(l, user_input,lst)
  377.  
  378.  
  379. def ui_sterge_pret(l,lst):
  380. if len(l) == 0:
  381. print("nu exista niciun pachet de sters")
  382. else:
  383. user_input = float(input("Introduceti pretul pachetului pe care vreti sa il stergeti: "))
  384. print("Se vor sterge pachetele care au pretul mai mare decat: ", user_input)
  385. sterge_pret(l, user_input,lst)
  386.  
  387.  
  388. def ui_numar_oferte(l):
  389. if len(l) == 0:
  390. print("Nu a fost introdus niciun pachet!")
  391. else:
  392. numar_oferte(l)
  393.  
  394.  
  395. def ui_medie_pret(l):
  396. if len(l) == 0:
  397. print("Nu a fost introdus niciun pachet!")
  398. else:
  399. dest_user_i = str(input("Introduceti destinatia pe care vreti sa o cautati aici: "))
  400. print("Media pretului este ", medie_pret(l, dest_user_i))
  401.  
  402.  
  403. def ui_eliminare_luna(l):
  404. if len(l) == 0:
  405. print("Nu a fost introdus niciun pachet!")
  406. else:
  407. luna_user_i = int(input("Introduceti luna pachetelor pe care vreti sa le stergeti: "))
  408. eliminare_luna(l, luna_user_i)
  409. print("Au fost sters(e) pachetele")
  410.  
  411.  
  412. def ui_undo(l):
  413. pass
  414.  
  415.  
  416. def run():
  417. l = []
  418. lst = []
  419. while True:
  420. cmd = input(">>>")
  421. if cmd == "exit":
  422. return
  423. if cmd == " ":
  424. continue
  425. if cmd == "add_pachet":
  426. try:
  427. ui_adauga_pachet(l)
  428. except Exception as ex:
  429. print(ex)
  430. elif cmd == "modifica_pachet":
  431. try:
  432. ui_modifica_date(l)
  433. except ValueError:
  434. print("date numerice invalide!")
  435. elif cmd == "print_pachete":
  436. ui_print_pachet(l)
  437. elif cmd == "sterge_dest":
  438. ui_sterge_pachet_dest(l,lst)
  439. elif cmd == "sterge_zile":
  440. ui_sterge_zile(l,lst)
  441. elif cmd == "sterge_pret":
  442. ui_sterge_pret(l,lst)
  443. elif cmd == "numar_oferte":
  444. ui_numar_oferte(l)
  445. elif cmd == "medie_pret":
  446. ui_medie_pret(l)
  447. elif cmd == "eliminare_luna":
  448. ui_eliminare_luna(l)
  449. elif cmd == "undo":
  450. undo(l,lst)
  451. else:
  452. print("comanda invalida!")
  453.  
  454.  
  455. def main():
  456. run()
  457. run_teste()
  458.  
  459.  
  460. main()
  461.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement