Skip to content

Instantly share code, notes, and snippets.

View bogdanovich's full-sized avatar

Anton Bogdanovich bogdanovich

View GitHub Profile
@bogdanovich
bogdanovich / balance_parens.rb
Last active July 16, 2016 05:15
Balance parens
# Given a string with possibly unbalanced parentheses, return a string with balanced
# parentheses removing a minimal number of parentheses.
# Example: (abc)((),
# Output: (abc)()
def balance_parens(str)
stack = []
str.each_char.with_index do |char, i|
case char
when '('
@bogdanovich
bogdanovich / dijkstra.rb
Created June 30, 2016 05:09
Dijkstra algorithm
# dijkstra algorithm
Infinity = 1.0/0
class Graph
attr_accessor :nodes
def initialize
@nodes = {}
end
@bogdanovich
bogdanovich / heap.rb
Last active September 15, 2022 22:50
Heap data structure in ruby
class Heap
def initialize(type = :min)
@type = type
if @type == :min
@heap = [-1.0/0]
@compare = ->(a,b) { (a <=> b) < 0 }
elsif @type == :max
@heap = [1.0/0]
@compare = ->(a,b) { (b <=> a) > 0 }
else
def flatten(dict)
output_dict = {}
dict.each do |k,v|
if v.is_a? Hash
sub_dict = flatten(v)
sub_dict.each do |sk, sv|
output_dict[k+'.'+sk] = sv
end
else
output_dict[k] = v
@bogdanovich
bogdanovich / api_spyonweb_example.php
Last active June 11, 2016 02:56
API Spyonweb.com PHP example
<?php
$site = "fullmooncalendar.net";
$url = 'https://api.spyonweb.com/v1/domain/'.$site.'?access_token=YOUR_TOKEN_HERE';
$response = file_get_contents($url);
$data = json_decode($response, true);
echo "<pre>\r\n";
if ($data['status'] == "found") {
echo "Status: ".$data['status']."\r\n";
// domain summary
package main
import (
"crypto/rand"
"encoding/binary"
"log"
"github.com/syndtr/goleveldb/leveldb"
)