#include <iostream>
struct Node {
int value;
Node *next;
};
void RecursiveOutput( Node* item )
{
if ( item != 0 )
{
RecursiveOutput(item->next);
std::cout << item->value << std::endl;
}
}
void RecursiveInput( int n, int index = 1, Node* item = 0 )
{
if ( index <= n )
{
Node data;
std::cin >> data.value;
data.next = item;
RecursiveInput( n, index + 1, &data );
} else {
std::cout << "RecursiveOutput" << std::endl;
RecursiveOutput(item);
}
}
int DEPTH = 3;
int main()
{
std::cout << "input values: " << std::endl;
RecursiveInput(DEPTH);
std::cout << "end" << std::endl;
return 0;
}
const promises = [
Promise.reject('rejected1'),
Promise.reject('rejected2'),
Promise.resolve('resolved'),
];
1) err => {throw err;}
2) err => {throw err;}
3) onFullfiled
4) onRejected,
5) onRejected
6) v => v,
console[level].apply(console, argsWithFormat)
function createRandom(arr) {
const uniqArr = [...new Set(arr)]; // выкидываем дубликаты из массива
let size = uniqArr.length;
let index = -1;
return () => {
const rand = Math.floor(Math.random() * size);
index = (rand + index + 1) % uniqArr.length;
size = uniqArr.length - 1;
return uniqArr[index];
};
}
// использование
const getRand = createRandom([0, 1, 2, 3]);
for (let i = 0; i < 15; ++i) {
console.log(getRand());
}
class Solution {
private:
int sumAP(int n, int m) {
int c = n / m;
return m * c * (c + 1) / 2;
}
public:
int sumOfMultiples(int n) {
return sumAP(n, 3) + sumAP(n, 5) + sumAP(n, 7)
- sumAP(n, 3 * 5) - sumAP(n, 3 * 7) - sumAP(n, 5 * 7)
+ sumAP(n, 3 * 5 * 7);
}
};
// ----- общий код с генериками ---------------
type Parsers<T extends object> = Omit<{
[K in keyof T]: (str: string) => T[K];
}, 'id'>;
function setValue<T extends object>(obj: T, key: Exclude<keyof T, 'id'>, value: string, parsers: Parsers<T>): void {
obj[key] = parsers[key](value); // магия
}
// ----- конкретный кейс ---------------------
interface Todo {
id: number;
name: string;
text: string;
complete: boolean;
num: number;
}
type List = 'name' | 'text' | 'complete' | 'num';
let list: List[] = ['name', 'text', 'complete', 'num']
const parsers: Parsers<Todo> = {
complete: s => s === 'true',
name: s => s,
text: s => s,
num: s => +s
};
const data: Todo = {
id: 1,
name: '',
text: '',
complete: false,
num: 0,
}
for(let input of list) {
setValue(data, input, formData[input].value, parsers);
}