Advertisement
satishfrontenddev4

Untitled

Jan 6th, 2024
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. You are given a list of marks over 5 subjects scored by n students. You need to sort the student by following rules.
  3.  
  4. The student whose sum of marks will be highest must come at the top of the leaderboard.
  5.  
  6. Students whose total sum of marks are the same will be sorted alphabetically ascending by name.
  7.  
  8. In all other cases, student with lesser id will come before in the leaderboard
  9.  
  10. Input format
  11. First line will contain a single integer n number of students.
  12.  
  13. Next n lines will contain space separated unique integer Id, string S name of the student followed by 5 integers marks scored out of 100.
  14.  
  15. Output format
  16. Print space separated list of sorted ids of the student in a single line
  17.  
  18. Sample Input 1
  19. 3
  20.  
  21. 1 Alan 45 56 21 32 74
  22.  
  23. 2 Fabien 95 94 93 92 91
  24.  
  25. 3 Gamora 85 65 54 65 76
  26.  
  27. Sample Output 1
  28. 2 3 1
  29.  
  30. Explanation 1
  31. Fabien has the most marks obtained and then Gamora and followed by Alan
  32.  
  33. Constraints
  34. 1<=n<=50000
  35.  
  36. 1<=|S|<=10
  37.  
  38. Name of the Student will be in alphabetically lowercase characters.
  39.  
  40. 1<=Marks[i]<=100
  41.  
  42. 1<=Student id<=10^7
  43.  
  44. Student id will be unique for each student
  45. */
  46.  
  47. /**
  48.  * @param {number} n
  49.  * @return {number[]}
  50.  */
  51. function marksSort(n, students) {
  52.  
  53.   students.map(student=>{
  54.     let totalMarks=0;
  55.     student.slice(2).map(mark=>totalMarks=totalMarks+mark)
  56.     student.push(totalMarks);
  57.   })
  58.   // console.log(students)
  59.   // --ive -> ascending order
  60.   students.sort((student1,student2)=>{
  61.     const totalMarksComparision=student2[7]-student1[7];
  62.       if(totalMarksComparision==0){
  63.         const nameComparision=student1[1].localeCompare(student2[1]);// - , + , 0
  64.          if(nameComparision ==0){
  65.             return student1[0]-student2[0];
  66.          }
  67.          else
  68.          return nameComparision;
  69.       }
  70.       else
  71.       return totalMarksComparision;
  72.      
  73.   })
  74.   let sortedId= [...students.map(student=>student.slice(0,1))];
  75.   return sortedId;
  76. }
  77. /*
  78. That's a great question. The reason for the difference in ordering between `student1[1].localeCompare(student2[1])` and `student2[7] - student1[7]` is related to the desired sorting order for each comparison.
  79.  
  80. 1. **Name Comparison (`student1[1].localeCompare(student2[1])`):** In this case, we use `student1[1].localeCompare(student2[1])` to compare names alphabetically. The standard behavior of `localeCompare` is to return a negative value when the calling string (in this case, `student1[1]`) comes before the argument string (`student2[1]`) in alphabetical order. So, `student1[1].localeCompare(student2[1])` ensures that names are sorted in ascending order (A to Z).
  81.  
  82. 2. **Total Marks Comparison (`student2[7] - student1[7]`):** In this case, we use `student2[7] - student1[7]` to compare total marks. When we subtract `student1[7]` from `student2[7]`, the result is negative if `student1[7]` is less than `student2[7]`. Therefore, this comparison ensures that total marks are sorted in descending order (highest marks first).
  83.  
  84. The key difference is the desired sorting order for each comparison:
  85.  
  86. - For names, we want ascending alphabetical order, so we use `student1[1].localeCompare(student2[1])`.
  87.  
  88. - For total marks, we want descending order (highest marks first), so we use `student2[7] - student1[7]`.
  89.  
  90. These differences reflect the specific sorting criteria mentioned in your original question.
  91. */
  92.  
  93. function main() {
  94.   let n = parseInt(readLine());
  95.   let students = [];
  96.  
  97.   for (let i = 0; i < n; i++) {
  98.     let student = readLine().split(" ");
  99.     for (let j in student) {
  100.       if (j != 1) student[j] = parseInt(student[j]);
  101.     }
  102.     students.push(student);
  103.   }
  104.  
  105.   let sortedId = marksSort(n, students);
  106.   print(sortedId.join(" "));
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement