Skip to content

Instantly share code, notes, and snippets.

View dimitrilw's full-sized avatar

Dimitri dimitrilw

  • Colorado
  • 08:14 (UTC -06:00)
View GitHub Profile
@dimitrilw
dimitrilw / pipista.py
Created June 13, 2016 13:03 — forked from pudquick/pipista.py
pipista - pip module (for installing other modules) for Pythonista
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):
@dimitrilw
dimitrilw / logger.go
Last active July 15, 2023 02:09
golang default logger for use in coding challenges
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)
@dimitrilw
dimitrilw / filter_slice.go
Last active July 20, 2023 19:51
golang filter slice
// 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++
@dimitrilw
dimitrilw / iota.go
Last active July 20, 2023 19:49
Go (golang) magic constants via "iota"
// use "iota" for enumerated constants, like this:
const (
a = iota // 0
b // 1
c // 2
//...
n // n
)
@dimitrilw
dimitrilw / lsb.go
Last active July 31, 2023 22:27
go (golang) least significant bit (lsb) / last set bit
// 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))
}
}
@dimitrilw
dimitrilw / custom_sort.go
Last active August 9, 2023 12:14
Go (golang) custom sort
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,
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
@dimitrilw
dimitrilw / gcd.go
Last active July 30, 2023 22:05
Go (golang) greatest common denominator
// 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)
}
@dimitrilw
dimitrilw / factors.go
Last active August 18, 2023 15:10
Go (golang) get all factors of an int
// 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)
}
}
@dimitrilw
dimitrilw / fenwick.go
Created July 31, 2023 22:48
Fenwick tree
// 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]