function shallowCopy(obj) {
return Object.assign({}, obj)
}
function createWeightedRandom_O1(input) {
const len = input.length
if(len === 0) return null
throw new Error('not implemented');
}
function createWeightedRandom_OlogN(input) {
throw new Error('not implemented');
}
function getSum(input){
return input.reduce((pre, cur) => {
return pre + cur
})
}
function getRatio(input){
let map = new Map(), sum = getSum(input)
for(let el of input) {
map.set()
}
}
function random(input){
let random = Math.floor(Math.random() * 100)
return random
}
console.log('random',random([1,2,3,5]))
class AsyncWorker {
constructor(capacity) {
this.capacity = capacity;
}
exec(task) {
throw new Error('not implemented');
}
}
function assert(value, message) {
if (value !== true) {
throw new Error(message);
}
}
function testShallowCopy() {
class UserModel {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
fullName() {
return this.firstName + ' ' + this.lastName;
}
}
const user = new UserModel('Hello', 'World');
const user2 = shallowCopy(user);
user2.firstName = 'Hello 2';
user2.lastName = 'World 2';
assert(user2 instanceof UserModel, 'should be instance');
assert(user.fullName() === 'Hello World', 'should keep original fields');
assert(user2.fullName() === 'Hello 2 World 2', 'should returns new value');
}
function testCreateWeightedRandom_O1() {
const input = [4, 2, 1, 3];
const sampling = createWeightedRandom_O1(input);
const count = [0, 0, 0, 0];
for (let i = 0; i < 10000; i++) {
count[sampling()]++;
}
const rates = count.map((i) => Math.round(i / 1000));
assert(JSON.stringify(rates) === '[4,2,1,3]', `${rates} is not same to ${input}`);
}
function testCreateWeightedRandom_OlogN() {
const input = [4, 2, 1, 3];
const sampling = createWeightedRandom_OlogN(input);
const count = [0, 0, 0, 0];
for (let i = 0; i < 10000; i++) {
count[sampling()]++;
}
const rates = count.map((i) => Math.round(i / 1000));
assert(JSON.stringify(rates) === '[4,2,1,3]', `${rates} is not same to ${input}`);
}
async function testAsyncWorker() {
const start = Date.now();
const createTask = (timeout, error) => {
return () => new Promise((resolve, reject) => {
setTimeout(() => {
if (error) {
reject(error);
}
else {
resolve(timeout);
}
}, timeout);
});
};
const worker = new AsyncWorker(2);
const tasks = [
{ status: 'fulfilled', value: 100, idealCost: 100, task: worker.exec(createTask(100)) },
{ status: 'fulfilled', value: 201, idealCost: 200, task: worker.exec(createTask(201)) },
{ status: 'rejected', reason: 'REJECTED', idealCost: 300, task: worker.exec(createTask(202, 'REJECTED')) },
{ status: 'fulfilled', value: 203, idealCost: 400, task: worker.exec(createTask(203)) },
{ status: 'fulfilled', value: 300, idealCost: 600, task: worker.exec(createTask(300)) },
];
for (let index = 0; index < tasks.length; index++) {
const t = tasks[index];
let result;
try {
const value = await t.task;
result = { status: 'fulfilled', value };
}
catch (e) {
result = { status: 'rejected', reason: e };
}
const realCost = Date.now() - start;
const idealCost = (realCost - (realCost % 100)) | 0;
if (idealCost !== t.idealCost) {
throw new Error(`unexpected time cost: ${idealCost}, expected is ${t.idealCost} for ${index}`);
}
if (result.status !== t.status) {
throw new Error(`unexpected status ${result.status} for ${index}`);
}
if (t.status === 'fulfilled' && result.status === 'fulfilled' && result.value !== t.value) {
throw new Error(`unexpected fulfilled value ${result.value}, expected is ${t.value} for ${index}`);
}
if (t.status === 'rejected' && result.status === 'rejected' && result.reason !== t.reason) {
throw new Error(`unexpected rejected reason ${result.reason}, expected is ${t.reason} for ${index}`);
}
}
}
async function main() {
try {
console.log('Testing Q1...');
await testShallowCopy();
console.log('PASSED!');
}
catch (e) {
console.log(e.stack || e);
}
try {
console.log('Testing Q2.1...');
await testCreateWeightedRandom_O1();
console.log('PASSED!');
}
catch (e) {
console.log(e.stack || e);
}
try {
console.log('Testing Q2.2...');
await testCreateWeightedRandom_OlogN();
console.log('PASSED!');
}
catch (e) {
console.log(e.stack || e);
}
try {
console.log('Testing Q3...');
await testAsyncWorker();
console.log('PASSED!');
}
catch (e) {
console.log(e.stack || e);
}
}
main();