From 0f67a9f7526b956484f8dce44c937614db815f9e Mon Sep 17 00:00:00 2001 From: Elahi Concha Date: Wed, 11 Dec 2024 16:28:42 +0100 Subject: [PATCH] Change example solution to accomodate new test cases --- .../practice/grade-school/.meta/example.cpp | 37 +++++++++++-------- .../practice/grade-school/.meta/example.h | 32 ++++++++-------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/exercises/practice/grade-school/.meta/example.cpp b/exercises/practice/grade-school/.meta/example.cpp index 72d7f7bb3..606afc8d8 100644 --- a/exercises/practice/grade-school/.meta/example.cpp +++ b/exercises/practice/grade-school/.meta/example.cpp @@ -1,22 +1,27 @@ #include "grade_school.h" -#include -using namespace std; +namespace grade_school { + void school::add(std::string const& name, int grade) { + // Check if the student already exists + if (students_.find(name) != students_.end()) + { + // Student already exists; do not add + return; + } + // Add the student to the set of students + students_.insert(name); + // Proceed to add the student to the given grade in sorted order + std::vector& grade_roster = roster_[grade]; + auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); + grade_roster.insert(it, name); + } -namespace grade_school -{ + std::vector school::grade(int grade) const + { + auto it = roster_.find(grade); + return (it != roster_.end()) ? it->second : std::vector{}; + } -void school::add(string const& name, int grade) -{ - vector& grade_roster = roster_[grade]; - auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); - grade_roster.insert(it, name); -} -vector school::grade(int grade) const -{ - auto it = roster_.find(grade); - return (it != roster_.end()) ? it->second : vector{}; -} +} // namespace grade_school -} diff --git a/exercises/practice/grade-school/.meta/example.h b/exercises/practice/grade-school/.meta/example.h index 9a6baf692..cc0f19cd7 100644 --- a/exercises/practice/grade-school/.meta/example.h +++ b/exercises/practice/grade-school/.meta/example.h @@ -1,28 +1,26 @@ -#if !defined(GRADE_SCHOOL_H) -#define GRADE_SCHOOL_H - -#include -#include +#include #include +#include +#include namespace grade_school { -class school -{ -public: - const std::map>& roster() const { - return roster_; - } + class school + { + public: + const std::map>& roster() const { + return roster_; + } - void add(std::string const& name, int grade); + void add(std::string const& name, int grade); - std::vector grade(int grade) const; + std::vector grade(int grade) const; -private: - std::map> roster_; -}; + private: + std::map> roster_; + std::set students_; // New member to track existing students + }; } -#endif