Skip to content

Instantly share code, notes, and snippets.

View alexland's full-sized avatar

doug ybarbo alexland

View GitHub Profile
@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 / 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 / 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)
}
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal