Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@cgopalan
cgopalan / ListMap.scala
Created October 6, 2011 02:59
Scala Map function
object ListMap {
def squareList(xs: List[Int]): List[Int] = xs match {
case List() => xs
case y :: ys => y * y :: squareList(ys)
}
def squareListMap(xs: List[Int]): List[Int] =
xs map (x => x * x)
def main(args: Array[String]) {
println("squarelist for square of (9,13,21) returns : " + squareList(List(9, 13, 21)));
@cgopalan
cgopalan / ListFilter.scala
Created October 6, 2011 03:06
Forall and Exists in term of Filter
object ListFilter {
def forall(xs: List[Int], p: Int => Boolean): Boolean =
xs.isEmpty || xs.filter(p) == xs
def exists(xs: List[Int], p: Int => Boolean): Boolean =
!xs.isEmpty && !xs.filter(p).isEmpty
def main(args: Array[String]) {
println("Are members of list (1,2,3,4,5) less than 10? : " +
forall(List(1,2,3,4,5), (x => x < 10)));
println("Is there at least one member of list (1,2,3,4,5) thats greater than 5? : " +
@cgopalan
cgopalan / NQueensProblem.scala
Created October 6, 2011 03:26
Function isSafe for NQueens problem
object NQueensProblem {
def queens(n: Int): List[List[Int]] = {
def placeQueens(k: Int): List[List[Int]] =
if (k == 0) List(List())
else for { queens <- placeQueens(k - 1)
column <- List.range(1, n + 1)
if isSafe(column, queens, 1)} yield column :: queens
placeQueens(n)
}
def isSafe(col: Int, queens: List[Int], delta: Int): Boolean = queens match {
@cgopalan
cgopalan / ForToMap.scala
Created October 6, 2011 03:37
Translate for to higher-order functions
object ForToMap {
def main(args: Array[String]) {
case class Book(title: String, authors: List[String])
val books: List[Book] = List(
Book("Structure and Interpretation of Computer Programs",
List("Abelson, Harold", "Sussman, Gerald J.")),
Book("Principles of Compiler Design",
List("Aho, Alfred", "Ullman, Jeffrey")),
Book("Programming in Modula2",
List("Wirth, Niklaus")),
@cgopalan
cgopalan / FunctionalLoops.scala
Created October 6, 2011 03:48
RepeatLoop and RepeatLoop-Until
object FunctionalLoops {
def repeatLoop(command: => Unit)(condition: => Boolean) {
command
if (condition) {
repeatLoop(command)(condition)
} else ()
}
def repeatUntilLoop(command: => Unit) = {
class RepeatUntilLoopClass(command: => Unit) {
@cgopalan
cgopalan / SimpleOrGate.scala
Created October 6, 2011 21:49
Simple OR Gate
object SimpleOrGate {
type Action = () => Unit
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s
@cgopalan
cgopalan / CompoundOrGate.scala
Created October 6, 2011 21:57
OR Gate as combination of inverter and AND gate
object CompoundOrGate {
type Action = () => Unit
class Wire {
private var sigVal = false
private var actions: List[Action] = List()
def getSignal = sigVal
def setSignal(s: Boolean) =
if (s != sigVal) {
sigVal = s
@cgopalan
cgopalan / autocomplete.py
Created January 7, 2012 17:55
Autocomplete for python interpreter
import rlcompleter, readline
readline.parse_and_bind('tab: complete')
@cgopalan
cgopalan / mergesort.py
Created March 24, 2012 22:17
Mergesort In Python
def mergesort(arr, n):
if n == 1: return arr
len_half = int(n/2)
return merge(mergesort(arr[:len_half], len_half),
mergesort(arr[len_half:], n - len_half), n)
def merge(arr1, arr2, n):
result = []
i,j = 0,0
for x in range(n):
@cgopalan
cgopalan / search_all_org_repos.py
Created February 25, 2015 21:34
Searches all repos in an organization for a text
"""
Search for a text in all repos for an organization.
Currently via the web UI you can only search per repo.
This will only return number of matches for the text and not
the filenames (as of now).
You need an OAuth Token for this to work.
Run it as: