UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

54 lines (43 loc) 1.37 kB
import times from 'lodash/fp/times' import concat from 'lodash/fp/concat' import map from 'lodash/fp/map' import reduce from 'lodash/fp/reduce' import range from 'lodash/fp/range' import flatten from 'lodash/fp/flatten' export function identityMatrix([a = 1, b = 0], size) { return map(row => times(n => (n === row ? a : b), size), range(0, size)) } // same concept as https://github.com/dankogai/js-combinatorics#cartesian-product export function cartesianProduct(...arrays) { return reduce((a, b) => flatten(map(x => map(y => concat(x, [y]), b), a)), [[]], arrays) } // same concept as https://github.com/dankogai/js-combinatorics#base-n export function baseNCombination(values, n) { if (n === 0) { return [] } if (n === 1) { return map(v => [v], values) } const previous = baseNCombination(values, n - 1) return cartesianProduct(values, previous).map(([v, p]) => [v, ...p]) } // same concept as https://github.com/dankogai/js-combinatorics#combination export function combination(array, size) { if (size === 0) { return [] } if (size === 1) { return map(v => [v], array) } if (array.length === 1) { return [times(() => array[0], size)] } if (array.length === 0) { return [] } return concat( map(v => [array[0], ...v], combination(array, size - 1)), combination(array.slice(1), size), ) }