Skip to content

Instantly share code, notes, and snippets.

View darrenmason's full-sized avatar

Darren Mason darrenmason

View GitHub Profile
@darrenmason
darrenmason / dungeonGenerator.js
Created July 4, 2023 23:12
dungeon generator
class DungeonGenerator {
constructor(width, height, maxRooms, minRoomSize, maxRoomSize) {
this.width = width;
this.height = height;
this.maxRooms = maxRooms;
this.minRoomSize = minRoomSize;
this.maxRoomSize = maxRoomSize;
this.dungeon = [];
}
1. pr-1.js
On line 1, don't bind to window. This is exposing the current user details to the global namespace for no apparent reason, and is generally bad practice. (other than for a coding test demo purposes?:)
On line 11, I would prefer a forEach for brevity unless there's a specific need for for(), like a multidimensional array.
On line 12, using the Date object is fine but this should be imported from a utility that handles date conversions, with readable reusable method names such as getTimeSpan(24) ; this logic should not be in the component or we'll end up with code littered with ad hoc or repeated conversions.
On line 22, use <></> instead of <div></div> unless there is a specific reason for a new container other than wrapper element.
// linked list
// print() (traversal) is O(n) time complexity where n scales with list size
// remaining methods are O(1) constant time
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
@darrenmason
darrenmason / binarySearch.js
Created June 21, 2023 22:00
binary search
// binary search
// O(log n)
function binarySearch(array, target) {
let left = 0;
let right = array.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
@darrenmason
darrenmason / robotParts.js
Last active June 20, 2023 17:11
robot parts problem
/*
All robots are made of the same types of parts, and we have a string of all of the parts required to form a complete robot.
Given a list of available parts, return the collection of robot names for which we can build at least one complete robot.
N: Number of elements in `all_parts`
K: Number of robots
M: Number of elements in `required_parts`
Time Complexity:
@darrenmason
darrenmason / findRectangle.js
Last active June 20, 2023 17:11
2d array search in javascript
// 2d array search
// O(n * m)
function findRectangle(image) {
const rows = image.length;
const cols = image[0].length;
let row = -1;
let col = -1;
let width = 0;
@darrenmason
darrenmason / breadthFirstSearch.js
Last active June 21, 2023 22:03
breadth-first binary tree search
// breadth-first binary search
// O(log n)
/*
10
/ \
4 17
/ \ / \
1 9 12 18