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 ...
随机推荐
- Java输入输出小结
无论使用哪一种编程语言,输入输出都是我们首当其冲的,因此简单整理了 一下关于Java输入输出知识点,还有些内容摘自其它博客,忘见谅. 第一部分,让我们看一下Java的输出 public class M ...
- SharePoint Framework 构建你的第一个web部件(三)
博客地址:http://blog.csdn.net/FoxDave 本篇接上一讲,我们一起来看一下如何部署和测试本地开发的web部件. 在SharePoint中预览web部件 SharePoint ...
- perl代码调试
perl调试教程 一.DESCRIPTIONA (very) lightweight introduction in the use of the perl debugger, and a point ...
- Core 中 Filter 中相关处理
//返回401 ContentResult Content = new ContentResult(); Content.StatusCode = 401; filterContext.Result ...
- Oracle hint手动优化
例子 select/*+FULL(fortest)*/ * from fortest where id = 2000000 //使用0.70s时间 select* from fortest where ...
- ChinaCock界面控件介绍-CCSystemBar
Android 4.4之后谷歌提供了沉浸式全屏体验, 在沉浸式全屏模式下, 状态栏. 虚拟按键动态隐藏, 应用可以使用完整的屏幕空间, 按照 Google 的说法, 给用户一种 身临其境 的体验.而A ...
- ubantu安装python3虚拟环境
Ubuntu安装python3虚拟环境 安装虚拟环境 步骤: 打开Linux终端(快捷键Ctrl+Alt+T),输入命令: sudo apt install python-virtualenv sud ...
- 栈溢出原理与 shellcode 开发
ESP:该指针永远指向系统栈最上面一个栈帧的栈顶 EBP:该指针永远指向系统栈最上面一个栈帧的底部 01 修改函数返回地址 #include<stdio.h> #include< ...
- 2016 多校联赛7 Elegant Construction
Being an ACMer requires knowledge in many fields, because problems in this contest may use physics, ...
- OAuth和OpenID的区别
OAuth关注的是authorization:而OpenID侧重的是authentication.从表面上看,这两个英文单词很容易混淆,但实际上,它们的含义有本质的区别: authorization: ...