Skip to content

Instantly share code, notes, and snippets.

import math
class CollidingCircles(object):
def get_expected_value(self, radii, steps_count):
return math.pi * self.__get_expected_value_for_step(radii, steps_count)
def __get_expected_value_for_step(self, radii_set, step_idx):
if step_idx == 0:
return sum(radius * radius for radius in radii_set)
def _get_next_words(self, word):
if word not in self.next_words_dict:
self._generate_next_words_list(word)
return self.next_words_dict[word]
def _generate_next_words_list(self, word):
next_words = []
word_array = list(word)
for i in range(len(word_array)):
def _bfs(self):
word_queue = [(self.begin_word, 1)]
word_enqueued = {self.begin_word: True}
while word_queue:
word, level = word_queue.pop(0)
self.word_levels[word] = level
if word == self.end_word: return level
queue = [hit]
pop hit, enqueue hot
queue = [hot]
pop hot, enqueue dot, lot
queue = [dot, lot]
pop dot, enqueue dog, ignore lot
queue = [lot, dog]
begin_word = "hit"
end_word = "cog"
dictionary = "hot", "dot", "dog", "lot", "log", "cog"
class WeightedQuickUnion(object):
def __init__(self, size):
self.group_count = self.size= size
self.group = [i for i in range(size)]
self.tree_size = [1] * size
def union(self, child, parent):
child_root = self.find(child)
parent_root = self.find(parent)
class memoize(dict):
def __init__(self, func):
self.func = func
def __call__(self, *args):
return self[args]
def __missing__(self, key):
result = self[key] = self.func(*key)
anuvrat@ ~/Documents/goWorkspace $ go run hw.go
hello, world
anuvrat@ ~/Documents/goWorkspace $ go install hw.go
go install: no install location for .go files listed on command line (GOBIN not set)
✘ anuvrat@ ~/Documents/goWorkspace $ export GOBIN=/Users/anuvrat/Documents/goWorkspace/bin
anuvrat@ ~/Documents/goWorkspace $ bin/hw
hello, world
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
anuvrat@ ~/Documents $ mkdir -p goWorkspace
anuvrat@ ~/Documents $ cd goWorkspace
anuvrat@ ~/Documents/goWorkspace $ export GOPATH=/Users/anuvrat/Documents/goWorkspace
anuvrat@ ~/Documents/goWorkspace $ vi hw.go