Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #DOMACI 5
- def ucitajPodatke():
- # Ucitavamo fajl i podatke
- # a) Ucitamo input i output fajl sa standardnog ulaza
- input_file, output_file = input().split()
- # b) Ucitavamo pocetni i krajnji datum sa standardnog ulaza
- start_date = input().split(".")
- end_date = input().split(".")
- for i in range(3):
- start_date[i] = int(start_date[i])
- end_date[i] = int(end_date[i])
- # c) Ucitavamo zanr koji se posmatra sa standardnog ulaza
- genre = input()
- # d) Citamo iz input fajla
- movies_file = open(input_file)
- lines = movies_file.readlines()[1:] #Odbacuje se prva linija tj zaglavlje
- movies_file.close()
- return lines, input_file, output_file, start_date, end_date, genre
- def obradiPodatke(lines, input_file, output_file, start_date, end_date, genre):
- # 2) Obrada podataka
- # Svaki film se pretvara u recnik i dodaje na listu filmova
- movies = []
- for line in lines:
- # npr da je line = "1;Daddy and Them;Comedy|Drama;14.11.2007;Briano Andriveau;$90672351.24"
- movie_info = line[:-1].split(";") #Odbacuje se poslednji karakter tj "\n"
- movie = {"id": movie_info[0], "Movie Title": movie_info[1], "Movie Genre": movie_info[2].split("|"),
- "Date": movie_info[3].split("."), "Director": movie_info[4], "Revenue": float(movie_info[5][1:])}
- movies.append(movie)
- # a) izdvojimo samo one filmove koji su izasli izmedju ona dva datuma
- movies_to_keep = []
- for movie in movies:
- movie["Date"], start_date, end_date
- #proverimo godine
- date = [int(movie["Date"][0]), int(movie["Date"][1]), int(movie["Date"][2])]
- #Provere se godine pa meseci pa dani
- if date[2] < end_date[2] and date[2] > start_date[2]:
- movies_to_keep.append(movie)
- elif date[2] == end_date[2]:
- if date[1] > start_date[1]:
- movies_to_keep.append(movie)
- elif date[1] == start_date[1]:
- if date[0] > start_date[0]:
- movies_to_keep.append(movie)
- elif date[0] == start_date[0]:
- movies_to_keep.append(movie)
- elif date[2] == start_date[2]:
- if date[1] < end_date[1]:
- movies_to_keep.append(movie)
- elif date[1] == end_date[1]:
- if date[0] < end_date[0]:
- movies_to_keep.append(movie)
- elif date[0] == end_date[0]:
- movies_to_keep.append(movie)
- # b) izdvojimo samo one filmove koji spadaju u zadati genre
- movies_to_keep_2 = []
- for movie in movies_to_keep:
- for i in movie["Movie Genre"]:
- if i == genre:
- movies_to_keep_2.append(movie)
- # c) delimo na reditelje
- directors = []
- for movie in movies:
- if movie["Director"] not in directors:
- directors.append(movie["Director"])
- directors.sort()
- movies_per_director = {}
- for director in directors:
- movies_per_director[director] = []
- for movie in movies_to_keep_2:
- movies_per_director[movie["Director"]].append(movie)
- # d) Za svakog reditelja nadjemo film sa najmanjom i film sa najvecom zaradom
- min_movies = []
- max_movies = []
- for director in directors:
- movies = movies_per_director[director]
- min_movie = movies[0].copy()
- max_movie = movies[0].copy()
- for movie in movies:
- if movie["Revenue"] < min_movie["Revenue"]:
- min_movie = movie.copy()
- if movie["Revenue"] > max_movie["Revenue"]:
- max_movie = movie.copy()
- min_movies.append(min_movie)
- max_movies.append(max_movie)
- return directors, min_movies, max_movies
- def ispisiPodatke(directors, min_movies, max_movies):
- # 3) Ispisivanje podataka u output fajl
- # a) Otvoriti fajl za pisanje
- f = open(output_file, "w")
- # b) Upisati zaglavlje (Director;Movie Title Min Revenue;Movie Title Max Revenue)
- f.write("Director;Movie Title Min Revenue;Movie Title Max Revenue\n")
- # c) Za svakog direktora ispisati liniju koja prikazuje trazene podatke
- for i in range(len(directors)):
- min_title = min_movies[i]["Movie Title"]
- min_rev = "{:.2f}".format(min_movies[i]["Revenue"])
- max_title = max_movies[i]["Movie Title"]
- max_rev = "{:.2f}".format(max_movies[i]["Revenue"])
- f.write(directors[i] + ";" + min_title + " : " + min_rev + ";" + max_title + " : " + max_rev + "\n")
- # d) Zatvoriti output fajl
- f.close()
- try:
- lines, input_file, output_file, start_date, end_date, genre = ucitajPodatke()
- directors, min_movies, max_movies = obradiPodatke(lines, input_file, output_file, start_date, end_date, genre)
- ispisiPodatke(directors, min_movies, max_movies)
- except FileNotFoundError:
- print("DAT_GRESKA")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement