Skip to content

Instantly share code, notes, and snippets.

View jinahya's full-sized avatar

Jin Kwon jinahya

  • Wemakeprice
  • Seoul, Korea
View GitHub Profile
@jinahya
jinahya / IterableSortedTester.java
Last active January 10, 2020 10:46
Checks an Iterable is sorted by a comparator.
import java.util.Comparator;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import static java.util.Arrays.asList;
import static java.util.Comparator.*;
class IterableSortedTester {
@jinahya
jinahya / SieveOfEratosthenes.java
Last active April 28, 2019 04:32
The Sieve of Eratosthenes in Java
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.stream.IntStream;
import static java.util.stream.IntStream.rangeClosed;
public class SieveOfEratosthenes {
static boolean isPrime(final int candidate) {
if (candidate < 2) {
return false;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.IntStream;
import static java.util.concurrent.ThreadLocalRandom.current;
class InsertionSort {
import static java.lang.Math.ceil;
import static java.lang.Math.floor;
import static java.lang.Math.pow;
import static java.lang.Math.round;
import java.util.*;
strictfp class A3b3EqualsC3d3 {
static class Pair {
@jinahya
jinahya / CountPairsOfIntegersThatHaveDifferenceK.java
Last active April 6, 2019 10:11
Cracking the Coding Interview
import java.util.*;
class CountPairsOfIntegersThatHaveDifferenceK {
static int f1(/*distinct*/final int[] A, final int K) {
int count = 0;
for (int i = 0; i < A.length - 1; i++) {
for (int j = i + 1; j < A.length; j++) {
if (Math.abs(A[j] - A[i]) == K) {
count++;
import static java.util.concurrent.ThreadLocalRandom.current;
/**
* A program finds the largest sum of contiguous subarrays.
*
* @see <a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm">Kadane's algorithm</a>
*/
class Kadane2 {
static long kadane(final int[] A) {
import java.math.BigInteger;
import java.util.Arrays;
public class FibonacciNumber {
static final BigInteger F0 = BigInteger.ZERO;
static final BigInteger F1 = BigInteger.ONE;
static BigInteger f(final int n) {
if (n <= 0) return F0;
if (n == 1) return F1;
final BigInteger[] a = new BigInteger[] {F0, F1};
for (int i = 2; i < n; i++) {
@jinahya
jinahya / SystemEnv.java
Last active March 26, 2019 07:22
Prints results of System.getenv()
public class SystemEnv {
public static void main(final String... args) {
System.getenv().entrySet().forEach(e -> System.out.printf("%s: %s\n", e.getKey(), e.getValue()));
}
}
@jinahya
jinahya / SystemProperties.java
Last active March 26, 2019 07:20
Prints result of System.getProperties()
public class SystemProperties {
public static void main(final String[] args) {
System.getProperties().forEach((k, v) -> System.out.printf("%s: %s\n", k, v));
}
}
import static java.util.concurrent.ThreadLocalRandom.current;
/**
* A program finds the largest sum of contiguous subarrays.
*
* @see <a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane's_algorithm">Kadane's algorithm</a>
*/
class Kadane {
static long kadane(final int[] A) {