This file contains 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
import os, os.path, sys, urllib2, requests | |
class PyPiError(Exception): | |
def __init__(self, value): | |
self.value = value | |
def __str__(self): | |
return repr(self.value) | |
def _chunk_report(bytes_so_far, chunk_size, total_size): | |
if (total_size != None): |
This file contains 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
import ( | |
// most already have these imported; putting here just in case | |
"log" | |
"os" | |
) | |
var ( | |
// logger = log.New(os.Stdout, "", log.Lshortfile) | |
logger1 = log.New(os.Stdout, "optional prefix: ", log.LstdFlags|log.Lshortfile) | |
logger2 = log.New(os.Stdout, "log 2: ", log.Lshortfile) |
This file contains 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
// remember that "slice magic" means that the underlying array of data | |
// beyond the 'i'th value may still be available, so the slice should not | |
// be exported outside of the package, like to third-party software | |
func demoFilter(s []int) []int { | |
i := 0 | |
for _, v := range s { | |
if v > 1 { // some criteria; this is the filter | |
s[i] = v | |
i++ |
This file contains 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
// use "iota" for enumerated constants, like this: | |
const ( | |
a = iota // 0 | |
b // 1 | |
c // 2 | |
//... | |
n // n | |
) |
This file contains 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
// for use with signed numbers, like an int, *not* a uint | |
func lsb(n int) int { return n & (-n) } | |
func demo() { | |
for i := 0; i < 10; i++ { | |
fmt.Printf("i %d as bits %04b lsb %b\n", i, i, lsb(i)) | |
} | |
} |
This file contains 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
package main | |
import ( | |
"fmt" | |
"sort" | |
) | |
// Slices implements the Go standard lib's Sort interface | |
// by having Len, Less, and Swap methods. | |
// This particular demo implementation sorts multiple slices, |
This file contains 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
brew tap homebrew/cask-fonts | |
brew install --cask font-cascadia-code | |
brew install --cask font-cascadia-code-pl | |
brew install --cask font-cascadia-mono | |
brew install --cask font-cascadia-mono-pl |
This file contains 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
// greatest common denominator | |
func gcd(a, b int) int { | |
if b > a { a, b = b, a } | |
if b == 0 { return a } | |
return gcd(b, a%b) | |
} |
This file contains 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
// returns unsorted slice of *all* factors, not just primes | |
func getFactors(n int) []int { | |
res := []int{} | |
for i := 1; i <= int(math.Sqrt(float64(n))); i++ { | |
if n%i == 0 { | |
res = append(res, i) | |
if i*i != n { | |
res = append(res, n/i) | |
} | |
} |
This file contains 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
// https://en.wikipedia.org/wiki/Fenwick_tree | |
// https://www.youtube.com/watch?v=uSFzHCZ4E-8 | |
type FenwickTree []int | |
// returns sum of all elements up to index 'i' | |
func (ft FenwickTree) sum(i int) int { | |
res := 0 | |
for ; i > 0; i -= lsb(i) { | |
res += ft[i] |
OlderNewer