Skip to content

Instantly share code, notes, and snippets.

View SergeyNarozhny's full-sized avatar

SergeyNarozhny

  • Saint-Petersburg, Russia
View GitHub Profile
@SergeyNarozhny
SergeyNarozhny / Twins.js
Created February 20, 2018 06:29
Twin Strings. Two strings, a and b, are said to be twins only if they can be made equivalent by performing some number of operations on one or both strings...
function Twins(a, b) {
var result = Array(a.length),
swap = function(arr, index) {
var tmp = arr[index];
arr[index] = arr[index + 2];
arr[index + 2] = tmp;
return arr;
},
swapBack = function(str, index) {
var arr = str.split('');
import heapq
from collections import Counter, namedtuple
class Node(namedtuple("Node", ["left", "right"])):
def walk(self, code, acc):
self.left.walk(code, acc + "0")
self.right.walk(code, acc + "1")
class Leaf(namedtuple("Leaf", ["char"])):
def walk(self, code, acc):
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <iostream>
#include <string>
#include <queue>
#include <tuple>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cassert>
#include <ios>
#include <cinttypes>
#include <iostream>
#include <vector>
struct Item final {
int weight;
int value;
def test(gcd, n_iter=100):
for i in range(n_iter):
c = random.randint(0, 1024);
a = c * random.randint(0, 128);
b = c * random.randint(0, 128);
assert gcd(a, a) == gcd(a, 0) == a
assert gcd(b, b) == gcd(b, 0) == b
assert gcd(a, 1) == gcd(b, 1) == 1
d = gcd(a, b);
assert a % d == b % d == 0
def memo(f):
cache = {};
def inner(n):
if n not in cache:
cache[n] = f(n);
return cache[n];
return inner;
#include <cassert>
#include <cstdint>
#include <iostream>
template <class Int>;
Int gcd(Int a, Int b) {
assert(a > 0 && b > 0);
Int current_gcd = 1;
for (Int d = 1; d * d <= a; d++) {
#include <cassert>
#include <iostream>
#include <unordered_map>
class Fibonacci final {
public:
static int get(int n) {
assert(n >= 0);
if (n == 0 || n == 1) {
return n;
@SergeyNarozhny
SergeyNarozhny / eqbintree.go
Created March 25, 2017 18:47
Equivalent Binary Trees
package main
import (
"golang.org/x/tour/tree"
"fmt"
)
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
// [1,2,3,4,5].duplicator()
Array.prototype.duplicator = function() {
var arr = this;
if (!this.length) return;
Array.prototype.map.call(this, function(el){ arr.push(el); });
return arr;
}
// sum(1)(2)(3)();
function sum(x) {