This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sumSlice(slice []int) int { | |
tot := 0 | |
for _, val := range slice { | |
tot += val | |
} | |
return tot | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_list(lst): | |
tot = 0 | |
for val in lst: | |
tot += val | |
return tot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_lst(lst): | |
tot = 0 | |
for val in lst: | |
tot += val | |
return tot | |
# or return sum(lst) ¯\_(ツ)_/¯ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Walk walks the tree t sending all values | |
// from the tree to the channel ch. | |
func Walk(t *tree.Tree, ch chan int) { | |
recursiveWalk(t, ch) | |
close(ch) // close channel when done | |
} | |
func recursiveWalk(t *tree.Tree, ch chan int) { | |
if t != nil { | |
// in order |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func twoSum(nums []int, target int) []int { | |
// map from int -> int | |
m := make(map[int]int) | |
// python like enumerate | |
for i, num := range(nums) { | |
// get value and check existence in map | |
// and in if statement where ok is the boolean comparison value | |
if index, ok := m[target - num]; ok { | |
// construct array and return values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* type ListNode struct { | |
* Val int | |
* Next *ListNode | |
* } | |
*/ | |
func deleteDuplicates(head *ListNode) *ListNode { | |
// check head isn't nil (null) | |
if head == nil { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func searchRange(nums []int, target int) []int { | |
// empty nums case | |
if len(nums) == 0 { | |
return []int{-1, -1} | |
} | |
// find starting and ending | |
return []int{findStarting(nums, target), findEnding(nums, target)} | |
} | |
func findStarting(nums []int, target int) int { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type LRUCache struct { | |
capacity int | |
cache map[int]*node // map | |
head, tail *node // linked list | |
} | |
type node struct { | |
prev, next *node | |
key, val int | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ free -m | |
total used free shared buff/cache available | |
Mem: 990 78 518 4 392 867 | |
Swap: 0 0 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ free -m | |
total used free shared buff/cache available | |
Mem: 990 127 453 4 410 818 | |
Swap: 0 0 0 |