/**
* Разбивает строку на три части: цифры, две буквы, две буквы.
* Возвращает либо массив из трех элементов,
* либо undefined
*/
function getParts(str) {
const re = /^(\d+)(\S{2})(\S{2})$/;
const match = str.match(re);
if (match) return match.slice(1);
}
getParts("5дмсм") // ["5", "дм", "см"]
YX| 0 1 2 3 4
--|-----------
0 | 0 1 0 0 1
1 | 1 1 0 1 1
2 | 1 0 1 1 0
3 | 0 1 1 0 0
4 | 0 0 1 0 0
[(1,0), (0,1), (1,1), (0,2)],
[(4,0), (3,1), (4,1), (2,2), (3,2), (1,3), (2,3), (2,4)]
>> 1, 2, 3, 4, 5;
<– 5
undefined
.console.log()
. function camelCase(str) {
return str
.trim()
.split(' ')
.map((w, i, a) => {
if (i === 0) return w.toLowerCase();
return w.substr(0, 1).toUpperCase() + w.substr(1).toLowerCase();
})
.join('')
}
function test() {
const testIn = ['Good Day', 'good night', 'Good Evening', 'Sleep', 'Go'];
const testOut = ['goodDay', 'goodNight', 'goodEvening', 'sleep', 'Go'];
for (let i = 0; i < testIn.length; i++) {
const input = testIn[i];
const output = camelCase(input);
if (testOut[i] === output) console.log("Test passed: %s => %s", input, output);
else console.error("Test failed: %s => %s", input, output);
}
}
test();
function chooseBestSum(t, k, ls) {
/**
* make next combination of N on bits in a 32-bit integer
*/
function nextPerm(x) {
// via https://stackoverflow.com/questions/506807/creating-multiple-numbers-with-certain-number-of-bits-set
if (x === 0) return 0;
const smallest = (x & -x);
const ripple = x + smallest;
const new_smallest = (ripple & -ripple);
const ones = ((new_smallest/smallest) >> 1) - 1;
return ripple | ones;
}
let bestSum = null, bestN;
const len = ls.length;
if (len > 31) throw "Too many (over 31) options for this algorithm";
const maxmask = (1 << len) - 1;
if (len < k) return null; // not enough distances
ls.sort((a, b) => a - b); // todo: skip checking rest of combinations once solid selection of elements exceeds t
let mask = (1 << k) - 1; // initial mask value with k less significant bits on
let sum, pos, n;
while(true) {
for(sum = 0, n = 0, pos = 0; pos < 32; pos++) {
if (mask & (1 << pos)) {
sum += ls[pos];
if (++n === k) break;
}
}
if (sum > bestSum && sum <= t) {
bestSum = sum;
bestN = mask;
}
mask = nextPerm(mask);
if (mask > maxmask) break;
if (mask < 0) break;
}
return bestSum;
}
N
на остаток деления S % n = 0
Вдруг, найдётся число, которое надо просто повторить m
раз, чтобы получить S
N
на разницу: если найдётся разница в 1, можно получить любое число через эти дваN
Подзадача та же, получить 1. Есть 1 – есть любое целое.npm install --save-dev script-loader
import exec from 'script-loader!./script.js';
const data = [
[ '6', 'Shorter', '157' ],
[ '7', 'Fraser', '157' ],
[ '6', 'Chandter', '156' ]
// ...
];
const result = data.reduce((acc, cur) => {
acc.forEach((el, idx) => {el.push(cur[idx])});
return acc;
}, [[], [], []]);
// [["6","7","6"],["Shorter","Fraser","Chandter"],["157","157","156"]]
var numbers = [1,-3,5,-6,-10,13,4,-8];
var arr = numbers.slice(), sum = 0;
while((sum += arr.shift()) !== 0); // изысканный пустой while !
var index = numbers.length - arr.length - 1; // 5
function sum0(arr, idx = 0, sum = 0) {
if ((sum += numbers[idx]) === 0) return idx;
return sum0(arr, idx + 1, sum);
}
sum0([1,-3,5,-6,-10,13,4,-8]) // 5