Coding kata: get the top two teams in one group
In this week, we did a coding kata, the subject is to select the top two teams of football group match results.
The requirements are listed below.
- The number of team is not limited.
- The score will be +3 when it wins, the score will be 0 if lose, otherwise the score is 1.
- Rule 1: The advanced team are the top two highest score teams.
- Rule 2: if the score is same, the advanced team is that who has the higher net goal.
- Rule 3: if the score and net goal are same, the advanced team is the more goal team.
- Rule 4: select two teams of group to advance
Below is my draft implementation without any comments and unit test after kata as it was not a success solution during the kata, just for record.
#ifndef TEAM_H_
#define TEAM_H_ #include <string> namespace HW
{
/**
* @class TeamC
* @brief
*/
class TeamC
{
public:
/**
* @brief Constructor
*/
TeamC();
TeamC(std::string iName); /**
* @brief Destructor
*/
~TeamC() = default; void AddMatch(uint8_t iScore, uint8_t iGoal, uint8_t iLose);
std::string GetName(void);
int GetScore(void);
int GetNetGoal(void); bool operator<(const TeamC& iValue); private:
std::string mName;
int mScore;
uint8_t mGoal;
uint8_t mLose;
}; } // end of namespace HW #endif /* TEAM_H_ */
#include "Team.h" namespace HW
{ TeamC::TeamC():mName(""), mScore(), mGoal(), mLose()
{ }
TeamC::TeamC(std::string iName):mName(iName), mScore(), mGoal(), mLose()
{ } bool TeamC::operator<(const TeamC& iValue)
{
bool rt = false; //the first rule is to compare score
if (mScore != iValue.mScore)
{
rt = mScore > iValue.mScore;
}
else
{
//if the score is equal, then compare the net goal when net goals are not identical
if (mGoal - mLose != iValue.mGoal - iValue.mLose)
{
rt = mGoal - mLose > iValue.mGoal - iValue.mLose;
}
//only compare the goal
else
{
rt = mGoal > iValue.mGoal;
}
} return rt;
} int TeamC::GetScore(void)
{
return mScore;
} int TeamC::GetNetGoal(void)
{
return mGoal - mLose;
} void TeamC::AddMatch(uint8_t iScore, uint8_t iGoal, uint8_t iLose)
{
mScore += iScore;
mGoal += iGoal;
mLose += iLose;
} std::string TeamC::GetName(void)
{
return mName;
} } // end of namespace HW
#ifndef TEAMSELECTOR_H_
#define TEAMSELECTOR_H_ #include <iostream>
#include <vector>
#include <string>
#include "Team.h" namespace HW
{
/**
* @class ScoreC
* @brief
*/
class TeamSelectorC
{
public:
/**
* @brief Constructor
*/
TeamSelectorC(); /**
* @brief Destructor
*/
~TeamSelectorC() = default; void AddMatch(std::string iTeam1, std::string iTeam2, uint8_t iGoal1, uint8_t iGoal2);
std::vector<std::string> Select(void); /**
* @brief Set copy constructor as delete to prevent unintentional creation
*/
TeamSelectorC(const TeamSelectorC& iValue) = delete; /**
* @brief Set copy assignment as delete to prevent unintentional creation
*/
const TeamSelectorC& operator=(const TeamSelectorC& iValue) = delete;
private:
std::vector<TeamC> mTeams;
private:
TeamC& GetTeam(std::string iTeam);
}; } // end of namespace HW #endif /* TEAMSELECTOR_H_ */
namespace HW
{ TeamSelectorC::TeamSelectorC()
{
} void TeamSelectorC::AddMatch(std::string iTeam1, std::string iTeam2, uint8_t iGoal1, uint8_t iGoal2)
{
TeamC& team1 = GetTeam(iTeam1);
TeamC& team2 = GetTeam(iTeam2); if (iGoal1 > iGoal2)
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
else if (iGoal1 == iGoal2)
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
else
{
team1.AddMatch(, iGoal1, iGoal2);
team2.AddMatch(, iGoal2, iGoal1);
}
} std::vector<std::string> TeamSelectorC::Select(void)
{
std::sort(mTeams.begin(), mTeams.end()); //debug only
for (auto& team: mTeams)
{
std::cout << team.GetName() << "_" << team.GetScore() << "_" << team.GetNetGoal() << std::endl;
} std::vector<std::string> results;
if (mTeams.size() > )
{
results.push_back(mTeams[].GetName());
} if (mTeams.size() > )
{
results.push_back(mTeams[].GetName());
}
return results;
} TeamC& TeamSelectorC::GetTeam(std::string iTeam)
{
for (auto& team: mTeams)
{
if (team.GetName() == iTeam)
{
return team;
}
} TeamC tmp(iTeam);
mTeams.push_back(tmp);
return mTeams[mTeams.size() - ];
} } // end of namespace HW
The test code
void TeamSelectTest(void)
{
HW::TeamSelectorC selector;
selector.AddMatch("A", "B", 2, 1);
selector.AddMatch("A", "C", 2, 1);
selector.AddMatch("A", "D", 2, 1);
selector.AddMatch("B", "C", 3, 1);
selector.AddMatch("B", "D", 2, 1);
selector.AddMatch("C", "D", 3, 1); std::vector<std::string> results = selector.Select();
for (auto result:results)
{
std::cout << result << std::endl;
}
}
Coding kata: get the top two teams in one group的更多相关文章
- Coding Kata - 挑战你的“底线”
Coding Kata简介 如何进行Kata练习 亲身感受 Coding Kata简介 前段时间听到一个比较有意思的概念叫做Coding Kata,今天试了一下来说说一些想法和思考.Kata是一个日语 ...
- UVALive 7147 World Cup(数学+贪心)(2014 Asia Shanghai Regional Contest)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...
- Notepad++ 安装 Zen Coding / Emmet 插件
Zen Coding 插件 ============== 下载: Zen.Coding-Notepad++.v0.7.zip ==Installation== 1. Copy contents of ...
- AssetBundle loading failed because.....已解决
http://blog.csdn.net/ldghd/article/details/9632455 ***************************** 一 ******* ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- POJ 3071 Football
很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比. from——Dinic算法 Football Time ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
- Football(POJ3071)
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3469 Accepted: 1782 Descript ...
- Football
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2882 Accepted: 1466 Descript ...
随机推荐
- Cracking The Coding Interview2.3
#include <iostream> #include <string> using namespace std; class linklist { private: cla ...
- 第三节 java 数组(循环遍历、获取数组的最值(最大值和最小值)、选择排序、冒泡排序、练习控制台输出大写的A)
获取数组的最值(最大值和最小值) 思路: 1.获取最值需要进行比较,每一次比较都会有一个较大的值,因为该 值不确定,需要一个变量进行临储. 2.让数组中的每一个元素都和这个变量中的值进行比较,如果大于 ...
- 使用MyEclipse开发Java EE应用:用XDoclet创建EJB 2 Session Bean项目(二)
[MyEclipse最新版下载] 二.创建一个Session EJB – Part 1 MyEclipse中的EJB 2.x开发使用了EJB向导和集成XDoclet支持的组合. 每个EJB由三个基本部 ...
- Eclipse几点常用设置+个人喜好
1.代码自动提示 在我们忘记方法名或者想偷懒时,代码自动提示很管用.不过Eclipse默认是输入"."后才会出现包或类成员的提示,也就意味着我们必须先输入一个完整的类名,提示才能出 ...
- 3-D models provided some resources
http://d-earth.jamstec.go.jp/GAP_P4/ http://ds.iris.edu/ds/products/emc-earthmodels/
- DRBD常用管理篇
在DRBD进入使用阶段之后,要经常查看它的工作状态,通过这些状态来判断DRBD运行情况. 1) 使用drbd-overview命令观察状态 最为简便的方式就是运行drbd-ove ...
- CentOS安装JDK9
1.使用XShell将下载好的jdk-9.0.1_linux-x64_bin.tar.gz包上传到/opt/下 2.解压文件 $ tar -zxvf jdk-9.0.1_linux-x64_bin.t ...
- RMQ_ST表
]; ]; ]; void init(int n) { int i, j; pwr[] = ; ; i<; ++i) pwr[i] = pwr[i-] << ; ; pwr[j]&l ...
- npm常规命令行集合
最近在摸索vue-cli脚手架的安装,中间用到了一些node的npm命令行,进行了一些整理,并且这个会一直搜集整理更新! 1,常规文件操作命令 cd.. 返回当前文 ...
- 为何linux(包括mac系统)执行指令要加上 ./ ??
比如,现在要在$HIVE_HOME/bin下执行hive指令来启动hive,则该指令的执行顺序如下所示: 1 先找PATH路径 1.1 如果PATH路径下配置了$HIVE_HOME/bin,无论PAT ...