Skip to content

Commit

Permalink
Change example solution to accomodate new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Elahi Concha committed Dec 11, 2024
1 parent ee3f741 commit 0f67a9f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
37 changes: 21 additions & 16 deletions exercises/practice/grade-school/.meta/example.cpp
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

}
32 changes: 15 additions & 17 deletions exercises/practice/grade-school/.meta/example.h
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

0 comments on commit 0f67a9f

Please sign in to comment.