Conversation
| new_movie_info = {"title": title, "genre": genre, "rating": rating} | ||
| return new_movie_info |
There was a problem hiding this comment.
since we aren't really doing anything with new_movie_info besides returning it, I would be okay with just returning the dictionary directly like so:
return {"title": title, "genre": genre, "rating": rating}| for movie in user_data["watchlist"]: | ||
| if movie["title"] == title: | ||
| user_data["watchlist"].remove(movie) | ||
| user_data["watched"].append(movie) |
There was a problem hiding this comment.
For this line, did we write a function that could do this for us? I know it's trivial here, but it helps to get in the habit so we can keep our code DRY. Also, why write a function if we won't use it? ✨ Great work on this function overall!
| for movie in user_data["watchlist"]: | ||
| if movie["title"] == title: | ||
| user_data["watchlist"].remove(movie) |
There was a problem hiding this comment.
So now that we have talked about Big O analysis, we know that .remove has a time complexity of O(n) since it needs to look through every element in a list to find the sought after value. Right now, this loop has an outer loop of O(n) and you when you find the movie to remove you have another O(n) operation. How could we circumvent using remove? (Hint: Think about list comprehension)
Also in this implementation, we will only use remove once, but in other filtering problems we might have to do this for multiple elements so it doesn't hurt to get in the habit now.
There was a problem hiding this comment.
question: will ask about it in office hours
| def get_watched_avg_rating(user_data): | ||
| if not user_data["watched"]: | ||
| return 0 | ||
|
|
||
| total_movie_ratings = 0 | ||
|
|
||
| for movie_rating in user_data["watched"]: | ||
| total_movie_ratings += movie_rating["rating"] | ||
|
|
||
| average_movie_rating = total_movie_ratings / len(user_data["watched"]) | ||
|
|
||
| return average_movie_rating |
| for single_movie in user_data["watched"]: | ||
| genre_type = single_movie.get("genre") | ||
| if genre_type: | ||
| frequency_of_genre_index[genre_type] = frequency_of_genre_index.get(genre_type,0) + 1 |
There was a problem hiding this comment.
Love this line right here! A great example of using the .get method that dictionaries supply us with!
| if frequency_of_genre_index[genre_type] > max_frequency: | ||
| max_frequency = frequency_of_genre_index[genre_type] | ||
| most_watched_genre = genre_type |
There was a problem hiding this comment.
Great work on this logic, I now that you've done it a few times (adagrams and PSE 1) I think it would be okay to use the max function that python provides us.
EDIT: Discussing with other instructors we came to the conclusion that we think that it's best to not use max until Unit 2 or to use it and have the logic written out and commented out so we know that you understand the logic behind it.
| my_friends_unique = [movie["title"] for my_friends in user_data["friends"] | ||
| for movie in my_friends["watched"]] |
There was a problem hiding this comment.
Great job with the list comprehension here, I would say this is the most I would try to fit in a comprehension. I think that anything pass this would lower the readability of your code.
| my_unique_movies = [movie for movie in user_data["watched"] | ||
| if movie["title"] not in my_friends_unique] |
There was a problem hiding this comment.
Since my_friends_unique is a list, the in operator has an O(n) time complexity here. Is there another data structure that we can use to circumvent the complexity here? How would that change our code?
There was a problem hiding this comment.
question: will ask about it in office hours
There was a problem hiding this comment.
A set can take a constant amount of time to find the elements. A list would have to look at n-1.
There was a problem hiding this comment.
in my_unique_movies....
if movie["title"] not in my_friends_unique]
searching through the my_friends_unique variable would take a constant if it was a set rather than if it was a list.
| if movie["title"] not in my_movie_titles and movie not in friends_unique_movies: | ||
| friends_unique_movies.append(movie) | ||
|
|
||
| return list(friends_unique_movies) |
There was a problem hiding this comment.
Being that friends_unique_movies is a list already, is there a reason that we wrap it in list()?
|
|
||
| def get_available_recs(user_data): | ||
| recommended_movies = [] | ||
| friends_unique_movies = get_friends_unique_watched(user_data) |
There was a problem hiding this comment.
Using functions that you've already written! Keeping your code DRY! 🎉
| user_watched_titles = set() | ||
|
|
||
| user_watched_titles = [movie['title'] for movie in user_data.get('watched', [])] |
There was a problem hiding this comment.
Is there a reason why this was declared as a set and then converted into a list? I would actually say to keep it as a set since it could improve the efficiency of your code.
| recommended_movies = [movie for movie in friends_unique_movies | ||
| if movie['host'] in user_subscriptions | ||
| and movie['title'] not in user_watched_titles | ||
| if movie not in recommended_movies] |
There was a problem hiding this comment.
I think that this is a good example of maybe opting to not us list comprehension, you lose out on readability here. Even though not using comprehension takes us a little longer to type out, both ways function the same. We would re-gain the loss readability we lost here.
| def get_new_rec_by_genre(user_data): | ||
| most_watched_genre = get_most_watched_genre(user_data) | ||
|
|
||
| recommended_movie = [movie for movie in get_friends_unique_watched(user_data) | ||
| if movie["genre"] == most_watched_genre] | ||
|
|
||
| return recommended_movie |
There was a problem hiding this comment.
Nice work, the only amendment I might ofter is to have result of get_friends_unique_watched assigned to a variable for readability.
| recommended_movie = [movie for movie in user_data["favorites"] | ||
| if movie["title"] not in my_friends_watched] |
There was a problem hiding this comment.
Love the use of a set here! Gives you a constant look up time!
| assert updated_data == { | ||
| "watchlist": [], | ||
| "watched": [{ | ||
| "title": MOVIE_TITLE_1, | ||
| "genre": GENRE_1, | ||
| "rating": RATING_1 | ||
| }] | ||
| } |
| assert updated_data == { | ||
| "watchlist": [ | ||
| FANTASY_1 | ||
| ], | ||
| "watched": [FANTASY_2, HORROR_1] | ||
| } |
| recommendations = get_new_rec_by_genre(sonyas_data) | ||
|
|
||
| # Assert | ||
| assert recommendations == [] |
mikellewade
left a comment
There was a problem hiding this comment.
Great work, everyone! Feel free to reach out to me if you have any questions the feedback I left!
No description provided.