This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(''); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cassert> | |
#include <cstddef> | |
#include <iostream> | |
#include <string> | |
#include <queue> | |
#include <tuple> | |
#include <unordered_map> | |
#include <vector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cassert> | |
#include <ios> | |
#include <cinttypes> | |
#include <iostream> | |
#include <vector> | |
struct Item final { | |
int weight; | |
int value; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def memo(f): | |
cache = {}; | |
def inner(n): | |
if n not in cache: | |
cache[n] = f(n); | |
return cache[n]; | |
return inner; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// [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) { |
NewerOlder