class Mutation
{
private $len = 0;
private $arr = [];
private $table = [];
private $result = [];
public function __construct(array $arr, int $len)
{
$this->len = $len;
$this->setHashTable($arr);
$this->arr = array_unique($arr);
}
private function setHashTable(array $arr)
{
foreach ($arr as $value) {
isset($this->table[$value]) ? $this->table[$value] += 1 : $this->table[$value] = 1;
}
}
private function build()
{
$this->result = array_reduce(array_fill(0, $this->len - 1, $this->arr), function ($previous, $current) {
$result = [];
foreach ($current as $a) {
foreach ($previous as $b) {
if (is_array($b)) {
$values = array_count_values($b);
if (empty($values[$a]) || $this->table[$a] > $values[$a]) {
$result[] = array_merge([$a], $b);
}
} elseif ($a !== $b || $this->table[$a] > 1) {
$result[] = array_merge([$a], [$b]);
}
}
}
return $result;
}, $this->arr);
}
public function print()
{
empty($this->result) && $this->build();
print_r($this->result);
}
public function getCount()
{
empty($this->result) && $this->build();
return count($this->result);
}
}
$mutation = new Mutation([1, 2, 3], 2);
$mutation->print();
echo $mutation->getCount();
const nextBigger = arr => {
const num = arr.join('');
const max = arr.sort((a, b) => b - a).join('');
const tpl = ''.padStart(arr.length, '-')
const check = str => {
arr.forEach(v => str = str.replace(v, '-'))
return str === tpl
}
if (max === num) return -1;
let n = +num;
while (true) {
n += 9
if (check(n.toString())) return n
}
}
const obj = {
something: {
something1: 'value',
something2: 'value1',
something3: {
something4: 'value2'
}
}
}
const getKeys = obj => {
const res = []
for (const key in obj) {
res.push(key)
obj[key] && Object.getPrototypeOf(obj[key]) === Object.prototype && res.push(...getKeys(obj[key]))
}
return res
}
console.log(getKeys(obj))
//[ 'something', 'something1', 'something2', 'something3', 'something4' ]
$json = '{"ops":[{"url":"site.com/100/test"},{"url":"site.com/200/test"}]}';
$json = str_replace([100, 200], [1000, 2000], $json);
echo $json;
//{"ops":[{"url":"site.com/1000/test"},{"url":"site.com/2000/test"}]}