Coding kata: get the top two teams in one group

时间:2021-02-25 09:41:51

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的更多相关文章

  1. Coding Kata - 挑战你的&OpenCurlyDoubleQuote;底线”

    Coding Kata简介 如何进行Kata练习 亲身感受 Coding Kata简介 前段时间听到一个比较有意思的概念叫做Coding Kata,今天试了一下来说说一些想法和思考.Kata是一个日语 ...

  2. UVALive 7147 World Cup(数学&plus;贪心)(2014 Asia Shanghai Regional Contest)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=6 ...

  3. Notepad&plus;&plus; 安装 Zen Coding &sol; Emmet 插件

    Zen Coding 插件 ============== 下载: Zen.Coding-Notepad++.v0.7.zip ==Installation== 1. Copy contents of ...

  4. AssetBundle loading failed because&period;&period;&period;&period;&period;已解决

    http://blog.csdn.net/ldghd/article/details/9632455 *****************************      一      ******* ...

  5. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  6. POJ 3071 Football

    很久以前就见过的...最基本的概率DP...除法配合位运算可以很容易的判断下一场要和谁比.    from——Dinic算法                         Football Time ...

  7. sdut 2162&colon;The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  8. Football(POJ3071)

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3469   Accepted: 1782 Descript ...

  9. Football

    Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2882   Accepted: 1466 Descript ...

随机推荐

  1. PL&sol;SQL配置Oracle数据库路径

    打开PL/SQL-Tools->Preferences-Orcacle->Connecttion 找到配置路径,打开-product\instantclient_11_2\NETWORK\ ...

  2. Appium 关于如何获取 apk 包名即 appPackage 和 appActivity

    方法2: a.启动待测apk b.开启日志输出:adb logcat>D:/log.txt c.关闭日志输出:ctrl+c d.查看日志 进入通过adb 连接上设备以后,通过如下命令查看包名 C ...

  3. SQL 行列转换简单示例

    SQLSERVER 2005 以后提供了新的方式进行行列转换,下面是一个实例供参考: if object_id('tb') is not null drop table tbTest go ),季度 ...

  4. codeforces 476C&period;Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  5. 字符流缓冲区BufferedReader之readLine方法的原理

  6. 数据库中的索引Index

    索引就像一本书的目录,而书中的索引是对一个词语的列表,其中注明了包含各个词的页码.数据库中的索引 是某一个表中一列或者若干列值的集合和相应的只想表中物理标识这些值的数据页的逻辑指针清单. 索引的作用: ...

  7. 动态链接库的生成(dll)和 动态链接库隐式and显式调用

    一.构建动态链接库(dll.dll dll.lib dll.h) 说明: .dll 是在执行程序是调用  .lib 是在连接程序是调用  .h是在编译程序时调用 1.头文件(声明导入函数):_decl ...

  8. ioremap&lowbar;nocache&lpar;&rpar; 函数的使用【转】

    本篇文章主要是在ioremap_nocache函数说明的基础上进行整理,加入该函数的用法简介. 函数原型 void __iomem * ioremap_nocache (unsigned long o ...

  9. Hadoop2&period;7&period;7&lowbar;HA高可用部署

    1. Hadoop的HA机制 前言:正式引入HA机制是从hadoop2.0开始,之前的版本中没有HA机制 1.1. HA的运作机制 (1)hadoop-HA集群运作机制介绍 所谓HA,即高可用(7*2 ...

  10. Build Tool

    building tool: 一.building tools 为什么主流? Gradle 是目前比较流行的构建工具之一,Android Studio 中集成的就是 Gradle,并针对 Androi ...