谢谢大家对楼赛的支持,下面带来本期「PHP挑战赛」的标准答案。
未参加过比赛的同学,请先自己动手挑战一下题目,点击进入楼赛 。
4月16日 19:30,我们在实验楼的 B站直播间 给大家带来了题目讲解,如果错过也没有关系,点这里可以看 录屏 。
第一题
if ($argc < 3) {
exit('Parameter Error');
}
array_shift($argv);
$count = count($argv);
$half = [];
foreach($argv as $k => $v) {
$len = ($count - $k) * 2 - 1;
//左边补全
$i = 0;
$line = '';
while($i < $k) {
$line .= $argv[$i];
$i++;
}
while($len > 0) {
$line .= $v;
$len--;
}
//右边补全
$j = $k-1;
while(isset($argv[$j])) {
$line .= $argv[$j];
$j--;
}
$half[] =$line.PHP_EOL;
}
$res = array_merge($half,array_slice(array_reverse($half),1));
echo implode($res,'');
第二题
babadbdddadbdccbadbdbdbdddaaba
第三题
class Orm extends Model
{
protected function find()
{
return $this;
}
public function select($select)
{
if (is_array($select)) {
$select = implode(',',$select);
}
$this->_select = trim($select);
return $this;
}
public function one()
{
$this->_limit = ' LIMIT 1';
return current($this->get());
}
public function all()
{
return $this->get();
}
public function and($where)
{
$this->_where .= $this->setCondition($where,'AND');
return $this;
}
public function or($where)
{
$this->_where .= $this->setCondition($where,'OR');
return $this;
}
public function where($where)
{
$this->_where = $this->setCondition($where,'WHERE');
return $this;
}
private function setCondition($where,$type)
{
if (is_string($where)) {
return " $type $where";
}
if (!is_array($where) || count($where) != 3) {
Throw new Exception("Invalid where condition");
}
$keyword = strtoupper($where[0]);
$field = $where[1];
$value = $this->checkValue($where[2]);
switch($keyword) {
case 'BETWEEN':
if (!is_array($value) || count($value) != 2) {
Throw new Exception("Invalid value in between");
}
$value = $value[0].' and '.$value[1];
break;
default:
if (is_array($value)) {
$value = '('.trim(implode($value,','),',').')';
}
}
return " $type $field $keyword $value";
}
private function checkValue($value)
{
if (is_array($value)) {
foreach($value as $k => $v) {
$value[$k] = is_numeric($v) ? $v : "'$v'";
}
} else {
$value = is_numeric($value) ? $value : "'$value'";
}
return $value;
}
}