Skip to content

Instantly share code, notes, and snippets.

@Dicee
Dicee / gist:e12c1d298993508cef033d5f397cf22c
Last active February 9, 2023 18:40
Ratio of sums versus average of ratios
public static void main(String[] args) {
Random rd = new Random();
int totalInputs = 0;
int totalOutputs = 0;
List<Double> rates = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
int inputs = 1 + rd.nextInt(100_000);
int outputs = /*rd.nextInt(inputs);*/ Math.min(rd.nextInt(10_000), inputs); // 0.9*5000 + 0.1*5000 = 5000
/**
* This class' sole purpose is to use LambdaFunctionMonitoring via composition rather than inheritance. The problem with the current design
* of the library is that it tries to be one-size-fits-all and packs a lot of features into extendable template classes. The issue is that
* these classes do not allow a decent level of customization. Extending them sometimes works, but can fail short in other cases.
*
* The solution? Ditch inheritance and use composition, allowing the user to do anything they want. Now, this class just becomes a thing that
* knows about Lambda and its monitoring, but doesn't try to impose one template for monitoring, only ingredients for users to do so.
* Thus, we just make all immutable properties public rather than protected.
*
* I opened a feature request on Github to get this pattern into the library directly: https://github.com/cdklabs/cdk-monitoring-constructs/issues/218
@Dicee
Dicee / RawUseOfGenericInterface.java
Created June 1, 2020 09:32
Answer to a StackOverflow comment
import java.util.Comparator;
// https://stackoverflow.com/questions/62122965/how-to-resolve-raw-use-of-parameterized-class-comparable-warning/62122994#62122994
public class Main {
interface FooInt extends Comparable<Object> {
int getInt();
@Override
default int compareTo(Object that) {
if (that instanceof Number) return Integer.compare(getInt(), ((Number) that).intValue());
// https://stackoverflow.com/questions/57447358/does-this-merge-function-of-mergesort-take-o-1-space-or-on-space/57448761?noredirect=1#comment101376213_57448761
public final class ShorterLinkedListMergeInPlace {
public static void main(String[] args) {
System.out.println(merge(oddList(), null)); // [1, 3, 7]
System.out.println(merge(oddList(), evenList())); // [1, 2, 3, 4, 6, 7, 10]
System.out.println(merge(new ListNode(-1, null), evenList())); // [-1, 2, 4, 6, 10]
System.out.println(merge(new ListNode(100, null), evenList())); // [2, 4, 6, 10, 100]
System.out.println(merge(evenList(), new ListNode(100, null))); // [2, 4, 6, 10, 100]
}
package org.apache.spark.sql.execution.datasources.json
import java.util.Comparator
import com.fasterxml.jackson.core._
import org.apache.spark.sql.catalyst.analysis.TypeCoercion
import org.apache.spark.sql.execution.datasources.json.JacksonUtils.nextUntil
import org.apache.spark.sql.types._
import org.apache.spark.util.Utils
/**
* See http://stackoverflow.com/questions/38287661/how-to-get-tostring-for-each-element-of-an-arraylist/38287746?noredirect=1#comment63993889_38287746
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
sb.append(i);
}
package stackoverflow;
import java.util.Scanner;
public class MatrixTraversal {
static int[][] cost;
static int m, n, maxCost = 0;
public static void solve(int currRow, int currCol, int[][] isVisited, int currCost) {
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class Benchmark {
private static final Random rd = new Random(System.currentTimeMillis());