Skip to content

Instantly share code, notes, and snippets.

@ylegall
ylegall / LordicNumbers.kt
Created May 2, 2019 13:57
generate lordic numbers
/**
* consider all positive integers starting at 2.
* add 2 to the results. cross off every 2nd remaining number.
* The next number is 3, add it to the results. cross off every 3rd remaining number.
* The next number is 5, add it to the results. cross off every 5th remaining number.
* The next number is 7, add it to the results. cross off every 7th remaining number.
* etc
*/
private fun lordicNumbers(size: Int): List<Int> {
if (size <= 0) return emptyList()
@ylegall
ylegall / AsyncMapWithTimeout.kt
Created February 2, 2019 20:15
run n tasks in parallel and get results that finish within a timeout
package aoc2015
import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
@ylegall
ylegall / PickingNumbers.kt
Created September 23, 2018 19:43
hackerrank easy puzzle
import java.io.FileInputStream
import java.util.*
// https://www.hackerrank.com/challenges/picking-numbers/problem
// Complete the pickingNumbers function below.
fun pickingNumbers(a: Array<Int>): Int {
val counts = a.groupingBy{ it }.eachCount()
val entries = counts.entries.asSequence().sortedBy { e -> e.key }.toList()
var maxLen = 0
@ylegall
ylegall / ThreadPoolTest.java
Last active January 12, 2018 18:31
RejectedExecutionException example
import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
import java.util.*;
import org.junit.jupiter.api.*;
public class LargestRectangleUnderHistogram {
public static int largestRectangleArea(int[] heights) {
if (heights.length < 1) return 0;
int maxArea = 0;
Deque<Integer> stack = new ArrayDeque<>(heights.length);
@ylegall
ylegall / display7.d
Created June 6, 2017 20:18
console 7 segment display
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.stdio;
import std.string;
import std.traits;
alias Digit = bool[7];
@ylegall
ylegall / make_change.py
Created May 22, 2016 22:27
number of ways to make change from coins without repetition
def make_change(amount, coins):
coins.sort()
def _make_change(amount, idx, result):
if amount == 0:
print result
return
for i in range(idx, len(coins)):
coin = coins[i]
@ylegall
ylegall / LargestProductOf3.scala
Created May 16, 2016 08:49
find the largest product of 3 integers in an array
object LargestProductOf3
{
def largestProductOf3(a: Array[Int]): Int = {
var (max, max2, max3) = (a(0), a(0), a(0))
var (min, min2) = (a(0), a(0))
a.foreach(i => {
if (i >= max) {
max3 = max2
max2 = max
@ylegall
ylegall / challenge.d
Created March 6, 2014 22:18
D challenge template
import std.stdio, std.string, std.conv;
import std.algorithm;
import std.array;
auto readArray(T)()
{
return array(map!(a => to!T(a))(stdin.readln().split()));
}
void main()
@ylegall
ylegall / rotated.d
Created December 11, 2013 16:58
search for an item in a sorted array that has been rotated.
import std.stdio;
auto search(int[] array, int target)
{
int low = 0;
int high = array.length-1;
debug writeln("target = ", target);
while (low <= high) {
debug writefln("low=%d, high=%d", low, high);
auto mid = (low + high + 1) / 2;