This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package qsort | |
import "math/rand" | |
func QuickSort(slice []int) []int { | |
length := len(slice) | |
if length <= 1 { | |
sliceCopy := make([]int, length) | |
copy(sliceCopy, slice) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lispyIf(condition: => Boolean)(thenClause: => Unit)(elseClause: => Unit) { | |
if (condition) thenClause | |
else elseClause | |
} | |
lispyIf(1 < 2) { | |
println("true") | |
} { | |
println("false") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def qSort[T <% Ordered[T]](list: List[T]): List[T] = list match { | |
case Nil => Nil | |
case l @ List(x) => l | |
case x :: xs => { | |
val (less, more) = xs.partition(_ < x) | |
qSort(less) ::: x :: qSort(more) | |
} | |
} | |
assert(qSort(Nil) == Nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def mean | |
inject(:+).to_f / size | |
end | |
def median | |
s = size | |
idx1 = s / 2 | |
sorted = sort | |
if s.odd? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
class LinearRegression | |
attr_reader :coords, :size | |
def initialize(coords) | |
@coords = coords | |
@size = coords.size | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
=begin | |
Math.sqrt(x) is consistently slower then x ** 0.5 in all major ruby implementations. | |
=end | |
SIZE = 1e6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
data1 = JSON.load('{"status": "fail", "messages": ["Out of capacity"]}') | |
data2 = JSON.load('{"status": "success", "messages": [], "result": {"node": {"ip": "1.2.3.4", "description": "", "id": 974, "name": "VM#3"}}}') | |
def get_from_json(data, query) | |
query.split('.').inject(data) do |memo, key| | |
key = key.to_i if memo.is_a?(Array) | |
memo.fetch(key) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
attr_accessor :data, :left, :right | |
def initialize(d, l = nil, r = nil) | |
self.data = d | |
self.left = l | |
self.right = r | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
type Node struct { | |
Data interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
assert :: Bool -> () | |
assert False = error "assertion failed" | |
assert True = () | |
qSort :: Ord a => [a] -> [a] | |
qSort [] = [] | |
qSort [x] = [x] | |
qSort (x:xs) = qSort less ++ [x] ++ qSort more |