一、判断给定的一个不限长的数字串大小变化趋势
自制了一道面试题:给定一个不限长的数字字符串,判断每一位数字的大小变化趋势是否是^或v趋势,如果是就返回true,如果不是就返回false。比如121即属于^,322129即属于v。这是一道相对较简单的笔试题,不过可以看看应聘者的动手能力和思考能力。代码示例如下:
<?php
#给定一个不限长的数字字符串,判断每一位数字的大小变化趋势是否是^或v趋势
$args = getopt('a:');
extract($args);
echo "入参:a{$a}\n";function checkOne($str){$arrow = false;$level = 0;for($i = 1; $i<strlen($str); $i++){$char = $str{$i};if(!is_numeric($char)){return false;}else{if($char > $str{$i-1}){$newarrow = 'up';}elseif($char <$str{$i-1}){$newarrow = 'down';} } if($arrow != $newarrow){$arrow = $newarrow;$level ++; } } if($level >2 ) return false;return true;
}var_dump(checkOne($a));
从代码中可以看到,主要的思路就是判断数字大小趋势发生转折的点,判断有几次转折。因此这道题也可以改成判断数字串的大小变化趋势是否是^v^v这种等,代码上难度没有什么增加,不过在正解及由问题转化成思路的难度上对应聘者会有一点增加。示例代码运行结果如下:
[root@04007 php]# php line.php -a=123578954
入参:a123578954
bool(true)
[root@04007 php]# php line.php -a=12357895478
入参:a12357895478
bool(false)
[root@04007 php]# php line.php -a=1239965331
入参:a1239965331
bool(true)
[root@04007 php]# php line.php -a=1239a965331
入参:a1239a965331
bool(false)
二、经典面试题:一群猴子排成一圈,数到第m只,把它踢出圈,求最后剩下的那只大王编号
题目:一群猴子排成一圈,按1,2,…,n依次编号。然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫做大王。要求编程模拟此过程,输入m、n, 输出最后那个大王的编号。使用程序模拟该过程。
#. 问题解决方法:解答1:
构建一个n个元素的数组,按顺序进行剔除,超过末尾则进行从头开始,在每次删除元素后重置一下数组。实现如下:
#经典面试题:一群猴子排成一圈,数到第m只,把它踢出圈,求最后剩下的那只大王编号
#007.cn$args = getopt('a:m:');
extract($args);
echo "入参:a{$a}, m:{$m} 求大王编号->\n";function lastMonkey($a, $m){$monkey = range(1,$a);echo "初始编号:",implode(',', $monkey),"\n";$index =0; #猴子数量大于1就一直进行剔除$cycle = 1;while(count($monkey) > 1){ $index += $m-1;if($index>count($monkey)-1){$index = $index%(count($monkey));} unset($monkey[$index]);$monkey = array_values($monkey);echo "第{$cycle}轮.剔除后编号:",implode(',', $monkey),"\n";$cycle++;usleep(300);} return current($monkey);
}echo "最终大王编号:". lastMonkey($a, $m)."\n\n";
#. 问题解决方法:解答2:
构建一个n个元素的数组,不从头开始,但将不剔除的元素放至最后,直至只剩一个元素,实现如下:
#经典面试题:一群猴子排成一圈,数到第m只,把它踢出圈,求最后剩下的那只大王编号
#04007.cnecho "入参:a{$a}, m:{$m} 求大王编号->\n";
function domonkey($a ,$m){$monkey = range(1, $a);echo "初始编号:",implode(',', $monkey),"\n";$i = 1;while(count($monkey) > 1){ #只要不是m的倍数就放至队列最后,否则就剔除if( $i % $m ){array_push($monkey, array_shift($monkey));}else{array_shift($monkey);echo '第'.($i/3)."轮.剔除后编号:",implode(',', $monkey),"\n";} $cycle++;$i++;} return current($monkey);
}
echo '大王编号:'.domonkey($a,$m), "\n";
#. 问题解决方法:执行结果
在服务器上执行php脚本命令及输出过程截图如下:
三、Debian服务器php中安装IMAP扩展各种报错解决过程
因业务中需要,我需要在php中执行QQ邮箱登录并拿回QQ邮箱中的邮件。服务器是DEBIAN服务器,环境是docker安装的php环境。在PHP中使用imap_open函数打开邮箱连接(记得在邮件设置中打开IMAP功能)。执行imap_open函数需要安装php_imap扩展,如果是windows下去除php_imap.dll扩展注释。
Fatal error: Uncaught Error: Call to undefined function imap_open() in D:\mail.php:10 Stack trace: #0 {main} thrown Who works with NT systems, can open the file "\xampp\php\php.ini" to active the php exstension by removing the beginning semicolon at the line ";extension=php_imap.dll". Should be: extension=php_imap.dll
但在Debian服务器中的docker环境下安装imap扩展还是遇到了一麻烦。直接进docker下php容器,开始安装。报错:configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.
root@2123245efdr5:/var/www# docker-php-ext-install imap
checking for IMAP Kerberos support... no
checking for IMAP SSL support... no
checking for utf8_mime2text signature... new
checking for U8T_DECOMPOSE...
configure: error: utf8_mime2text() has new signature, but U8T_CANONICAL is missing. This should not happen. Check config.log for additional information.
找了一下需要安装libc-client-devel模块,不过找了找发现这个模块名称只是适用于centos等,对Debian服务器没法安装,报错E: Unable to locate package libc-client-devel,因为根本不叫这名字。Debian -- Error 这里有说明这个包在debian下的完整名称。也可以使用apt-cache search libc-client搜索相关的包,发现需要使用ibc-client2007e-dev。
#在debian服务器下执行apt-get install libc-client-devel会报无法找到包文件
user@n123:~$ sudo apt-get install libc-client-devel
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package libc-client-devel
#通过apt-cache search libc-client搜索查找
user@n123:~$ sudo apt-cache search libc-client
libc-client2007e - c-client library for mail protocols - library files
libc-client2007e-dev - c-client library for mail protocols - development files
uw-mailutils - c-client support programs
#需要使用如下
root@4ba2159bfcd0:/var/www# apt-get install libc-client2007e-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
......
After this operation, 3816 kB of additional disk space will be used.
Do you want to continue? [Y/n] y......
但仍然没有结束,安装时还遇到报错:configure: error: This c-client library is built with Kerberos support.Add --with-kerberos to your configure line. Check config.log for details.看提示是让我们在后面加一些选项,于是增加选项再执行安装.
root@4009lki4434dd:/var/www# docker-php-ext-install imap
checking for utf8_mime2text signature... new
checking for U8T_DECOMPOSE...
checking for pam_start in -lpam... yes
checking for crypt in -lcrypt... yes
configure: error: This c-client library is built with Kerberos support.Add --with-kerberos to your configure line. Check config.log for details.
#提示安装时添加--with-kerberos,增加选项再执行安装
root@4009lki4434dd:/var/www# docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap
checking for crypt in -lcrypt... yes
checking for krb5-config... no
configure: error: Kerberos libraries not found.Check the path given to --with-kerberos (if no path is given, searches in /usr/kerberos, /usr/local and /usr )
选项是加了,但看提示是加的选项没有作用,因为默认的路径里找不到Kerberos libraries,此原因是因为没有安装Kerberos libraries。于是执行安装libkrb5-dev库,这一步成功之后再进行安装imap就成功了。
#安装libkrb5-dev库
root@4009lki4434dd:/var/www# apt-get install libkrb5-dev
#最后执行安装imap安装成功:
root@4009lki4434dd:/var/www# docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap
#如果已经安装了也可尝试使用下面的命令指定kerberos库的目录
docker-php-ext-configure imap --with-kerberos-inc=/usr/include --with-imap-ssl && docker-php-ext-install imap