Skip to content

Instantly share code, notes, and snippets.

View vderyagin's full-sized avatar

Victor Deryagin vderyagin

  • Ternopil, Ukraine
View GitHub Profile
# -*- coding: utf-8 -*-
require 'prime'
def Ring(size)
Ring.new(size)
end
class Ring
attr_reader :size
package int_palindrome
func isPalindrome(n int) bool {
reverse := 0
for t := n; t > 0; t /= 10 {
reverse *= 10
reverse += t % 10
}
@vderyagin
vderyagin / string_combinations.rb
Last active December 12, 2015 05:29
Find all combinations of characters in given string
def combinations(s)
return [s] if s.length == 1
chars = s.chars.to_a
chars.each_with_object([]) do |ch, cs|
rest = chars.drop(chars.find_index(ch) + 1).join
cs << ch
combinations(rest).each do |comb|
cs << (ch + comb)
@vderyagin
vderyagin / string_permutations.rb
Last active December 12, 2015 05:28
find all permutations of characters in given string
def permutations(s)
return [s] if s.length == 1
chars = s.chars.to_a
chars.each_with_object([]) do |ch, ps|
rest = chars.reject { |c| c.equal? ch }.join
permutations(rest).each do |perm|
ps << (ch + perm)
end
package atoi_itoa
import (
"fmt"
"regexp"
)
func atoi(s string) (int, error) {
re := regexp.MustCompile(`^-?\d+$`)
package sorted_linked_list
import (
"bytes"
"fmt"
)
type list struct {
head *node
}
package hash_table
import "fmt"
type bucket struct {
head *entry
}
type entry struct {
key string
@vderyagin
vderyagin / stack.go
Last active December 11, 2015 17:08
Thread-safe implementation of Stack data structure in Go
package stack
import (
"bytes"
"errors"
"fmt"
"sync"
)
type node struct {
@vderyagin
vderyagin / queue.go
Last active December 11, 2015 17:08
Thread-safe implementation of Queue data structure in Go
package queue
import (
"bytes"
"errors"
"fmt"
"sync"
)
type node struct {
@vderyagin
vderyagin / bag.go
Last active December 11, 2015 15:09
Bag data structure implemented in Go
package bag
import (
"bytes"
"errors"
"fmt"
)
type bag struct {
m map[interface{}]int