计算一个数字能拆分计算的次数

编写一个函数persistence,它接受一个正参数num并返回其乘法持久性,即必须将num中的数字相乘到一个数字的次数。这是codewars上的一道习题,感觉挺有意思的。
示例:
persistence(39) === 3; // because 3 * 9 = 27, 2 * 7 = 14, 1 * 4 = 4 and 4 has only one digit
persistence(999) === 4; // because 9 * 9 * 9 = 729, 7 * 2 * 9 = 126, 1 * 2 * 6 = 12, and finally 1 * 2 = 2
persistence(4) === 0; // because 4 is already a one-digit number

代码:

function persistence(int $num): int {
   if($num<10) return 0;
        $split_arr = str_split($num);
        $res = array_product($split_arr);
        return 1+persistence($res);

}
0

程序动态函数库解析

ldd [-vdr] [fildname]

ldd /usr/bin/passwd

    linux-vdso.so.1 =>  (0x00007fffabbc2000)
libuser.so.1 => /lib64/libuser.so.1 (0x00007f72a8c58000)
libgobject-2.0.so.0 => /lib64/libgobject-2.0.so.0 (0x00007f72a8a08000)
libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007f72a86f1000)
libpopt.so.0 => /lib64/libpopt.so.0 (0x00007f72a84e7000)
libpam.so.0 => /lib64/libpam.so.0 (0x00007f72a82d8000)
libpam_misc.so.0 => /lib64/libpam_misc.so.0 (0x00007f72a80d3000)
libaudit.so.1 => /lib64/libaudit.so.1 (0x00007f72a7eaa000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f72a7c83000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f72a7a66000)
libc.so.6 => /lib64/libc.so.6 (0x00007f72a7699000)
libgmodule-2.0.so.0 => /lib64/libgmodule-2.0.so.0 (0x00007f72a7495000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007f72a725d000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f72a6ffb000)
libffi.so.6 => /lib64/libffi.so.6 (0x00007f72a6df3000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f72a6bee000)
libcap-ng.so.0 => /lib64/libcap-ng.so.0 (0x00007f72a69e8000)
/lib64/ld-linux-x86-64.so.2 (0x00005589b1a5b000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007f72a67e4000)
0

call_user_func使用范例

/**
 * Class my_class
 */
class my_class {
    /**
     * @param $name
     */
    static function say_hello($name)
    {
        echo "Hello,".$name."\n";
    }
}

$classname = "my_class";
call_user_func([$classname,'say_hello'],'damon');
call_user_func($classname."::say_hello",'connie');

$myObj = new my_class();
call_user_func([$myObj,'say_hello'],'Jerry');
1+

array_reduce的用法

$employees = [
    [
        'name' => 'damon',
        'age' => 34,
        'email' => 'yank@qq.com',
    ],
    [
        'name' => 'connie',
        'age' => 33,
        'email' => 'connie@qq.com',
    ],
    [
        'name' => 'jerry',
        'age' => 7,
        'email' => 'jerry@qq.com',
    ]
];


$emails = array_reduce($employees,function ($carry,$item){
    $carry[$item['name']] = $item['email'];
    return $carry;
},[]);

dd($emails);

print result:

array:3 [▼
  "damon" => "yank@qq.com"
  "connie" => "connie@qq.com"
  "jerry" => "jerry@qq.com"
]
0

用一层循环找出数组当中两两相加等于100的数

这是昨天碰到的面试题,不得不承认直接想有点想不出来,用代码实现其实还好。

<?php

    $sourceArr = [56,10,20,50,50,70,30,44];
    $tmpArr = [];

    foreach ($sourceArr as $key => $value) {
        if(isset($tmpArr[100 - $value])){
            echo $value.'---'.$tmpArr[100 - $value]."<br/>";
        }else{
            $tmpArr[$value] = $value;
        }
    }

结果:

50---50
30---70
44---56
2+