-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change example solution to accomodate new test cases
- Loading branch information
Elahi Concha
committed
Dec 11, 2024
1 parent
ee3f741
commit 0f67a9f
Showing
2 changed files
with
36 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
#include "grade_school.h" | ||
#include <algorithm> | ||
|
||
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<std::string>& 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<std::string> school::grade(int grade) const | ||
{ | ||
auto it = roster_.find(grade); | ||
return (it != roster_.end()) ? it->second : std::vector<std::string>{}; | ||
} | ||
|
||
void school::add(string const& name, int grade) | ||
{ | ||
vector<string>& grade_roster = roster_[grade]; | ||
auto it = lower_bound(grade_roster.begin(), grade_roster.end(), name); | ||
grade_roster.insert(it, name); | ||
} | ||
|
||
vector<string> school::grade(int grade) const | ||
{ | ||
auto it = roster_.find(grade); | ||
return (it != roster_.end()) ? it->second : vector<string>{}; | ||
} | ||
} // namespace grade_school | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
#if !defined(GRADE_SCHOOL_H) | ||
#define GRADE_SCHOOL_H | ||
|
||
#include <map> | ||
#include <string> | ||
#include <set> | ||
#include <vector> | ||
#include <string> | ||
#include <map> | ||
|
||
namespace grade_school | ||
{ | ||
|
||
class school | ||
{ | ||
public: | ||
const std::map<int, std::vector<std::string>>& roster() const { | ||
return roster_; | ||
} | ||
class school | ||
{ | ||
public: | ||
const std::map<int, std::vector<std::string>>& roster() const { | ||
return roster_; | ||
} | ||
|
||
void add(std::string const& name, int grade); | ||
void add(std::string const& name, int grade); | ||
|
||
std::vector<std::string> grade(int grade) const; | ||
std::vector<std::string> grade(int grade) const; | ||
|
||
private: | ||
std::map<int, std::vector<std::string>> roster_; | ||
}; | ||
private: | ||
std::map<int, std::vector<std::string>> roster_; | ||
std::set<std::string> students_; // New member to track existing students | ||
}; | ||
|
||
} | ||
|
||
#endif |