struct Problem;
struct Solution;
class Skill {
public:
Skill() = default;
Solution Solve(Problem problem);
}
// Implement class to represent an engineer. Each engineer could have multiple skills.
// Implement class to represent a team. Each team could have multiple engineers.
// A team class needs to provide a Solve function: given a problem, whether it could be solved
// and what is the solution. A problem could be solved if there is an engineer in the team that
// could provide a solution to it.
struct Problem;
struct Solution;
class Skill {
public:
Skill() = default;
Solution Solve(Problem problem);
}
// Implement class to represent an engineer. Each engineer could have multiple skills.
// Implement class to represent a team. Each team could have multiple engineers.
// A team class needs to provide a Solve function: given a problem, whether it could be solved
// and what is the solution. A problem could be solved if there is an engineer in the team that
// could provide a solution to it.
// My solution:
class Engineer{
public:
Engineer() = default;
void setSkill(const Skill& skill);
Solution Solve(Problem problem) const;
private:
std::set<Skill> skills;
};
Solution Engineer::Slove(Problem problem) const
{
if(skills.empty())
return Solution::NOSOLUTION;
for(auto i : skills{
Solution solution = i.solve(problem);
if (solution != Solution::NOSOLUTION)
return solution;
}
return Solution::NOSOLUTION;
}
Solution Engineer::setSkill(const Skill& skill){
skills.insert(skill);
}
class Team{
public:
Team() = default;
void addMember(const Engineer&);
SolutionSolve(Problem problem) const;
private:
std::set<Engineer> engineers;
}
void Team::addMember(const Engineer& engeineer){
engineers.insert(engeineer);
}
Solution Team::Slove(Problem problem) const
{
if(engineers.empty())
return Solution::NOSOLUTION;
for(auto i : engineers{
Solution solution = i.solve(problem);
if (solution != Solution::NOSOLUTION)
return solution;
}
return Solution::NOSOLUTION;
}
class Skill 的 Solution Solve(Problem problem); 最后加个 const
class Engineer::Solve 里面的 for loop 就可以改成 for (const auto& i : skills)
public struct Problem { } public struct Solution { } public class Skill { public Solution? Solve(Problem problem) { throw new NotImplementedException(); } } // define a class to represent an engineer. Each engineer could have multiple skills. public class Engineer { public string Name { get; set; } public List<Skill> Skills { get; set; } } // define a class to represent a team. Each team could have multiple engineers. public class Team { public string Name { get; set; } public List<Engineer> Engineers { get; set; } public Solution? Solve(Problem problem) { foreach (var engineer in Engineers) foreach (var skill in engineer.Skills) { var solution = skill.Solve(problem); if (solution != null) return solution; } return null; } }
struct Problem; struct Solution; class Skill { public: Skill() = default; Solution Solve(Problem problem); } // define a class to represent an engineer. Each engineer could have multiple skills. class Engineer { public: Engineer() = default; Engineer(std::string name, std::vector<Skill> skills) : name_(name), skills_(skills) {} std::string name_; std::vector<Skill> skills_; // solve a problem using the engineer's skills Solution Solve(Problem problem) { for (auto skill : skills_) { Solution solution = skill.Solve(problem); if (solution) { return solution; } } return Solution(); } } // define a class to represent a team. Each team could have multiple engineers. class Team { public: Team() = default; Team(std::string name, std::vector<Engineer> engineers) : name_(name), engineers_(engineers) {} std::string name_; std::vector<Engineer> engineers_; // solve a problem using the team's engineers Solution Solve(Problem problem) { for (auto engineer : engineers_) { Solution solution = engineer.Solve(problem); if (solution) { return solution; } } return Solution(); } }
#include gtest plz
class Skill {
public:
enum class SID { NONE,
EAT, SLEEP, BEAT_DOUDOU,
LADY_READ /*hard skill, contact you@are.lying.com if feel confident*/ };
Skill(SID sid) : m_sid(sid) {};
private:
const SID m_sid = SID::NONE;
};
struct Solution {
public:
Solution(bool solved) : m_solved(solved) { };
Solution(const Solution& others) = default;
Solution& operator=(const Solution& others) = default;
bool Solved() const { return m_solved; }
private:
bool m_solved { false };
};
class Problem {
public:
Problem() = delete;
Problem(std::initializer_listconst & skills): m_desiredSkills(skills) { }
struct WorkTable {
WorkTable(size_t size) : m_result(size, false) {}
bool Done() const { return std::find(begin(m_result), end(m_result), false) == end(m_result); }
void SetSolved(size_t index){ m_result[index] = true; }
vector m_result;
};
WorkTable NewWorkTable() const { return WorkTable(m_mustHaveSkills.size()); }
void Research(const Skill::SID sid, WorkTable& worktable) const {
auto itor = m_mustHaveSkills.find(sid);
if(itor != m_mustHaveSkills.end()) {
worktable.SetSolved(abs(std::distance(m_mustHaveSkills.begin(), itor)));
}
}
void Research(const std::unordered_set& skills, WorkTable& worktable) const {
for(auto sid : skills) {
Research(sid, worktable);
}
}
private:
std::unordered_set m_mustHaveSkills;
};
class Engineer {
public:
Engineer() = delete; // no skill? not a engineer.
Engineer(std::initializer_listconst & skills) : m_skills(skills) { };
void Work(const Problem& problem, Problem::WorkTable& worktable) const {
problem.Research(m_skills, worktable);
}
private:
std::unordered_set m_skills;
};
class Team {
public:
Team() = delete;
Team(std::initializer_listconst & engineers) : m_engineers(engineers) { };
Solution Work(const Problem& problem) const {
auto worktable = problem.NewWorkTable();
for(auto& e : m_engineers) {
e.Work(problem, worktable);
if(worktable.Done()) {
return Solution(true);
}
}
return Solution(false);
}
private:
std::vector m_engineers;
};
TEST(Q0007_SIMPLE, TEAM1)
{
Team team_1 {
{ {Skill::SID::EAT} },
{ {Skill::SID::SLEEP } },
{ {Skill::SID::EAT, Skill::SID::SLEEP } },
{ {Skill::SID::BEAT_DOUDOU, Skill::SID::SLEEP } },
};
Problem p1 { Skill::SID::BEAT_DOUDOU };
EXPECT_EQ(team_1.Work(p1).Solved(), true );
Problem p2 { Skill::SID::LADY_READ };
EXPECT_EQ(team_1.Work(p2).Solved(), false );
}