Question: ( write this code over so it has a different structure and it logically makes sense.: vector RecommendationSystem::generateRecommendationsForStudent ( const string& studentName ) const {

(write this code over so it has a different structure and it logically makes sense.: vector RecommendationSystem::generateRecommendationsForStudent(const string& studentName) const {
vector recommendations;
if (students.find(studentName)== students.end()){
return recommendations; // Return empty if student not found
}
unordered_set enrolledCourses;
for (const auto& [course, studentMap] : coursePopularity){
if (studentMap.count(studentName)){
enrolledCourses.insert(course);
}
}
map recommendationScores;
for (const auto& [course, studentMap] : coursePopularity){
if (enrolledCourses.count(course)) continue; // Skip courses already enrolled
int score =0;
bool isUnique = false;
for (const auto& [otherStudent, count] : studentMap){
for (const auto& enrolled : enrolledCourses){
if (coursePopularity.at(enrolled).count(otherStudent)){
score += count;
isUnique = true;
break;
}
}
}
if (isUnique){
recommendationScores[course]= score;
}
}
vector> sortedRecommendations;
for (const auto& [course, score] : recommendationScores){
sortedRecommendations.emplace_back(-score, course); // Use negative score for descending order
}
sort(sortedRecommendations.begin(), sortedRecommendations.end());
for (const auto& [negScore, course] : sortedRecommendations){
if (recommendations.size()<3){
recommendations.push_back(course);
} else {
break;
}
}
return recommendations;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!