Skip to content

Instantly share code, notes, and snippets.

@bdelmas
Last active November 26, 2019 12:02
Show Gist options
  • Save bdelmas/d9ba1035b327cb550e3facffb6db5782 to your computer and use it in GitHub Desktop.
Save bdelmas/d9ba1035b327cb550e3facffb6db5782 to your computer and use it in GitHub Desktop.
let test = [1, 2, 3, 3, 4, 4, 4, 4]
const getTotal = list => {
let result = new Map()
list.map(e => {
let total = result.get(e)
if (total !== undefined) {
result.set(e, total + 1)
} else {
result.set(e, 1)
}
})
return result
}
const reduceTotal = listMapTotals => {
let result = new Map()
listMapTotals.map(lmt => {
lmt.forEach((total, key) => {
let finalTotal = result.get(key)
if (finalTotal !== undefined) {
result.set(key, finalTotal + total)
} else {
result.set(key, total)
}
})
})
return result
}
console.log(reduceTotal([getTotal(test), getTotal(test)]))
const groupByCallback = (list, fn) => {
const listTotal = getTotal(list)
listTotal.forEach((_, key) => {
const total = listTotal.get(key)
for (let i = 1; i <= total; i++) {
fn(key, i, total)
}
})
}
/*
console.log(
groupByCallback(
test,
(key, i, total) => console.log(key, i, total)
)
)
*/
const getPar = (index, l, r, string) => {
if (l < 0 || r < l) return;
if (l === 0 && r === 0) {
console.log(string.reduce((acc, v) => acc + v), '')
} else {
if (l > 0) {
string[index] = '('
getPar(index + 1, l - 1, r, string)
}
if (l < r) {
string[index] = ')'
getPar(index + 1, l, r - 1, string)
}
}
}
const getParenthesis = total => {
getPar(0, total, total, [])
}
getParenthesis(3)
// Robot
const testRobot = [
[true, true, true],
[true, true, false],
[false, false, true],
]
let endresult = []
const getTP = (test, x, y, path) => {
if (test.length === 0 ) return;
if (test.length !== test[0].length) return;
if (
(x !== 0 || y !== 0)
&& x < test.length
&& y < test[x].length
&& test[x][y]
) {
endresult = [...endresult, path]
}
if (
x + 1 < test.length
&& y < test[x + 1].length
&& test[x + 1][y]
) {
getTP(test, x + 1, y, [...path, 'down'])
}
if (
x < test.length
&& y + 1 < test[x].length
&& test[x][y + 1]
) {
getTP(test, x, y + 1, [...path, 'right'])
}
}
const getTPs = test => {
getTP(test, 0, 0, [], [])
}
getTPs(testRobot)
console.log(endresult)
let screen = [
[[1, 0], [1, 0], [1, 1], [1, 0]],
[[1, 1], [1, 0], [1, 1], [1, 0]],
[[1, 1], [1, 0], [1, 0], [1, 1]],
[[1, 0], [1, 0], [1, 0], [1, 1]], // 4, 1
[[1, 0], [1, 1], [1, 1], [1, 1]],
[[1, 0], [1, 1], [1, 1], [1, 1]],
]
const fill = (x, y, color) => {
if (
x < 0
|| y < 0
|| color.length !== 2
) {
return
}
if (
x >= 0
&& x < screen.length
&& y >= 0
&& y < screen[x].length
&& (color[0] !== screen[x][y][0] || color[1] !== screen[x][y][1])
) {
screen[x][y] = color
}
if (
x + 1 >= 0
&& x + 1 < screen.length
&& y >= 0
&& y < screen[x + 1].length
&& (color[0] !== screen[x + 1][y][0] || color[1] !== screen[x + 1][y][1])
) {
fill(x + 1, y, color)
}
if (
x - 1 >= 0
&& x - 1 < screen.length
&& y >= 0
&& y < screen[x - 1].length
&& (color[0] !== screen[x - 1][y][0] || color[1] !== screen[x - 1][y][1])
) {
fill(x - 1, y, color)
}
if (
x >= 0
&& x < screen.length
&& y + 1 >= 0
&& y + 1 < screen[x].length
&& (color[0] !== screen[x][y + 1][0] || color[1] !== screen[x][y + 1][1])
) {
fill(x, y + 1, color)
}
if (
x >= 0
&& x < screen.length
&& y - 1 >= 0
&& y - 1 < screen[x].length
&& (color[0] !== screen[x][y - 1][0] || color[1] !== screen[x][y - 1][1])
) {
fill(x, y - 1, color)
}
}
const fillScreenWithColor = (x, y, color) => {
fill(x, y, color)
}
fillScreenWithColor(3, 1, [1,1])
console.log(screen)
// Stack
class MinStack {
constructor() {
this.stack = []
}
add(elm) {
const min = this.stack.length > 0
? this.stack[this.stack.length - 1].min
: Infinity
this.stack.push({elm, min: Math.min(elm, min)})
}
pop() {
return this.stack.pop()
}
min() {
return this.stack.length > 0
? this.stack[this.stack.length - 1].min
: Infinity
}
}
const stack = new MinStack()
stack.add(1)
stack.add(2)
stack.add(-2)
stack.add(2)
console.log(stack)
console.log(stack.pop())
console.log(stack)
console.log(stack.pop())
console.log(stack)
console.log(stack.pop())
console.log(stack)
console.log(stack.pop())
console.log(stack)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment