Skip to content

Instantly share code, notes, and snippets.

View lkrych's full-sized avatar

Leland Krych lkrych

  • Cisco
  • San Francisco, CA
View GitHub Profile
@lkrych
lkrych / sortListSkeleton.go
Created July 29, 2018 17:25
Skeleton for sorting input
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"time"
@lkrych
lkrych / testingAStack.go
Created June 13, 2018 16:09
test our stack implementation
func TestCreatingAStack(t *testing.T) {
s := &stack{
data: []int{1, 2, 3, 4, 5, 6},
}
testPop := s.pop()
testPop2 := s.pop()
if len(s.data) != 4 {
t.Errorf("Expected pop to return elements from the top of the stack: %v, popped: %v", s.data, testPop)
}
@lkrych
lkrych / stackWIthMinImp2.go
Created June 13, 2018 16:07
A more space-efficient implementation of stack with minimum
type stackWithMin struct {
data []int
minStack stack //utilizes the stack implementation we defined above!
}
func (s *stackWithMin) pop() int {
popped := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
if popped == s.min() { //if you've popped the min, move it off the minStack!
s.minStack.pop()
@lkrych
lkrych / TestingAMinStack.go
Created June 13, 2018 16:00
testing stack with minimum
func TestCreatingAMinStack(t *testing.T) {
s := &stackWithMin{}
for i := 1; i < 7; i++ {
s.push(i)
}
testPop := s.pop()
testPop2 := s.pop()
if len(s.data) != 4 {
t.Errorf("Expected pop to return elements from the top of the stack: %v, popped: %v", s.data, testPop)
@lkrych
lkrych / stackWithMinImp1.go
Created June 13, 2018 15:57
Stack with minimum using expanded data type
//the key to this implementation is using a different data structure in the stack.
//each val in the stack contains the current minimum at the time of insertion.
type stackVal struct {
val int
min int
}
type stackWithMin struct {
data []stackVal
}
@lkrych
lkrych / stack.go
Created June 13, 2018 15:39
Stack implementation in Go
type stack struct { //use a struct to define the stack
data []int
}
func (s *stack) pop() int {
popped := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1] //resize the internal slice
return popped
}
@lkrych
lkrych / linked_list_add_optimized.go
Created May 23, 2018 02:09
solution for more optimized link_list
func optimizedAddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
carry := 0
sum := 0
//this is the starting node, we need to return this
head := ListNode{
Val: 0,
Next: nil,
}
//create a current node to save to
//struct we will use for Linked list
type ListNode struct {
Val int
Next *ListNode
}
//the function that puts it all together
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
n1 := createNumber(l1)
n2 := createNumber(l2)
@lkrych
lkrych / reverse_int.js
Created April 24, 2018 15:48
solution for reversing int in javascript
/**
* @param {number} x
* @return {number}
*/
var reverse = function(x) {
let str = x.toString().split("");
let reverse = 0;
if (str[0] === "-"){
reverse = parseInt( "-" + str.slice(1).reverse().join(""));
} else {
@lkrych
lkrych / reverse_int.go
Created April 24, 2018 15:46
Solution for reversing an integer
func reverse(x int) int {
neg := 1
ans := 0
if x < 0 {
//make sure to only work with positive numbers
x *= -1
neg = -1
}
//the business loop!