Skip to content

Instantly share code, notes, and snippets.

@hkurokawa
hkurokawa / ReverseLinkedList.java
Created February 12, 2019 13:23
Reverse a LinkedList
public class ReverseLinkedList {
static class Node {
int value;
Node next;
Node(int value, Node next) {
this.value = value;
this.next = next;
}
@Override public String toString() {
@hkurokawa
hkurokawa / contracts.kt
Created February 12, 2019 07:37
Kotlin Contracts sample
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
class Foo {
fun foo() {}
}
@ExperimentalContracts
inline fun Foo.bar(block: Foo.() -> Unit) {
import java.util.Stack
fun parse(s: String): MutableList<Any> {
val stack = Stack<MutableList<Any>>()
stack.add(mutableListOf())
var i = 0
while (i < s.length) {
val ch = s[i]
when (ch) {
'[' -> stack.push(mutableListOf())
@hkurokawa
hkurokawa / LucasTest.java
Created January 18, 2019 10:48
LucasTest
import java.math.BigInteger;
public class LucasTest {
public static void main(String[] args) {
int p = 521;
System.out.println(lucas(p - 1, BigInteger.valueOf(2).pow(p).subtract(BigInteger.ONE)));
}
static BigInteger lucas(int n, BigInteger mod) {
return lucasIter(BigInteger.valueOf(4), n, mod);
@hkurokawa
hkurokawa / MyLinkedList.kt
Last active December 17, 2018 05:51
Sample LinkedList implementation supporting flatten()
import java.lang.IllegalStateException
class MyLinkedList : Iterable<Any> {
var head: Node = Node(Any())
fun add(value: Any) {
var n: Node = head
while (n.next != null) {
n = n.next!!
}
@hkurokawa
hkurokawa / UTF8Encoder.kt
Last active November 17, 2018 10:04
UTF8 encode/decoder in Kotlin
import java.io.ByteArrayOutputStream
@ExperimentalUnsignedTypes
inline class Unicode(val point: Int)
@ExperimentalUnsignedTypes
object UTF8Encoder {
fun encode(unicodes: Array<Unicode>): ByteArray {
val array = ByteArrayOutputStream()
for (code in unicodes) {
class DrawSpiral {
public static void main(String[] args) {
int size = 15;
double cx = size / 2.d;
double cy = size / 2.d;
double tmax = 6 * Math.PI;
double a = size / 2.d / tmax;
boolean[][] res = new boolean[size][size];
@hkurokawa
hkurokawa / Covered.java
Last active September 24, 2018 00:48
Extract the covered area for the given segments
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
class Covered {
public static void main(String[] args) {
final int[][] input = new int[][] {
{1, 5}, {2, 8}, {9, 10}, {10, 15}, {3, 6}, {18, 20}
};
@hkurokawa
hkurokawa / crawler.py
Last active April 28, 2018 09:48
Twitter crawler for Elasticsearch 6.x. This crawler is just crawling my twitter timeline and index the tweets.
import sys
import time
from os import environ
import elasticsearch
import twitter
def sanitise_place(place):
if place is None or 'bounding_box' not in place or place['bounding_box']['type'] != 'Polygon':
@hkurokawa
hkurokawa / config.json
Last active March 8, 2018 07:11
A query driver of rankin for issuing multiple queries to Elasticsearch
{
"run": {
"cluster":"test_cluster"
},
"jobs": [
{
"job_id": "job1",
"concurrency": 1,
"driver": "queries",
"cycle_operations": true,