c# - Implement a function to return top rated movies in the network of movie -
given class below:
public class movie { private readonly char movieid; private readonly float rating; private list<movie> similarmovies; // similarity bidirectional public movie(char movieid, float rating) { this.movieid = movieid; this.rating = rating; similarmovies = new list<movie>(); } public char getid() { return movieid; } public float getrating() { return rating; } public void addsimilarmovie(movie movie) { similarmovies.add(movie); movie.similarmovies.add(this); } public list<movie> getsimilarmovies() { return similarmovies; } public static ilist<movie> getmovierecommendations(movie movie, int numtopratedsimilarmovies) { //implement method return null; } }
reachable current movie eg: a(rating 1.2) / \ b(2.4) c(3.6) \ / d(4.8)in above example edges represent similarity , number rating. getmovierecommendations(a,2)should return c , d (sorting order doesn't matter can return d , c) getmovierecommendations(a,4) should return a, b, c, d (it can return these in order eg: b,c,d,a) getmovierecommendations(a,1) should return d. note distance d doesn't matter
public static hashset<movie> sd = new hashset<movie>(); public static ilist<movie> getmovierecommendations(movie movie, int numtopratedsimilarmovies) { sd.add(movie); foreach (movie m in movie.getsimilarmovies()) { if (!sd.contains(m)) getmovierecommendations(m, -1); } if(numtopratedsimilarmovies > -1) return sd.orderbydescending(o => o.rating).take(numtopratedsimilarmovies).tolist(); return null; }
Comments
Post a Comment