Advertisement
PecaJ

Untitled

Dec 29th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.88 KB | None | 0 0
  1. #DOMACI 5
  2.  
  3. def ucitajPodatke():
  4. # Ucitavamo fajl i podatke
  5. # a) Ucitamo input i output fajl sa standardnog ulaza
  6. input_file, output_file = input().split()
  7. # b) Ucitavamo pocetni i krajnji datum sa standardnog ulaza
  8. start_date = input().split(".")
  9. end_date = input().split(".")
  10. for i in range(3):
  11. start_date[i] = int(start_date[i])
  12. end_date[i] = int(end_date[i])
  13. # c) Ucitavamo zanr koji se posmatra sa standardnog ulaza
  14. genre = input()
  15. # d) Citamo iz input fajla
  16. movies_file = open(input_file)
  17. lines = movies_file.readlines()[1:] #Odbacuje se prva linija tj zaglavlje
  18. movies_file.close()
  19.  
  20. return lines, input_file, output_file, start_date, end_date, genre
  21.  
  22.  
  23. def obradiPodatke(lines, input_file, output_file, start_date, end_date, genre):
  24. # 2) Obrada podataka
  25.  
  26. # Svaki film se pretvara u recnik i dodaje na listu filmova
  27. movies = []
  28. for line in lines:
  29. # npr da je line = "1;Daddy and Them;Comedy|Drama;14.11.2007;Briano Andriveau;$90672351.24"
  30. movie_info = line[:-1].split(";") #Odbacuje se poslednji karakter tj "\n"
  31. movie = {"id": movie_info[0], "Movie Title": movie_info[1], "Movie Genre": movie_info[2].split("|"),
  32. "Date": movie_info[3].split("."), "Director": movie_info[4], "Revenue": float(movie_info[5][1:])}
  33. movies.append(movie)
  34.  
  35. # a) izdvojimo samo one filmove koji su izasli izmedju ona dva datuma
  36.  
  37. movies_to_keep = []
  38. for movie in movies:
  39. movie["Date"], start_date, end_date
  40. #proverimo godine
  41.  
  42. date = [int(movie["Date"][0]), int(movie["Date"][1]), int(movie["Date"][2])]
  43. #Provere se godine pa meseci pa dani
  44. if date[2] < end_date[2] and date[2] > start_date[2]:
  45. movies_to_keep.append(movie)
  46. elif date[2] == end_date[2]:
  47. if date[1] > start_date[1]:
  48. movies_to_keep.append(movie)
  49. elif date[1] == start_date[1]:
  50. if date[0] > start_date[0]:
  51. movies_to_keep.append(movie)
  52. elif date[0] == start_date[0]:
  53. movies_to_keep.append(movie)
  54. elif date[2] == start_date[2]:
  55. if date[1] < end_date[1]:
  56. movies_to_keep.append(movie)
  57. elif date[1] == end_date[1]:
  58. if date[0] < end_date[0]:
  59. movies_to_keep.append(movie)
  60. elif date[0] == end_date[0]:
  61. movies_to_keep.append(movie)
  62.  
  63. # b) izdvojimo samo one filmove koji spadaju u zadati genre
  64. movies_to_keep_2 = []
  65. for movie in movies_to_keep:
  66. for i in movie["Movie Genre"]:
  67. if i == genre:
  68. movies_to_keep_2.append(movie)
  69.  
  70. # c) delimo na reditelje
  71. directors = []
  72. for movie in movies:
  73. if movie["Director"] not in directors:
  74. directors.append(movie["Director"])
  75. directors.sort()
  76.  
  77.  
  78. movies_per_director = {}
  79. for director in directors:
  80. movies_per_director[director] = []
  81. for movie in movies_to_keep_2:
  82. movies_per_director[movie["Director"]].append(movie)
  83.  
  84.  
  85. # d) Za svakog reditelja nadjemo film sa najmanjom i film sa najvecom zaradom
  86. min_movies = []
  87. max_movies = []
  88. for director in directors:
  89. movies = movies_per_director[director]
  90. min_movie = movies[0].copy()
  91. max_movie = movies[0].copy()
  92. for movie in movies:
  93. if movie["Revenue"] < min_movie["Revenue"]:
  94. min_movie = movie.copy()
  95. if movie["Revenue"] > max_movie["Revenue"]:
  96. max_movie = movie.copy()
  97. min_movies.append(min_movie)
  98. max_movies.append(max_movie)
  99.  
  100.  
  101. return directors, min_movies, max_movies
  102.  
  103. def ispisiPodatke(directors, min_movies, max_movies):
  104. # 3) Ispisivanje podataka u output fajl
  105. # a) Otvoriti fajl za pisanje
  106. f = open(output_file, "w")
  107. # b) Upisati zaglavlje (Director;Movie Title Min Revenue;Movie Title Max Revenue)
  108. f.write("Director;Movie Title Min Revenue;Movie Title Max Revenue\n")
  109. # c) Za svakog direktora ispisati liniju koja prikazuje trazene podatke
  110. for i in range(len(directors)):
  111. min_title = min_movies[i]["Movie Title"]
  112. min_rev = "{:.2f}".format(min_movies[i]["Revenue"])
  113. max_title = max_movies[i]["Movie Title"]
  114. max_rev = "{:.2f}".format(max_movies[i]["Revenue"])
  115. f.write(directors[i] + ";" + min_title + " : " + min_rev + ";" + max_title + " : " + max_rev + "\n")
  116. # d) Zatvoriti output fajl
  117. f.close()
  118.  
  119.  
  120.  
  121.  
  122.  
  123. lines, input_file, output_file, start_date, end_date, genre = ucitajPodatke()
  124. directors, min_movies, max_movies = obradiPodatke(lines, input_file, output_file, start_date, end_date, genre)
  125. ispisiPodatke(directors, min_movies, max_movies)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement