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 ...
随机推荐
- 7.3 C++模板中的函数式参数
参考:http://www.weixueyuan.net/view/6400.html 总结: 模板类至少有一个类参数,但是可以有多个参数,这些参数中可以存在非类类型的参数. 类参数是指 class ...
- 1.4socket服务器打印信息的四种不同方式()
方式一 socker 服务器 # -*- coding: utf-8 -*- import sys,os,multiprocessing from socket import * serverHost ...
- h5视频配置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- synchronized 和 ReentrantLock 区别
synchronized 使用: 1:当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁.结果,其它线程对该object对象所有同步代 ...
- POJ 2001 Shortest Prefixes(字典树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...
- 前端笔记 (3.JavaScript 1)
JavaScript 是属于网络的脚本语言! JavaScript 是一种轻量级的编程语言. JavaScript 是可插入 HTML 页面的编程代码. JavaScript 插入 HTML 页面后, ...
- MySQL篇,第二章:数据库知识2
MySQL 数据库 2 名词介绍 1.DB(Database) DB就是数据库,存储数据的仓库 2.DBMS(Database Management System) 数据库管理系统 管理数据库的软件, ...
- 二叉树求逆序对(伪AC 23333)
成链的时候 是最坏情况 O(n^2)的复杂度呢! 按照输入的数据 一个一个的插入建树 然后维护左右儿子的个数 (我们规定, 左儿子 小于 父亲 右儿子大于父亲) 往左走 说明存在逆序对 逆序对的 ...
- linux 禁ping和开启ping方法
Linux 禁ping和开启ping操作# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all如果要恢复,只要:# echo 0 > /pro ...
- Django中的Templates
1.定义: 定义和flask框架中的是一样的,唯一的不同就是Django中有自己的模板引擎,并非Jinja2,因此有一些不同之处. 2.模板的设置 在 settings.py 中 设置 TEMPLAT ...