Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@alexland
alexland / r-kernel-setup.R
Created September 17, 2018 14:32
installing & using R Kernel for Jupyter Notebook
# at the R REPL prompt
devtools::install_github('IRkernel/repr')
devtools::install_github("IRkernel/IRdisplay")
devtools::install_github("IRkernel/IRKernel")
require(IRdisplay)
@alexland
alexland / NQueen.scala
Created April 15, 2016 11:36 — forked from pathikrit/NQueen.scala
O(n!) solution to the n-Queen puzzle (https://en.wikipedia.org/wiki/Eight_queens_puzzle)
/**
* Solves the n-Queen puzzle in O(n!)
* Let p[r] be the column of the queen on the rth row (must be exactly 1 queen per row)
* There also must be exactly 1 queen per column and hence p must be a permuation of (0 until i)
* There must be n distinct (col + diag) and n distinct (col - diag) for each queen (else bishop attacks)
* @return a Vector p of length n such that p[i] is the column of the queen on the ith row
*/
def nQueens(n: Int) = (0 until n).permutations filter {p =>
p.zipWithIndex.flatMap{case (c, d) => Seq(n + c + d, c - d)}.distinct.size == 2*n
}
@alexland
alexland / crosstab.py
Last active October 26, 2021 14:04
cross tabulation in NumPy (table whose cells are counts by value over the two table dimensions)
def xtab(*cols, apply_wt=False):
'''
returns:
(i) xt, NumPy array storing the xtab results, number of dimensions is equal to
the len(args) passed in
(ii) unique_vals_all_cols, a tuple of 1D NumPy array for each dimension
in xt (for a 2D xtab, the tuple comprises the row and column headers)
pass in:
(i) 1 or more 1D NumPy arrays of integers
(ii) if wts is True, then the last array in cols is an array of weights
@alexland
alexland / timeSeriesGrid.R
Last active April 19, 2018 18:08
convert an irregular time series to one with equal intervals between data points.
require(zoo)
require(xts)
#---------------------- mock an irregular (non-uniform interval) time series -----------------------#
start <- Sys.time() - (20 * 60 * 60)
end <- Sys.time()
# create the time series index
idx_ts = seq.POSIXt(start, end, by='hours')
@alexland
alexland / fizzbuzz_scala.scala
Created December 29, 2015 02:03
fizzbuzz implemented in Scala
/**
* returns a transformed list (of strings) of the
* 1-to-100 integer list passed in such that:
* integers evenly divisible by 3 are replaced by "fizz"
* integers evenly divisible by 5 are replaced by "buzz"
* integers evenly divisible by both 3 & 5 are replaced by "fizzbuz"
* all other integers are replaced by their string representation
* */
def f[Int](q:List[Int]=(1 to 100).toList):List[String] = {
fb.foldLeft(List[String]())( (u, v) =>
@alexland
alexland / postgres-cheatsheet.md
Last active August 29, 2015 14:26 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

Magic words:

psql -U postgres

Most \d commands support additional param of __schema__.name__ and accept wildcards like *.*

  • \q: Quit/Exit
  • \c __database__: Connect to a database
  • \d __table__: Show table definition including triggers
@alexland
alexland / sbtmkdirs.sh
Last active August 29, 2015 14:26 — forked from alvinj/sbtmkdirs.sh
A shell script to create an SBT project directory structure
#!/bin/bash
#------------------------------------------------------------------------------
# Name: sbtmkdirs
# Purpose: Create an SBT project directory structure with a few simple options.
# Author: Alvin Alexander, http://alvinalexander.com
# Info: http://alvinalexander.com/sbtmkdirs
# License: Creative Commons Attribution-ShareAlike 2.5 Generic
# http://creativecommons.org/licenses/by-sa/2.5/
#------------------------------------------------------------------------------
# Bulk convert shapefiles to geojson using ogr2ogr
# For more information, see http://ben.balter.com/2013/06/26/how-to-convert-shapefiles-to-geojson-for-use-on-github/
# Note: Assumes you're in a folder with one or more zip files containing shape files
# and Outputs as geojson with the crs:84 SRS (for use on GitHub or elsewhere)
#geojson conversion
function shp2geojson() {
ogr2ogr -f GeoJSON -t_srs crs:84 "$1.geojson" "$1.shp"
}
@alexland
alexland / spark-custom-install.sh
Last active August 29, 2015 14:20
apache spark custom installation using sbt and current scala version
#---------------- custom install Apache Spark -------------------#
# instructions to build Apache Spark from source w/ current Scala & using sbt (vs maven)
# download Apache Spark src from the appropriate mirror
# untar:
tar zxf spark-1.3.1.tgz
@alexland
alexland / pipe-operator-in-scala
Last active August 29, 2015 14:20 — forked from SteveGilham/gist:e9ef541553e09be75994
pipe operator implemented in scala
package operator
object FunctionalPipeline {
class PipedObject[T] private[Functional] (value:T)
{
def |>[R] (f : T => R) = f(this.value)
}
implicit def toPiped[T] (value:T) = new PipedObject[T](value)
}