Skip to content

Instantly share code, notes, and snippets.

View cgopalan's full-sized avatar

Chandrakant Gopalan cgopalan

View GitHub Profile
@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:
import csv
import json
input_file = 'PROD_CUSTOMERS_GOOD.csv'
output_file = 'wowcher_customer_creation_formatted.json'
with open(input_file) as infile:
infile.readline()
with open(output_file, 'w') as outfile:
@cgopalan
cgopalan / scala_quicksort.scala
Created September 2, 2011 18:22
Scala Quicksort
object QuickSort {
def sort(xs: Array[Int]): Array[Int] = {
if (xs.length <= 1) xs
else {
val pivot = xs(xs.length / 2)
val lessThanPivot = xs filter (pivot >)
val equalToPivot = xs filter (pivot ==)
val greaterThanPivot = xs filter (pivot <)
println("Pivot for array [%s] is %d".format(xs.deep.mkString(" "),
pivot))
@cgopalan
cgopalan / tailrecursivefactorial.scala
Created September 6, 2011 18:56
Scala Tail Recursive Factorial
object TailRecursiveFactorial {
def factorial(n: Int, product: Int): Int =
if (n == 1) product else factorial(n - 1, n * product)
def main(args: Array[String]) {
println("Factorial of 8 is: " + factorial(8,1))
}
}
@cgopalan
cgopalan / higherorderfunctions.scala
Created September 13, 2011 04:04
Scala product
object HigherOrderFunctions {
def product(f: Int => Int)(a: Int, b:Int): Int = {
if (a > b) 1 else f(a) * product(f)(a + 1, b)
}
def main(args: Array[String]) {
println("Product of range of numbers from 3 to 8 is: " + product(x => x)(3,8))
}
}
@cgopalan
cgopalan / factorial2.scala
Created September 13, 2011 04:27
Scala Factorial in terms of a product function
object Factorial {
def product(f: Int => Int)(a: Int, b:Int): Int = {
if (a > b) 1 else f(a) * product(f)(a + 1, b)
}
def factorial(n: Int): Int = {
product(x => x)(1, n)
}
def main(args: Array[String]) {
println("Factorial of 6 is: " + factorial(6))
}
@cgopalan
cgopalan / OperatorOverRange.scala
Created September 13, 2011 04:41
Scala generalized sum/product function
object OperatorOverRange {
def operateOverRange(f: Int => Int)(operate: (Int, Int) => Int)(base: Int)(a: Int, b:Int): Int =
if (a > b) base else operate(f(a), operateOverRange(f)(operate)(base)(a + 1, b))
def main(args: Array[String]) {
println("Sum of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x + y)(0)(3,8))
println("Product of range of numbers from 3 to 8 is: " + operateOverRange(x => x)((x,y) => x * y)(1)(3,8))
}
}
@cgopalan
cgopalan / cuberoot.scala
Created September 14, 2011 21:16
Scala Cube Root
object FixedPoint {
var tolerance = 0.0001
def abs(x: Double) = if (x >= 0) x else -x
def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < tolerance
def fixedPoint(f: Double => Double)(firstGuess: Double) = {
def iterate(guess: Double): Double = {
val next = f(guess)
println(next)
if (isCloseEnough(guess, next)) next
else iterate(next)
@cgopalan
cgopalan / IntTreeTest.scala
Created September 23, 2011 19:18
Case Classes and Pattern Matching
object IntTreeTest {
abstract class IntTree
case object EmptyTree extends IntTree
case class Node(elem: Int, left: IntTree, right: IntTree) extends IntTree
def contains(t: IntTree, v: Int): Boolean = t match {
case EmptyTree => false
case Node(e, l, r) =>
if (v < e) contains(l, v)
else if (v > e) contains(r, v)
@cgopalan
cgopalan / ListSort.scala
Created September 23, 2011 19:20
List Insertion Sort
object ListSort {
def isort(xs: List[Int]): List[Int] =
if (xs.isEmpty) Nil
else insert(xs.head, isort(xs.tail))
def insert(head: Int, xs: List[Int]): List[Int] =
if (xs.isEmpty) List(head)
else if (head > xs.head) xs.head :: insert(head, xs.tail)
else head :: xs
def main(args: Array[String]) {