Last active
August 9, 2021 07:42
-
-
Save apolkingg8/12cf41959d5bd69c9e127786e6664814 to your computer and use it in GitHub Desktop.
Orusky_pretest
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 isSubset from "./answer1"; | |
describe("answer1", ()=> { | |
// the time complex is O(n) | |
test("isSubset()", async ()=> { | |
expect(isSubset([], [])).toEqual(false) | |
expect(isSubset([`A`, `B`, `C`], [])).toEqual(false) | |
expect(isSubset([], [`A`, `B`, `C`])).toEqual(false) | |
expect(isSubset([`A`, `B`, `C`], [`B`, `C`])).toEqual(true) | |
expect(isSubset([`B`, `C`], [`A`, `B`, `C`])).toEqual(true) | |
expect(isSubset([`B`, `C`], [`C`, `D`])).toEqual(false) | |
}) | |
test("isSubset() bad input", async ()=> { | |
expect(isSubset([], null)).toEqual(false) | |
expect(isSubset(null, [])).toEqual(false) | |
expect(isSubset(null, null)).toEqual(false) | |
}) | |
}) |
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
const isSubset = (arr1: string[], arr2: string[]): boolean => { | |
try { | |
const hash1: string = arr1.join(``) | |
const hash2: string = arr2.join(``) | |
// empty is not subset | |
if (hash1.length === 0 || hash2.length === 0) { | |
return false | |
} | |
return hash1.indexOf(hash2) > -1 | |
|| hash2.indexOf(hash1) > -1 | |
} catch (err) { | |
console.error(err) | |
return false | |
} | |
} | |
export default isSubset |
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 {Cache, CacheStore} from "./answer2"; | |
describe("answer2", ()=> { | |
// time complex is O(n) | |
test("put()", ()=> { | |
let cacheStore = new CacheStore() | |
let cache1 = new Cache({key: `c1`, val: `c1`, weight: 100}) | |
let cache2 = new Cache({key: `c2`, val: `c2`, weight: 200}) | |
let cache3 = new Cache({key: `c3`, val: `c3`, weight: 300}) | |
let cache4 = new Cache({key: `c4`, val: `c4`, weight: 400}) | |
let cache5 = new Cache({key: `c5`, val: `c5`, weight: 500}) | |
expect(cacheStore.caches.length).toEqual(0) | |
cacheStore.put(cache1.key, cache1.val, cache1.weight) | |
cacheStore.put(cache2.key, cache2.val, cache2.weight) | |
cacheStore.put(cache3.key, cache3.val, cache3.weight) | |
expect(cacheStore.caches.length).toEqual(3) | |
cacheStore.put(cache4.key, cache4.val, cache4.weight) | |
cacheStore.put(cache5.key, cache5.val, cache5.weight) | |
expect(cacheStore.caches.length).toEqual(3) | |
expect(cacheStore.caches.map((cache)=> (cache.key))).toMatchObject([cache5.key, cache4.key, cache3.key]) | |
}) | |
test("get()", ()=> { | |
let cacheStore = new CacheStore() | |
let cache1 = new Cache({key: `c1`, val: `c1`, weight: 100}) | |
let cache2 = new Cache({key: `c2`, val: `c2`, weight: 200}) | |
let cache3 = new Cache({key: `c3`, val: `c3`, weight: 300}) | |
let cache4 = new Cache({key: `c4`, val: `c4`, weight: 400}) | |
let cache5 = new Cache({key: `c5`, val: `c5`, weight: 500}) | |
cacheStore.put(cache1.key, cache1.val, cache1.weight) | |
cacheStore.put(cache2.key, cache2.val, cache2.weight) | |
cacheStore.put(cache3.key, cache3.val, cache3.weight) | |
expect((cacheStore.get(cache1.key) as Cache).key).toEqual(cache1.key) | |
expect((cacheStore.get(cache2.key) as Cache).key).toEqual(cache2.key) | |
expect((cacheStore.get(cache3.key) as Cache).key).toEqual(cache3.key) | |
expect(cacheStore.get(cache4.key) as number).toEqual(-1) | |
expect(cacheStore.get(cache5.key) as number).toEqual(-1) | |
cacheStore.put(cache4.key, cache4.val, cache4.weight) | |
cacheStore.put(cache5.key, cache5.val, cache5.weight) | |
expect(cacheStore.get(cache1.key) as number).toEqual(-1) | |
expect(cacheStore.get(cache2.key) as number).toEqual(-1) | |
expect((cacheStore.get(cache3.key) as Cache).key).toEqual(cache3.key) | |
expect((cacheStore.get(cache4.key) as Cache).key).toEqual(cache4.key) | |
expect((cacheStore.get(cache5.key) as Cache).key).toEqual(cache5.key) | |
}) | |
}) |
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
export class Cache { | |
key: string = null | |
val: any = null | |
weight: number = 0 | |
lastUpdate: number = 0 | |
get score(): number { | |
return this.weight / (Date.now() - this.lastUpdate + 1) | |
} | |
constructor(props: Partial<Cache>) { | |
Object.assign(this, props) | |
this.lastUpdate = Date.now() | |
} | |
} | |
export class CacheStore { | |
readonly cacheLimit: number = 3 | |
caches: Cache[] = [] | |
get = (key: string)=> { | |
return this.caches.find((cache)=> { | |
return cache.key === key | |
}) || -1 | |
} | |
put = (key: string, val: any, weight: number)=> { | |
let newCache = new Cache({ | |
key: key, | |
val: val, | |
weight: weight, | |
}) | |
this.caches.push(newCache) | |
if(this.caches.length > this.cacheLimit) { | |
this.caches.sort((a, b)=> (b.score - a.score)) | |
this.caches.pop() | |
} | |
} | |
} | |
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 foo from "./answer3"; | |
describe("answer3", ()=> { | |
function recur(n, cur) { | |
if (!cur) { | |
cur = 0; | |
} | |
if (n < 2) { | |
throw new Error('Invalid input'); | |
} | |
if (n === 2) { | |
return 1 / n + cur; | |
} | |
return recur(n - 1, cur + 1 / (n * (n -1))); | |
} | |
test("foo()", ()=> { | |
expect(foo(7, 2)).toEqual(recur(7, 2)) | |
expect(foo(3, 4)).toEqual(recur(3, 4)) | |
expect(foo(10, 10)).toEqual(recur(10, 10)) | |
expect(foo(20, 11)).toEqual(recur(20, 11)) | |
expect(foo(5, 7)).toEqual(recur(5, 7)) | |
}) | |
test("foo() bad input", ()=> { | |
expect(()=> {foo(1, 2)}).toThrow('Invalid input') | |
}) | |
}) |
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
const foo = (n: number, cur: number)=> { | |
if(n < 2) { | |
throw `Invalid input` | |
} | |
cur = cur || 0 | |
for(;n>2;n-=1) { | |
cur = cur + 1 / (n * (n -1)) | |
} | |
return 1 / n + cur | |
} | |
export default foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment