Skip to content

Instantly share code, notes, and snippets.

View vderyagin's full-sized avatar

Victor Deryagin vderyagin

  • Ternopil, Ukraine
View GitHub Profile
@vderyagin
vderyagin / qsort.go
Created November 28, 2012 13:38
quick sort implementation in Golang
package qsort
import "math/rand"
func QuickSort(slice []int) []int {
length := len(slice)
if length <= 1 {
sliceCopy := make([]int, length)
copy(sliceCopy, slice)
@vderyagin
vderyagin / lispy_if.scala
Created December 6, 2012 17:46
lisp-style "if" condition is Scala
def lispyIf(condition: => Boolean)(thenClause: => Unit)(elseClause: => Unit) {
if (condition) thenClause
else elseClause
}
lispyIf(1 < 2) {
println("true")
} {
println("false")
}
@vderyagin
vderyagin / qsort.scala
Created December 6, 2012 20:04
quicksort in scala
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)
@vderyagin
vderyagin / array_statistics.rb
Last active December 10, 2015 08:28
monkeypatched Array with some statistical methods
class Array
def mean
inject(:+).to_f / size
end
def median
s = size
idx1 = s / 2
sorted = sort
if s.odd?
@vderyagin
vderyagin / linear_regression.rb
Created December 30, 2012 13:13
half-ass implementation of linear regression in Ruby
#! /usr/bin/env ruby
class LinearRegression
attr_reader :coords, :size
def initialize(coords)
@coords = coords
@size = coords.size
end
@vderyagin
vderyagin / square_root_benchmark.rb
Last active December 10, 2015 11:18
Benchmarking of square root getting operations in different Ruby implementations.
require 'benchmark'
=begin
Math.sqrt(x) is consistently slower then x ** 0.5 in all major ruby implementations.
=end
SIZE = 1e6
@vderyagin
vderyagin / json_query.rb
Last active December 10, 2015 14:19
querying json data
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
@vderyagin
vderyagin / binary_tree_traversal.rb
Last active December 11, 2015 02:48
traversal of binary tree
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
@vderyagin
vderyagin / binary_tree_traversal.go
Last active December 11, 2015 03:38
Go implementation of binary tree data structure with methods for traversal and finding lowest common ancestor for nodes.
package main
import (
"fmt"
"strconv"
"strings"
)
type Node struct {
Data interface{}
@vderyagin
vderyagin / qsort.hs
Last active December 11, 2015 07:09
quicksort implementation in haskell
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