This file contains 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 java.io.IOException; | |
/** | |
* A class for generateing <i>counterstring</i>s. | |
* | |
* @author Jin Kwon <onacit_at_gmail.com> | |
* @see <a href="http://www.satisfice.com/blog/archives/22">Counterstrings: Self-Describing Test Data</a> | |
*/ | |
public class CounterString { |
This file contains 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 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 <onacit_at_gmail.com> | |
*/ |
This file contains 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 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 |
This file contains 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
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) |
This file contains 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 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", |
This file contains 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 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 { | |
// ----------------------------------------------------------------------------------------------------------------- |
This file contains 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
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) { |
This file contains 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 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()); | |
} |
This file contains 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
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])) { |
This file contains 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 <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; |