实验楼Linux挑战赛答案,楼赛第18期——「PHP挑战赛」标准答案公布

谢谢大家对楼赛的支持,下面带来本期「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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值