Skip to content

Instantly share code, notes, and snippets.

View grevych's full-sized avatar
🤓

Gerardo Reyes grevych

🤓
View GitHub Profile
@grevych
grevych / merge_k_sorted_arrays.go
Created October 10, 2019 22:03
Merge K sorted arrays in a single one. Link: https://goplay.space/#rDyKPxaNLcl
package main
import (
"container/heap"
"fmt"
)
type Count struct {
arrayRef int
indexRef int
@grevych
grevych / tree_height.py
Last active August 28, 2019 22:32
Build a function that returns the height of a tree
def height_dfs(root):
stack = [(root, 0)]
total_height = 0
while stack:
current_node, height = stack.pop()
total_height = max(total_height, height)
if current_node.left:
stack.append((current_node.left, height + 1))
@grevych
grevych / find_first_missing.py
Last active August 27, 2019 21:30
Find the first missing element between 1 and N in an array of integers. -1000000 <= x <= 1000000
LIMIT = 1000000
def unmaskNumber(number):
if number <= -LIMIT:
number = number + LIMIT
elif number > LIMIT:
number = number - LIMIT
return number
@grevych
grevych / tree.go
Created August 14, 2019 22:59
Simple Binary Tree. Iterative traversals. Link: https://goplay.space/#b08ePZaI0y-
package main
import (
"fmt"
)
type node struct {
Value int
Left *node
Right *node
@grevych
grevych / hash.go
Last active August 1, 2019 22:53
Hash Map structure in Go. Link: https://goplay.space/#2XYw1W3ddUf
package main
import (
"errors"
"fmt"
)
type hash struct {
vector []interface{}
length int
@grevych
grevych / structs.go
Last active July 24, 2019 22:54
Stack and Queue of integers implemented in go. Link: https://goplay.space/#nUwwilNoGs0
package main
import (
"errors"
"fmt"
)
type node struct {
value int
next *node
@grevych
grevych / list.go
Last active July 17, 2019 22:48
Linked list of integers https://goplay.space/#jOlr7VSOmrK
package main
import (
"errors"
"fmt"
)
type node struct {
value int
next *node
@grevych
grevych / find_duplicates.js
Last active March 2, 2019 00:02
Find all duplicates in an array. The elements in the array have no restrictions, but in this algorithm we'll work specifically with integers.
/*
Example:
duplicates([1, 21, -4, 103, 21, 4, 1]);
result = [1, 21]
*/
function find_duplicates(arr) {
const duplicates = {};
@grevych
grevych / chessboard_traveling.js
Created March 1, 2019 23:58
Have the function ChessboardTraveling(x, y, a, b) read str which will be a string consisting of the location of a space on a standard 8x8 chess board with no pieces on the board along with another space on the chess board. The structure of str will be the following: "(x y)(a b)" where (x y) represents the position you are currently on with x and…
/*
Example:
(1, 1) -> (1, 2) -> (2, 2)
(1, 1) -> (2, 1) -> (2, 2)
*/
function chessboardTraveling(x, y, a, b) {
let count = 0;
const queue = [];
@grevych
grevych / stack_tracker.py
Last active February 15, 2019 23:04
Stack Tracker: Limit the number of elements of a stack based in a min or max criteria.
class TrackerError(Exception):
pass
class TrackerMethodNotImplemented(TrackerError):
pass
class StackTracker(object):
def __init__(self, limit, items = None):