Skip to content

Instantly share code, notes, and snippets.

View takuyadev's full-sized avatar
🔥
Coding grind!

Takuya Toyokawa takuyadev

🔥
Coding grind!
View GitHub Profile
@takuyadev
takuyadev / deepequalobject.js
Created June 4, 2023 03:35
2628. JSON Deep Equal
/**
* @param {any} o1
* @param {any} o2
* @return {boolean}
*/
const arrayEqual = (arr1, arr2) => {
const array1Length = arr1.length
const array2Length = arr2.length
@takuyadev
takuyadev / counter-two.js
Last active June 4, 2023 02:01
2665. Counter II
const createCounter = function (init) {
// Keep in memory by initiating a count
let initialValue = init
let count = init
// Return methods for the function
return {
increment: () => ++count,
decrement: () => --count,
reset: () => count = initialValue
@takuyadev
takuyadev / chatgpt.js
Created May 30, 2023 19:39
2623. Memoize
function memoize(fn) {
let cache = new Map();
return function (...args) {
let key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key);
}
@takuyadev
takuyadev / composefn.js
Created May 30, 2023 18:39
2629. Function Composition
/**
* @param {Function[]} functions
* @return {Function}
*/
var compose = function (functions) {
// This returns a function,
return function (x) {
// Initialize the output as the x parameter
@takuyadev
takuyadev / sleep.js
Created May 30, 2023 00:44
2622. Cache With Time Limit
/**
* @param {number} millis
*/
async function sleep(millis) {
// setTimeout cannot be used conjunction with async await
// You have to return a new Promise, that passes the resolve fn to setTimeout after milliseconds
return new Promise(resolve => setTimeout(resolve, millis))
}
@takuyadev
takuyadev / flatten.js
Created May 30, 2023 00:06
2625. Flatten Deeply Nested Array
/**
* @param {any[]} arr
* @param {number} depth
* @return {any[]}
*/
var flat = function (arr, n) {
// If base case is reached, only return the array.
// Reasoning is, you don't want to return a spread array
// ...ONLY IF n reaches 0. Otherwise, you want to continue
if (n === 0) {
@takuyadev
takuyadev / game.rb
Last active May 1, 2023 18:11
Math Game (Ruby)
class Game
# Manages the state of the game
# Can change turns, ask questions to players, check answers
def initialize(players, max_points)
@index = 0
@players = players
@max_points = max_points
@is_start = false
end
class Person
attr_accessor :name
def initialize(name)
@name = name
end
end
p = Person.new("L. Ron")
puts p.name
def benchmark
# Mark current time
start_time = Time.now()
# Run long callback / yield command
yield
# Mark end time
end_time = Time.now()
# Calculate total time using marked times
total_time = end_time - start_time
@takuyadev
takuyadev / vampire.js
Created March 24, 2023 21:27
Vampire Tree
class Vampire {
constructor(name, yearConverted) {
this.name = name;
this.yearConverted = yearConverted;
this.offspring = [];
this.creator = null;
}
/** Simple tree methods **/