Home > Mobile >  Build a list of the names of applicants who have scores strictly greater than the given threshold
Build a list of the names of applicants who have scores strictly greater than the given threshold

Time:01-28

I'm having trouble getting the logic down for adding values that are only greater than the score threshold.

I've tried multiple things like nested if statements and while loops but I can't seem to get it down. I have to return a list of names of students whose scores are all above a certain threshold.

type hereimport java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class ExampleMap {

  /**
   * Return a list of "high scoring" students --- High scoring students are
   * students who have all grades strictly greater than the given threshold.
   *
   * @param scoresByApplicantName A map of applicant names to applicant scores
   * @param scoreThreshold        The score threshold
   * @return The list of high-scoring applicant names
   */
  public static List<String> highScoringStudents(Map<String, List<CourseGrade>> scoresByApplicantName, int scoreThreshold) {

    List<String> highScoringStudents = new LinkedList<>();

    /*
     * Build a list of the names of applicants who have scores strictly greater than
     * the given threshold.
     */
    for (Map.Entry<String, List<CourseGrade>> current : scoresByApplicantName.entrySet()) {
      List<CourseGrade> course_grades = current.getValue();
      for (CourseGrade courses : course_grades) {
        if (courses.getScore() > scoreThreshold) {
          highScoringStudents.add(current.getKey());
        }
      }
    }
    return highScoringStudents;
  }
}

the course grade class

/*
 * This file should remain unchanged.
 */
public class CourseGrade {
  private String courseName;
  private int score; 

  /**
   * This is a constructor. It creates a new CourseGrade with the specified scores.
   * @param courseName
   * @param score 
   */
  public CourseGrade(String courseName, int score) {
    this.courseName = courseName;
    this.score = score;
  }

  public String getCourseName() {
    return this.courseName;
  }

  public int getScore() {
    return this.score;
  }
}

test to be ran

  @Test
   public void testExampleMap1()
   {
      Map<String, List<CourseGrade>> courseListsByStudent = new HashMap<>();
      courseListsByStudent.put("Julie",
         Arrays.asList(
            new CourseGrade("CPE 123", 89),
            new CourseGrade("CPE 101", 90),
            new CourseGrade("CPE 202", 99),
            new CourseGrade("CPE 203", 100),
            new CourseGrade("CPE 225", 89)));
      courseListsByStudent.put("Paul",
         Arrays.asList(
            new CourseGrade("CPE 101", 86),
            new CourseGrade("CPE 202", 80),
            new CourseGrade("CPE 203", 76),
            new CourseGrade("CPE 225", 80)));
      courseListsByStudent.put("Zoe",
         Arrays.asList(
            new CourseGrade("CPE 123", 99),
            new CourseGrade("CPE 203", 91),
            new CourseGrade("CPE 471", 86),
            new CourseGrade("CPE 473", 90),
            new CourseGrade("CPE 476", 99),
            new CourseGrade("CPE 572", 100)));

      List<String> expected = Arrays.asList("Julie", "Zoe");

      /*
       * Why compare HashSets here?  Just so that the order of the
       * elements in the list is not important for this test.
       */
      assertEquals(new HashSet<>(expected),
         new HashSet<>(ExampleMap.highScoringStudents(
            courseListsByStudent, 85)));
   }

I've tried different if statements but I've always only been able to either add the student who is under the score threshold or only all 3.

,,,,,,,,,,,,,,,,,,,

CodePudding user response:

Use a boolean flag to store whether or not the student has a higher grade than the threshold in ALL courses.

boolean pass = true;
for (CourseGrade courses : course_grades) 
    if (courses.getScore() <= scoreThreshold) {
        pass = false;
        break;
    }
if (pass) highScoringStudents.add(current.getKey());

This can be written more concisely using Streams.

return scoresByApplicantName.entrySet().stream().filter(e -> e.getValue().stream()
          .allMatch(course -> course.getScore() > scoreThreshold))
          .map(Map.Entry::getKey).collect(Collectors.toList());
  •  Tags:  
  • java
  • Related