This repository acts as a personal archive for my solutions to EdX course *Data Structures and Software Design* from PennX.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.4 KiB

  1. /*
  2. * SD2x Homework #5
  3. * Implement the methods below according to the specification in the assignment description.
  4. * Please be sure not to change the method signatures!
  5. */
  6. import java.util.List;
  7. import java.util.PriorityQueue;
  8. import java.util.TreeMap;
  9. import java.util.Map.Entry;
  10. import java.util.Set;
  11. import java.util.Stack;
  12. import java.util.LinkedList;
  13. public class MovieRatingsProcessor {
  14. public static List<String> getAlphabeticalMovies(TreeMap<String, PriorityQueue<Integer>> movieRatings) {
  15. if(movieRatings == null) return new LinkedList<String>();
  16. return new LinkedList<String>(movieRatings.keySet()); // Normally sets aren't ordered but the docs say the returned set is ordered and it works sooo. \_(._.)_/
  17. }
  18. public static List<String> getAlphabeticalMoviesAboveRating(TreeMap<String, PriorityQueue<Integer>> movieRatings, int rating) {
  19. List<String> movies = new LinkedList<String>();
  20. if(movieRatings == null) return movies;
  21. Set<Entry<String, PriorityQueue<Integer>>> entries = movieRatings.entrySet();
  22. for(Entry<String, PriorityQueue<Integer>> e : entries){
  23. if(e.getValue().peek() > rating) movies.add(e.getKey());
  24. }
  25. return movies;
  26. }
  27. public static TreeMap<String, Integer> removeAllRatingsBelow(TreeMap<String, PriorityQueue<Integer>> movieRatings, int rating) {
  28. TreeMap<String, Integer> removed = new TreeMap<String, Integer>();
  29. if(movieRatings == null) return removed;
  30. int count;
  31. PriorityQueue<Integer> value;
  32. LinkedList<String> deleted = new LinkedList<String>();
  33. Integer[] foo = new Integer[1];
  34. for(String n : movieRatings.keySet()){
  35. value = movieRatings.get(n);
  36. if(value.peek() >= rating){
  37. continue;
  38. }
  39. Integer[] ratings = value.toArray(foo);
  40. count = 0;
  41. for(int i = 0; i < ratings.length; i++){
  42. if(ratings[i] < rating){
  43. value.remove(ratings[i]);
  44. count++;
  45. }
  46. }
  47. removed.put(n, count);
  48. if(count == ratings.length){
  49. deleted.add(n);
  50. }
  51. }
  52. for(String n : deleted)
  53. movieRatings.remove(n);
  54. return removed;
  55. }
  56. }