Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package cpecmu.cpe218.sp2019.hw2.submit;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8. import cpecmu.cpe218.sp2019.Pair;
  9. import cpecmu.cpe218.sp2019.hw2.CommanderInspection;
  10.  
  11. public class CommanderInspectionImpl implements CommanderInspection {
  12.  
  13. /**
  14. * input file name
  15. */
  16. protected static final String inFile = "hw2tests/cmdinspect02.in";
  17.  
  18. /*
  19. Might be useful:
  20. If you need to sort a list l, you can use method
  21. Collections.sort(l, comp)
  22. where comp is a Comparator, taking two elements from the list,
  23. and returning a negative integer if the first is "less than" the second,
  24. a positive integer if the first is "greater than" the second,
  25. and zero if they are equal.
  26. Example for comparing two pairs of integers:
  27. Comparator<Pair<Integer, Integer>> comp = (p1, p2) -> {
  28. // Compare first component first.
  29. // If not equal, the lower element is less.
  30. if (p1.fst() != p2.fst()) return p1.fst() - p2.fst();
  31. // If the first components are equal,
  32. // compare the second element.
  33. return p1.snd() - p2.snd();
  34. };
  35. See Java API on java.util.Comparator interface for more details.
  36. */
  37.  
  38. @Override
  39. public Set<Integer> inspectionTimes(List<Pair<Integer, Integer>> shifts) {
  40. // TODO Your code here
  41. List<Pair<Integer, Integer>> availableList = shifts;
  42. List<Pair<Integer, Integer>> temp = new ArrayList<>();
  43. List<Pair<Integer, Integer>> acceptedList = new ArrayList<>();
  44. Comparator<Pair<Integer, Integer>> comp1 = (p1, p2) -> {
  45. if (p1.fst() != p2.fst()) return p1.fst() - p2.fst();
  46. return p1.snd() - p2.snd();
  47. };
  48. Collections.sort(availableList, comp1);
  49. while (!availableList.isEmpty()) {
  50. acceptedList.add(availableList.get(availableList.size() - 1));
  51. availableList.remove(availableList.size() - 1);
  52. Collections.sort(acceptedList, comp1);
  53. temp = new ArrayList<>();
  54. for (int i = 0; i < availableList.size(); i++) {
  55. if (availableList.get(i).snd() < acceptedList.get(0).fst()) {
  56. temp.add(availableList.get(i));
  57. }
  58. }
  59. availableList = temp;
  60. }
  61. Set<Integer> set = new LinkedHashSet<>();
  62. for (int i = 0; i < acceptedList.size(); i++)
  63. set.add(acceptedList.get(i).fst());
  64. return set;
  65. }
  66.  
  67.  
  68. public static void main(String[] args) {
  69. try (FileReader fr = new FileReader(inFile);
  70. Scanner s = new Scanner(fr)) {
  71. // read number of shifts
  72. int n = s.nextInt();
  73. List<Pair<Integer, Integer>> shifts = new ArrayList<>(n);
  74. for (int i = 0; i < n; i++) {
  75. // read shift
  76. shifts.add(new Pair<>(s.nextInt(), s.nextInt()));
  77. }
  78.  
  79. // invoke algorithm
  80. CommanderInspection sbm = new CommanderInspectionImpl();
  81. Set<Integer> res = sbm.inspectionTimes(shifts);
  82. System.out.println("We need to do the minimum of " + res.size() + " inspection.");
  83. System.out.print("One possible way is to inspect at time ");
  84. System.out.println(res);
  85. } catch (FileNotFoundException e) {
  86. e.printStackTrace();
  87. } catch (IOException e) {
  88. e.printStackTrace();
  89. }
  90. }
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement