Skip to content

Instantly share code, notes, and snippets.

View jinahya's full-sized avatar

Jin Kwon jinahya

  • Wemakeprice
  • Seoul, Korea
View GitHub Profile
import java.io.IOException;
/**
* A class for generateing <i>counterstring</i>s.
*
* @author Jin Kwon &lt;onacit_at_gmail.com&gt;
* @see <a href="http://www.satisfice.com/blog/archives/22">Counterstrings: Self-Describing Test Data</a>
*/
public class CounterString {
import java.util.Stack;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
/**
* A program solves <a href="https://en.wikipedia.org/wiki/Tower_of_Hanoi">Tower of Hanoi</a>.
*
* @author Jin Kwon &lt;onacit_at_gmail.com&gt;
*/
@jinahya
jinahya / TriFunction.java
Last active February 3, 2019 08:27
A functional interface for three arguments.
import java.util.function.BiFunction;
import java.util.function.Function;
import static java.util.Objects.requireNonNull;
/**
* Represents a function that accepts three arguments and produces a result. This is a three-arity specialization of
* {@link Function}.
*
* @param <T> the type of the first argument to the function
@jinahya
jinahya / GcdLcm.java
Last active January 11, 2019 09:31
GCD / LCM
public class GcdLcm {
/**
* Returns the greatest common devisor of given two numbers. An {@code IllegalArgumentException} will be thrown if
* either {@code a} or {@code b} is zero.
*
* @param a the first number.
* @param b the second number.
* @return the greatest common devisor of given two numbers.
* @see <a href="https://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest common divisor</a> (Wikipedia)
@jinahya
jinahya / ChronoFieldAttributes.java
Last active December 20, 2018 06:25
Prints attributes of each ChronoField's value.
import java.time.temporal.ChronoField;
import java.time.temporal.ValueRange;
import java.time.temporal.TemporalUnit;
import java.util.Locale;
public class ChronoFieldAttributes {
public static void main(final String... args) {
System.out.printf("%-30s %-30s %-10s %-10s %-10s %-10s %s\n",
"name", "displayName", "baseUnit", "rangeUnit",
@jinahya
jinahya / BinarySearch.java
Last active October 27, 2018 06:02
Binary search with Java
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
public class BinarySearch {
// -----------------------------------------------------------------------------------------------------------------
@jinahya
jinahya / BitCounter.java
Last active October 20, 2018 04:18
Count 1's in a number.
public class BitCounter {
public static void main(final String... args) {
int i = Integer.parseInt(args[0]);
int m = Integer.parseInt(args[1]);
int count = 0;
switch (m) {
case 0:
// iterates 32 regarless of the value
for (int j = 0; j < Integer.SIZE; j++) {
if ((i & 0x01) == 0x01) {
@jinahya
jinahya / Rotate.java
Last active October 14, 2018 05:14
rotate and find max index
import java.util.*;
import java.util.stream.*;
public class Rotate {
private static List<Integer> solve(final List<Integer> a, final List<Integer> rotate) {
// find the index of maximum value in a
final int max = IntStream.range(0, a.size()).reduce(0, (i, j) -> a.get(i) > a.get(j) ? i : j);
return rotate.stream().map(r -> (max + r) % a.size()).collect(Collectors.toList());
}
@jinahya
jinahya / Countries.java
Last active October 11, 2018 12:24
Count contries.
public class Countries {
private static int countries(final int[][] regions) {
int countries = 0;
for (int i = 0; i < regions.length; i++) {
for (int j = 0; j < regions[i].length; j++) {
final boolean n = i > 0 && regions[i - 1][j] == regions[i][j];
final boolean w = j > 0 && regions[i][j - 1] == regions[i][j];
if (n) {
if (w && (i == 0 || j == 0 || regions[i - 1][j - 1] != regions[i][j])) {
@jinahya
jinahya / fmtgp.c
Last active June 4, 2018 06:52
From Mathematics to Generic Programming by Alexander A. Stepanov
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
bool odd(int n) { return n & 0x1; }
int half(int n) { return n >> 1; }
int multiply0(int n, int a) {
if (n == 1) return a;