$arr = [0 => 'a', 1 => 'b', 2 => 'c'];
$res = [];
for ($i = 1; $i < pow(2, count($arr)); $i++ ) {
$bin = decbin($i);
$case = "";
foreach(str_split(strrev($bin)) as $ind => $symb) if ($symb == "1") $case .= $arr[$ind];
$res[] = $case;
}
echo implode(", ", $res); // a, b, ab, c, ac, bc, abc
let customKeys
const items = Array.from(['custom', 'default']).flatMap(group => {
const scope = group + "Data"
// Собираем элементы группы
const items = (data[group] || []).flatMap(({ id, links }) =>
links.map(link => ({...link, id, scope }))
)
const getKey = v => JSON.stringify([v.id, v.number])
if (!customKeys) {
// индексируем ключи кастомных элементов
customKeys = items.reduce((agg, v) =>
Object.assign(agg, {[getKey(v)]: v})
, {})
return items
} else {
// Для ключей, найденных ранее, устанавливаем родителя и отфильтровываем
const defaultIds = []
const defaultItems = items.filter(v => {
const child = customKeys[getKey(v)]
if (child) {
child.parent = v
defaultIds.push(v.id)
} else {
return true
}
})
// Оставляем в data.default лишь элементы с id, не найденные в custom
if (defaultIds.length) {
const idsSet = new Set(defaultIds)
const copy = [...data.default]
data.default.length = 0
data.default.push(...copy.filter(v => !idsSet.has(v.id)))
}
return defaultItems
}
})
console.log(data) // Дата без кастомных ключей в дефолт скоуп
console.log(items) // Результат
import math
def middle_permutation(string):
res, letters = '', sorted(list(string))
fct = math.factorial(len(letters))
remained = math.ceil(fct / 2)
while (len(letters)):
fct /= len(letters)
cnt = math.ceil(remained/ fct) - 1
res = res + letters.pop(cnt)
remained -= fct * cnt
if (remained == 0):
remained = fct
return res
$paramsFlat = [
['value' => ['option1' => 1], 'check' => 0],
['value' => ['option1' => 2], 'check' => 0],
...
['value' => ['option3' => 2], 'check' => 0],
];
function getNext($paramsFlat) {
foreach($paramsFlat as &$elem) {
// Инвертируем значения до первого найденного нуля
if($elem['checked'] = !$elem['checked']) break;
}
// Получаем массив, содержащий отмеченные элементы
$retVal = array_filter($paramsFlat, function($v) {return $v['checked'];});
if(!count($retVal)) return false;
// Возвращаем массив из значений отмеченных элементов
return array_map(function($v) {return $v['value'];}, $paramsFlat);
}
var sourceList = [11, 14, 15, 17, 27, 28, 32, 52, 53, 54, 55, 56, 57, 75, 90, 97];
sourceList.sort();
var max_d = 9, K = 4;
var result = [];
var sequence = [sourceList[0] - max_d];
sourceList.forEach(num => {
if(num - sequence[sequence.length - 1] >= max_d) {
if(sequence.length >= K) result = sequence;
sequence = [];
}
sequence.push(num);
});
if(sequence.length >= K) result = sequence;
console.log(result);
function combine($arr, $k) {
$combs = [];
$comb = range(0, $k - 1);
while($comb) {
$combs[] = array_map(function($i) use($arr) {return $arr[$i];}, $comb);
for($i = $k - 1, $max = count($arr) - 1; $i > -1; $i--, $max--) {
if($comb[$i] < $max) {
$comb[$i] ++;
for($j = $i + 1; $j < $k; $j++) {
$comb[$j] = $comb[$j - 1] + 1;
}
break;
}
}
if($i < 0) $comb = null;
}
return $combs;
}
print_r(combine([1,2,3,4,5], 3));
function checkWinner(arr, length) {
length = length || arr.length;
var winner = [
arr.map(row=>row.join('')).join(' '),
arr.map((_,v)=>arr.map(row=>row[v]).join('')).join(' '),
arr.map((_,v)=>arr[v][v]).join(''),
arr.map((_,v)=>arr[v][arr.length - v - 1]).join(''),
].join(' ').match('1'.repeat(length) + '|' + '2'.repeat(length));
return +((winner || ['0'])[0][0]);
}