Skip to content

Instantly share code, notes, and snippets.

Avatar

Jin Kwon jinahya

  • Wemakeprice
  • Seoul, Korea
View GitHub Profile
@jinahya
jinahya / IterableSortedTester.java
Last active Jan 10, 2020
Checks an Iterable is sorted by a comparator.
View IterableSortedTester.java
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 Apr 28, 2019
The Sieve of Eratosthenes in Java
View SieveOfEratosthenes.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;
View InsertionSort.java
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 {
View A3b3EqualsC3d3.java
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 {
View CountPairsOfIntegersThatHaveDifferenceK.java
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++;
View Kadane2.java
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) {
View FibonacciNumber.java
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 Mar 26, 2019
Prints results of System.getenv()
View SystemEnv.java
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 Mar 26, 2019
Prints result of System.getProperties()
View SystemProperties.java
public class SystemProperties {
public static void main(final String[] args) {
System.getProperties().forEach((k, v) -> System.out.printf("%s: %s\n", k, v));
}
}
View Kadane.java
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) {