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 / 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 / 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 / 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 / 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 / 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 / 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.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 / quick_sort.rb
Created November 28, 2012 12:44
quick sort implementation in ruby
def quick_sort(list)
return list.dup unless list.size > 1
x = list.sample
h = list.group_by { |item| item <=> x }
h.default = []
less, equal, more = h[-1], h[0], h[1]
@vderyagin
vderyagin / git-churn.rb
Created November 19, 2012 08:25
git churn
#! /usr/bin/env ruby
IO.popen('git log --name-only --pretty=format:""')
.readlines
.map(&:chomp)
.reject(&:empty?)
.select { |file| File.exists?(File.expand_path(file, ENV['GIT_WORK_TREE'])) }
.each_with_object(Hash.new(0)) { |file, changes| changes[file] += 1 }
.to_a
.sort_by(&:last)
@vderyagin
vderyagin / helloworld.go
Created November 19, 2012 08:22
concurrent "Hello World" in Golang
package main
import "fmt"
/*
concurrent hello world
*/
func main() {
sayHello := make(chan bool)