Skip to content

Instantly share code, notes, and snippets.

@dholbrook
dholbrook / sudoku.scala
Created September 4, 2012 15:38
brute force Sudoku solver
/**
* D Holbrook
*
* Code Club: Sudoku solver
*
* Brute force solver
*/
object Sudoku extends App {
val valid = Range(1, 10)
@dholbrook
dholbrook / Tree.scala
Created June 21, 2012 17:59
Scala binary tree
/**
* D Holbrook
*
* Code Club: PO1
*
* (*) Define a binary tree data structure and related fundamental operations.
*
* Use whichever language features are the best fit (this will depend on the language you have selected). The following operations should be supported:
*
* Constructors
@dholbrook
dholbrook / S9926.scala
Created May 17, 2012 16:08
S99 problem 26 Generate the combinations of K distinct objects
package scalaninetynine
import scala.annotation.tailrec
import scala.collection.Iterable
import scala.collection.IndexedSeq
/*
* S99 P6 http://aperiodic.net/phil/scala/s-99/
*
* Generate the combinations of K distinct objects chosen from the N elements of a list.
* In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficient). For pure mathematicians, this result may be great.
* But we want to really generate all the possibilities.
@dholbrook
dholbrook / S9922.scala
Created April 19, 2012 03:11
S99 P22 Create a list containing all integers within a given range.
import scala.annotation.tailrec
/*
* S99 P22 http://aperiodic.net/phil/scala/s-99/
*
* Create a list containing all integers within a given range.
* Example:
* scala> range(4, 9)
* res0: List[Int] = List(4, 5, 6, 7, 8, 9)
*
/*
* S99 P19 http://aperiodic.net/phil/scala/s-99/
*
* (**) Rotate a list N places to the left.
*
* Examples:
*
* scala> rotate(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))
* res0: List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c)
*
@dholbrook
dholbrook / vagrant_postinstall.sh
Created March 24, 2012 05:27
vagrant centos 5.8 post install
####################
# centos 5.8
# post installation
# adapted from
# https://github.com/jedi4ever/veewee/tree/master/templates/CentOS-5.7-i386-netboot
####################
yum -y install sudo
/usr/sbin/groupadd vagrant
@dholbrook
dholbrook / S9916.scala
Created March 8, 2012 17:56
S99 Problem 16
/*
* S99 P16 http://aperiodic.net/phil/scala/s-99/
* (**) Drop every Nth element from a list.
*
* Example:
*
* scala> drop(3, List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k))
* res0: List[Symbol] = List('a, 'b, 'd, 'e, 'g, 'h, 'j, 'k)
*
*/
/*
* S99 P15 http://aperiodic.net/phil/scala/s-99/
* (**) Duplicate the elements of a list a given number of times.
*
* Example:
*
* scala> duplicateN(3, List('a, 'b, 'c, 'c, 'd))
* res0: List[Symbol] = List('a, 'a, 'a, 'b, 'b, 'b, 'c, 'c, 'c, 'c, 'c, 'c, 'd, 'd, 'd)
*/
@dholbrook
dholbrook / S9912.scala
Created February 16, 2012 18:50
S99 P12
/*
* S99 P12 http://aperiodic.net/phil/scala/s-99/
*
* Decode a run-length encoded list.
*
* Given a run-length code list generated as specified in problem P10,
* construct its uncompressed version.
*
* Example:
*
package scalaninetynine
/*
* S99 P11 http://aperiodic.net/phil/scala/s-99/
* Modified run-length encoding.
* Modify the result of problem P10 in such a way that if an element has no
* duplicates it is simply copied into the result list. Only elements with
* duplicates are transferred as (N, E) terms.
*
* Example:
*