Skip to content

Instantly share code, notes, and snippets.

View Bablzz's full-sized avatar
💭
Quality is remembered long after the price is forgotten (Sir Frederick Royce)

Maksim Sokolnikov Bablzz

💭
Quality is remembered long after the price is forgotten (Sir Frederick Royce)
View GitHub Profile
@Bablzz
Bablzz / bs.rb
Last active October 27, 2020 16:10
binary search
# Best O(1)
# Worst O(long n)
arr = [1,2,3,4,5,6,7,10]
def bs (list, elem)
low = 0
high = list.length
@Bablzz
Bablzz / s_sort.rb
Last active August 8, 2018 14:51
selection sort
arr = [9,81,-9,3,0,12,-10]
def find_min(list)
min = list[0]
index = 0
list.each_with_index do |val, i|
if val < min
min = val
index = i
end
@Bablzz
Bablzz / dockerfile_spec.rb
Created August 8, 2018 14:49
Test dockefile with ruby/docker-api/serverspec
require "serverspec"
require "docker"
package = ['rake', 'pg']
describe "Dockerfile" do
before(:all) do
@image = Docker::Image.build_from_dir('.', {'dockerfile'=>'Dockerfile'})
@image.tag(repo: 'image-test', tag: 'latest').
@Bablzz
Bablzz / utf16.go
Last active February 9, 2024 06:58
Convert string UTF-8 to UTF-16LE golang
const BOM = '\ufffe' //LE. for BE '\ufeff'
func createFile(name string) error {
var bytes [2]byte
data := `test string UTF-8`
file, err := os.Create(name)
if err != nil {
fmt.Errorf("Can't open file. %v", err)
return err
@Bablzz
Bablzz / quicksort.rb
Last active October 21, 2019 09:57
quicksort in ruby
=begin
Worst time: O(n2) if choose bad pivot element
Approximate time: O(n log n)
=end
def quicksort array
if array.length < 2
array
else
pivot = array[rand(array.length)]
@Bablzz
Bablzz / Queue_bfs.rb
Last active October 13, 2020 14:14
breadth-first search, BFS
# O(|V|+|E|)
graph = Hash.new
graph["you"] = ["alice", "bob", "claire"]
graph["bob"] = ["anuj", "peggy"]
graph["alice"] = ["peggy"]
graph["claire"] = ["thom", "jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
@Bablzz
Bablzz / dijkstra.rb
Created July 1, 2019 14:06
dijkstra's_algorithm
graph = {}
graph['start'] = Hash.new
graph['start']['a'] = 6
graph['start']['b'] = 2
graph['a'] = Hash.new
graph['a']['fin'] = 1
graph['b'] = Hash.new
graph['b']['a'] = 3
graph['b']['fin'] = 5
@Bablzz
Bablzz / fibonacci.rb
Last active September 18, 2019 09:02
Fibonacci Binet
fib = 9
def BinetFib number
phi = ((1 + Math.sqrt(5)) / 2) #or 1.6180339887
divid = phi**number
divider = Math.sqrt(5)
(divid / divider).round
end
puts BinetFib(fib);
@Bablzz
Bablzz / Luhn.rb
Last active July 3, 2019 11:57
Luhn algorithm
def sum_digits digit
if digit < 10
digit
else
(digit % 10) + (digit / 10).to_f
end
end
def validate number
number = number.to_s.reverse.to_i
@Bablzz
Bablzz / greedy.rb
Last active July 9, 2019 10:01
Greedy algorithm
# Set cover problem
states = ['mt', 'wa', 'or', 'id', 'nv', 'ut', 'ca', 'az'].uniq
station = {}
station["kone"] = ['id', 'nv', 'ut'].uniq
station["ktwo"] = ['mt', 'wa', 'id'].uniq
station["kthree"] = ['or', 'nv', 'ca'].uniq
station["kfour"] = ['nv', 'ut'].uniq
station["kfive"] = ['ca', 'az'].uniq