✂️✅Подсчитайте количество отрицательных значения в массив, используя метод reduce().
const N = 10;
var
a: array[1..N] of integer;
i, pos, neg: byte;
begin
randomize;
pos := 0;
neg := 0;
for i := 1 to N do begin
a[i] := random(7) - 3;
write(a[i], ' ');
if a[i] < 0 then
neg := neg + 1
else
if a[i] > 0 then
pos := pos + 1;
end;
writeln;
✂️✅ Вычислите сумму чисел массива, используя метод reduce().
var euros = [29.76, 41.85, 46.5]; var sum = euros.reduce( function(total, amount){ return total + amount }); sum // 118.11
✂️✅Преобразуйте массив строк в одну строку с помощью метода reduce().
function learnJavaScript() { const friends = [ { passport: '03005988', name: 'Joseph Francis Tribbiani Jr', age: 32, sex: 'm' }, { passport: '03005989', name: 'Chandler Muriel Bing', age: 33, sex: 'm' }, { passport: '03005990', name: 'Ross Eustace Geller', age: 33, sex: 'm' }, { passport: '03005991', name: 'Rachel Karen Green', age: 31, sex: 'f' }, { passport: '03005992', name: 'Monica Geller', age: 31, sex: 'f' }, { passport: '03005993', name: 'Phoebe Buffay', age: 34, sex: 'f' } ]
const names = friends.reduce((accumulator, friend) =>
${accumulator} ${friend.name},
, 'Friends: ')return names }
✂️✅Подсчитайте количество отрицательных значения в массив, используя метод reduce().
const N = 10; var a: array[1..N] of integer; i, pos, neg: byte;
begin randomize; pos := 0; neg := 0; for i := 1 to N do begin a[i] := random(7) - 3; write(a[i], ' '); if a[i] < 0 then neg := neg + 1 else if a[i] > 0 then pos := pos + 1; end; writeln;
end.
✂️✅Найдите максимальное значение в массиве
int[] arr = {10, 7, 1, 4, 7, 4, 11};
// Предположим, что нулевой элемент максимальный int max = arr[0];
// В цикле начинаем с первой ячейки for (int i = 1; i < arr.length; i++) { if (arr[i] > max) { max = arr[i]; } }
System.out.println(max); // => 11
✂️✅ay.prototype.min = function(){ var min = parseInt(this[this.length-1]), el; for(var i=this.length-2; i>=0; i--){ el = parseInt(this[i]); if(el<min){ min = el; } } return min; };
Array.prototype.max = function(){ var max = parseInt(this[this.length-1]), el; for(var i=this.length-2; i>=0; i--){ el = parseInt(this[i]); if(el>max){ max = el; } } return max; };
// использование: var array = [1,3,5,-1,8,0]; document.write(array.min());// -1 document.write(''); document.write(array.max());// 8
✂️✅ Вычислите среднее значение массива чисел, используя метод reduce().
function average(nums) { return nums.reduce((a, b) => (a + b)) / nums.length; }
✂️ ✅Сведите массив массивов в один массив с помощью метода reduce().
const array1 = [1, 2, 3, 4];
// 0 + 1 + 2 + 3 + 4 const initialValue = 0; const sumWithInitial = array1.reduce((accumulator, currentValue) => accumulator + currentValue, initialValue);
console.log(sumWithInitial); // Expected output: 10
✂️✅ Удалите повторяющиеся значения из массива с помощью метода reduce().
const array = [1, 2, 3, 1, 1, '1', '2', '1', true, false, true, null, undefined, null, null, undefined];
const makeUniq = (arr) => { return arr.reduce((acc, currentValue) => { acc.indexOf(currentValue) === -1 && acc.push(currentValue); return acc; } , []); }
makeUniq(array);
✅Вычислите факториал числа, используя метод reduce().
var o = { q: '\'', b: '\', s: 'var o = {q: _q_b_q_q, b:_q_b_b_q, s: _q_s_q}; console.log(Object.keys(o).reduce(function(a, k){return a.split(_q_q + k).join(o[k])}, o.s))' }; console.log(Object.keys(o).reduce(function(a, k){return a.split('' + k).join(o[k])}, o.s));
✅Удалите ложные значения из массива
var arr = [ 0, 1, '', undefined, false, 2, undefined, null, , 3, NaN ];
var filtered = arr.filter(Boolean); console.log(filtered);
/ результат: [ 1, 2, 3 ] / ✅Подсчитайте, сколько раз определенное слово появляется в предложении
from collections import defaultdict
words = "слово1 слово2 слово1 слово3 слово2 Вася слово1" word_list = words.split()
word_count_dict = defaultdict(int)
for word in word_list: print(word, word_count_dict[word]) word_count_dict[word] += 1