Skip to content

Instantly share code, notes, and snippets.

Challenge:

Write a function that accepts an array of numbers and a number and returns all combinations of the numbers in the array that sum to the provided number. The result will be an array of arrays. The output arrays should be in order with the fewest numbers required to make the sum first. Each number in the input array can only be used once; if the same number is present in the input array multiple times it can be used once per occurrance. The goal is to find all combination of numbers from the input to reach the total, if the same combination could occur multiple times, you can decide if you return that combination more than once or not, whichever is easier for your implementation.

Expectations:

  • the function should be of the form f(number[], number) => number[][]

Examples:

@ryanguill
ryanguill / postgres-merge-arrays.sql
Created December 21, 2016 16:41
in postgres, there currently isnt a function to combine two arrays and remove duplicates - this will do that.
CREATE OR REPLACE FUNCTION mergeArrays (a1 ANYARRAY, a2 ANYARRAY) RETURNS ANYARRAY AS $$
SELECT ARRAY_AGG(x ORDER BY x)
FROM (
SELECT DISTINCT UNNEST($1 || $2) AS x
) s;
$$ LANGUAGE SQL STRICT;
<cfscript>
function prettyPrintJSON (inputJSON) {
var engine = createObject("java","javax.script.ScriptEngineManager").init().getEngineByName("nashorn");
engine.eval("
function prettyPrintJSON (data) {
return JSON.stringify(JSON.parse(data), null, '\t');
}
");
return engine.invokeFunction("prettyPrintJSON", [inputJSON]);
}
DROP FUNCTION IF EXISTS calculate_time_range();
CREATE OR REPLACE FUNCTION calculate_time_range (uom varchar, qty integer, startTs timestamptz DEFAULT (current_timestamp at time zone 'utc')::timestamptz) RETURNS tstzrange AS $$
BEGIN
IF uom IS null THEN RETURN null; END IF;
IF qty IS null THEN RETURN null; END IF;
CASE UPPER(uom)
WHEN 'TIME_MS' THEN
RETURN TSTZRANGE(startTs, startTs + (qty || ' milliseconds')::INTERVAL, '[)');

undefined

{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
//http://blog.adamcameron.me/2016/08/code-quiz.html
var firstMatch = (input, pattern) => input.find(item => item.match(pattern));
console.log(firstMatch(['a', 'at', 'cat', 'scat', 'catch'], '.+at'));
/*global _, jQuery */
var APP = APP || {};
//just for testing
APP.dependencyOne = true;
APP.dependencyTwo = true;
APP.player = (function player(dependencyOne, dependencyTwo) {
"use strict";
SELECT
n.nspname
, c.relname
, c.relhaspkey
, sum(case when i.indisunique = 't' then 1 else 0 end)
FROM
pg_catalog.pg_class c
INNER
JOIN pg_namespace n
ON c.relnamespace = n.oid
[\s#]+session[.\[]+[^\s]
better from choop: \bsession[.\[][^\s]+
tests:
dontmatchsession.x
session.foo
#session.foo
session["foo"]