Created
July 29, 2020 22:24
-
-
Save listenrightmeow/73a6ec4ca659fa4d4f7add4c3dadde5c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai'; | |
import { bool } from 'aws-sdk/clients/signer'; | |
describe('codewars', () => { | |
it('set unique', () => { | |
const arrayDiff = (l, r) => Array.from(new Set(l.concat(r))); | |
const t = arrayDiff([1, 2], [2]); | |
expect(t).to.have.lengthOf(2); | |
expect(t).to.deep.equal([1,2]); | |
}); | |
it('filter unique', () => { | |
const arrayDiff = (l, r) => { | |
const arr = l.concat(r); | |
return arr.filter((item, index) => arr.indexOf(item) === index); | |
} | |
const t = arrayDiff([1, 2, 1], [2]); | |
expect(t).to.have.lengthOf(2); | |
expect(t).to.deep.equal([1, 2]); | |
}); | |
it('reduce unique', () => { | |
const arrayDiff = (l, r) => { | |
const arr = l.concat(r); | |
return arr.reduce((n, i) => n.includes(i) ? n : [...n, i], []); | |
} | |
const t = arrayDiff([1, 2, 1], [2]); | |
expect(t).to.have.lengthOf(2); | |
expect(t).to.deep.equal([1, 2]); | |
}); | |
it('diff array', () => { | |
const diffArray = (l, r) => l.reduce((n, i) => r.includes(i) ? n : [...n, i], []); | |
const t = diffArray([1,2,2,2,3], [2]); | |
console.log(t); | |
}); | |
it('string duplicates', () => { | |
const dedup = (str) => { | |
const s = str.toLowerCase(); | |
const x = Array.from(new Set(s)); | |
console.log(s, s.length, x.length); | |
console.log(x); | |
return s.length - Array.from(new Set(s)).length; | |
} | |
const t = dedup('ABBA'); | |
console.log(t); | |
}); | |
it('senior vs open', () => { | |
const ctx = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]; | |
const openOrSenior = (data, age, range) => { | |
const r = range[1] - range[0]; | |
const handicap = Array.from(new Array(r), (x, i) => i + range[0]); | |
return data.map(x => { | |
if (x[0] < age) { | |
return "Open"; | |
} else if (handicap.includes(x[1])) { | |
return "Senior" | |
} else { | |
return "Open" | |
} | |
}); | |
}; | |
const t = openOrSenior(ctx, 55, [-2, 26]); | |
console.log(t) | |
}); | |
it('population', () => { | |
const year = (p0, percentage, augmentation, population, years = 1) => { | |
const inhabitants = Math.ceil(p0 + (p0 * (percentage / 100)) + augmentation); | |
if (population - inhabitants > 0) { | |
return year(inhabitants, percentage, augmentation, population, ++years); | |
} | |
return years; | |
} | |
const t = year(1500, 5, 100, 5000); | |
console.log(t); | |
}); | |
it('equal arrays', () => { | |
const test = (arr) => { | |
let result = -1; | |
arr.some((i, idx) => { | |
const l = arr.slice(0, idx).reduce((a, b) => a + b, 0); | |
const r = arr.slice(idx + 1).reduce((a, b) => a + b, 0); | |
const match = l === r; | |
result = (!match && idx + 1 === arr.length) ? -1 : idx; | |
return match; | |
}); | |
return result; | |
} | |
expect(test([1,2,3,4,3,2,1])).to.equal(3); | |
expect(test([1, 100, 50, -51, 1, 1])).to.equal(1); | |
expect(test([1, 2, 3, 4, 5, 6])).to.equal(-1); | |
expect(test([10, -80, 10, 10, 15, 35, 20])).to.equal(6); | |
}); | |
it('string position replacement', () => { | |
const alphabetPos = (str) => { | |
const ctx = str.toUpperCase().replace(/\W/g, ''); | |
return ctx.split('').map((i, idx) => { | |
const code = ctx.charCodeAt(idx); | |
if (code > 64 && code < 91) { | |
return code - 64; | |
} | |
}).filter(i => !!i).join(' '); | |
} | |
expect(alphabetPos("The sunset sets at twelve o' clock.")).to.equal('20 8 5 19 21 14 19 5 20 19 5 20 19 1 20 20 23 5 12 22 5 15 3 12 15 3 11'); | |
expect(alphabetPos('l73v&9:{')).to.equal('12 22'); | |
}); | |
it('ltr ordered uniques', () => { | |
const uniqueInOrder = (str) => { | |
const arr = str.split(''); | |
return arr.filter((ctx, idx) => ctx !== arr[idx - 1]); | |
}; | |
expect(uniqueInOrder('AAAABBBCCDAABBB')).to.deep.equal(['A', 'B', 'C', 'D', 'A', 'B']); | |
}); | |
it('high and low', () => { | |
const highAndLow = (ctx) => { | |
const arr = ctx.split(' ').map(i => parseInt(i, 10)).sort((a, b) => a - b); | |
const {0: low, [arr.length - 1]: high} = arr.reverse(); | |
return [low, high].join(' '); | |
}; | |
expect(highAndLow('1 2 3 4 5')).to.equal('5 1'); | |
expect(highAndLow('4 5 29 54 4 0 -214 542 -64 1 -3 6 -6')).to.equal('542 -214') | |
}); | |
it('calculating with functions 2', () => { | |
const [zero, one, two, three, four, five, six, seven, eight, nine] = Array.from({ length: 10 }, (v, i) => (fn = null) => fn ? fn(i) : i); | |
const [plus, minus, times, dividedBy] = ['+', '-', '*', '/'].map(operator => ((y) => { | |
return (x) => eval(`${x} ${operator} ${y}`); | |
})); | |
console.log(one(plus(one()))); | |
console.log(nine(dividedBy(three()))); | |
console.log(nine(minus(six()))); | |
}); | |
it('calculating with functions', () => { | |
const numbers = []; | |
for (let n = 0; n < 10; n++) { | |
numbers[n] = (fn) => fn ? fn(n) : n; | |
} | |
const functions = []; | |
for (let f = 0; f <= 3; f++) { | |
switch(f) { | |
case 0: | |
functions[f] = (y) => { | |
console.log('plus'); | |
return (x) => x + y | |
} | |
break; | |
case 1: | |
functions[f] = (y) => { | |
console.log('minus'); | |
return (x) => x - y | |
} | |
break; | |
case 2: | |
functions[f] = (y) => { | |
console.log('times'); | |
return (x) => x * y | |
} | |
break; | |
case 3: | |
functions[f] = (y) => { | |
console.log('dividedBy'); | |
return (x) => y === 0 ? 0 : Math.floor(x/y) | |
} | |
break; | |
} | |
} | |
const [zero, one, two, three, four, five, six, seven, eight, nine] = numbers; | |
const [plus, minus, times, dividedBy] = functions; | |
console.log(one(plus(two()))); | |
console.log(seven(minus(five()))); | |
console.log(three(times(three()))); | |
console.log(nine(dividedBy(three()))); | |
console.log(six(dividedBy(two()))); | |
}); | |
it('createPhonenumber', () => { | |
const createPhonenumber = (arr) => `(${n.slice(0, 3).join('')}) ${n.slice(3, 6).join('')}-${n.slice(6).join('')}`; | |
const n = [1,2,3,4,5,6,7,8,9,0]; | |
console.log(n.slice(0,3).join('')); | |
console.log(createPhonenumber(n)); | |
}); | |
it('isPrime', () => { | |
const isPrime = (n) => { | |
for(let i = 2, sq = Math.sqrt(n); i <= sq; i++) { | |
const x = n % i === 0 | |
if (x) return false; | |
} | |
return n > 1; | |
} | |
console.log(isPrime(-1)); | |
console.log(isPrime(1)); | |
console.log(isPrime(2)); | |
console.log(isPrime(3)); | |
console.log(isPrime(5)); | |
console.log(isPrime(6)); | |
}); | |
it('SQL', () => { | |
const persons = [ | |
{ name: 'Peter', profession: 'teacher', age: 20, maritalStatus: 'married' }, | |
{ name: 'Michael', profession: 'teacher', age: 50, maritalStatus: 'single' }, | |
{ name: 'Peter', profession: 'teacher', age: 20, maritalStatus: 'married' }, | |
{ name: 'Anna', profession: 'scientific', age: 20, maritalStatus: 'married' }, | |
{ name: 'Rose', profession: 'scientific', age: 50, maritalStatus: 'married' }, | |
{ name: 'Anna', profession: 'scientific', age: 20, maritalStatus: 'single' }, | |
{ name: 'Anna', profession: 'politician', age: 50, maritalStatus: 'married' } | |
]; | |
function duplicate(method: string) { | |
return function (_target: unknown, _key: string, descriptor: PropertyDescriptor) { | |
const ctx = descriptor.value; | |
descriptor.value = function (...args: unknown[]) { | |
if (this.action.includes(method)) { | |
throw new Error(`Duplicate ${method.toUpperCase()}`); | |
} | |
this.action = [...this.action, method]; | |
const result = ctx.apply(this, args); | |
return result; | |
}; | |
return descriptor; | |
} | |
} | |
interface Filter { | |
(param: any): bool | string; | |
} | |
interface Group { | |
(param: any): string; | |
} | |
interface Order { | |
(l: any, r: any): number; | |
} | |
class Connection { | |
private data: (number | unknown)[] = []; | |
protected action: string[] = []; | |
protected _select: Filter; | |
@duplicate('execute') | |
execute() { | |
return this._select ? this.data.map(this._select) : this.data; | |
} | |
@duplicate('from') | |
from(...args: (number | unknown)[][]) { | |
if (args.length > 1) { | |
this.data = args.map((_a, i) => args.map(arg => arg[i])); | |
} else { | |
this.data = args[0]; | |
} | |
return this; | |
} | |
having(filter: Filter) { | |
this.data = this.data.filter(filter); | |
return this; | |
} | |
@duplicate('orderBy') | |
orderBy(order: Order) { | |
this.data = this.data.sort(order); | |
return this; | |
} | |
@duplicate('select') | |
select(sel: Filter = null) { | |
this._select = sel; | |
return this; | |
} | |
where(filter: Filter, or: Filter = null) { | |
if (!or) { | |
this.data = this.data.filter(filter); | |
} else { | |
const l = this.data.filter(filter); | |
const r = this.data.filter(or); | |
this.data = [...l, ...r]; | |
} | |
return this; | |
} | |
private local(g: unknown, k: string, v: unknown) { | |
if (!g.hasOwnProperty(k)) { | |
g[k] = [v]; | |
} else { | |
g[k].push(v); | |
} | |
} | |
private loop(groups: unknown, group: Group, data: unknown[]) { | |
let ctx: unknown[]; | |
data.forEach((d, idx, arr) => { | |
const g = group(d); | |
this.local(groups, g, d); | |
if (idx === arr.length - 1) { | |
ctx = Object.keys(groups).map(g => [g, groups[g]]); | |
} | |
}); | |
return ctx; | |
} | |
@duplicate('groupBy') | |
groupBy(...args: unknown[]) { | |
let groups = {}; | |
args.forEach((group: Group, gidx) => { | |
if (!gidx) { | |
this.data = this.loop(groups, group, this.data); | |
groups = {} | |
} else { | |
this.data.forEach((d: unknown, i: number) => { | |
const data = d[1]; | |
this.data[i][1] = this.loop(groups, group, data); | |
}); | |
} | |
}); | |
return this; | |
} | |
} | |
const query = () => { | |
const klass = new Connection(); | |
return klass; | |
} | |
const x = query().select().from(persons).execute(); | |
console.log(x); | |
let select = person => person.profession; | |
const y = query().from(persons).select(select).execute(); | |
console.log(y); | |
const z = query().select().execute(); | |
console.log(z); | |
const filters = { | |
teacher: person => person.profession === 'teacher' | |
} | |
const xx = query().select(select).from(persons).where(filters.teacher).execute(); | |
console.log(xx); | |
const yy = query().select().from(persons).where(filters.teacher).execute(); | |
console.log(yy); | |
const profession = person => person.profession; | |
const zz = query().select().from(persons).groupBy(profession).execute(); | |
console.log(zz); | |
const xxx = query().select().from(persons).where(filters.teacher).groupBy(profession).execute(); | |
console.log(xxx); | |
select = group => group[0] | |
const yyy = query().select(select).from(persons).groupBy(profession).execute(); | |
console.log(yyy); | |
const xxxx = query().from(persons).execute(); | |
console.log(xxxx); | |
const yyyy = query().execute(); | |
console.log(yyyy); | |
let zzzz; | |
try { | |
zzzz = query().select().select().execute(); | |
} catch(error) { | |
console.log(error); | |
} | |
const numbers = [1,2,3,4,5,6,7,8,9]; | |
const even = n => n % 2 === 0; | |
const parity = n => even(n) ? 'even' : 'odd'; | |
const foo = query().select().from(numbers).execute(); | |
console.log(foo); | |
const bar = query().select().from(numbers).groupBy(parity).execute(); | |
console.log(bar); | |
const isPrime = (n) => { | |
for (let i = 2, sq = Math.sqrt(n); i <= sq; i++) { | |
const x = n % i === 0 | |
if (x) return false; | |
} | |
return n > 1; | |
} | |
const prime = n => isPrime(n) ? 'prime' : 'divisible'; | |
const baz = query().select().from(numbers).groupBy(parity, prime).execute(); | |
console.log(baz); | |
select = ctx => ctx[0] === 'odd'; | |
const boo = query().select().from(numbers).groupBy(parity).having(select).execute(); | |
console.log(boo); | |
const order = (l: number, r: number) => r - l; | |
const qux = query().select().from(numbers).orderBy(order).execute(); | |
console.log(qux); | |
const teachers = [ | |
{ | |
teacherId: '1', | |
teacherName: 'Peter' | |
}, | |
{ | |
teacherId: '2', | |
teacherName: 'Anna' | |
} | |
]; | |
const students = [ | |
{ | |
studentName: 'Michael', | |
tutor: '1' | |
}, | |
{ | |
studentName: 'Rose', | |
tutor: '2' | |
} | |
]; | |
function teacherJoin(join) { | |
return join[0].teacherId === join[1].tutor; | |
} | |
function student(join) { | |
return { studentName: join[1].studentName, teacherName: join[0].teacherName }; | |
} | |
const quux = query().select(student).from(teachers, students).where(teacherJoin).execute(); | |
console.log(quux); | |
function lol(join) { | |
return join[1].tutor === '1'; | |
} | |
const quuux = query().select(student).from(teachers, students).where(teacherJoin).where(lol).execute(); | |
console.log(quuux); | |
function lessThan3(number) { | |
return number < 3; | |
} | |
function greaterThan4(number) { | |
return number > 4; | |
} | |
const quuuux = query().select().from([1, 2, 3, 4, 5, 7]).where(lessThan3, greaterThan4).execute(); | |
console.log(quuuux); | |
function greatThan1(group) { | |
return group[1].length > 1; | |
} | |
function isPair(group) { | |
return group[0] % 2 === 0; | |
} | |
function id(value) { | |
return value; | |
} | |
function frequency(group) { | |
return { value: group[0], frequency: group[1].length }; | |
} | |
const quuuuux = query().select(frequency).from([1, 2, 1, 3, 5, 6, 1, 2, 5, 6]).groupBy(id).having(greatThan1).having(isPair).execute(); | |
console.log(quuuuux); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment