Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Range-expansion/Jsish/range-expansion.jsish
Normal file
40
Task/Range-expansion/Jsish/range-expansion.jsish
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env jsish
|
||||
"use strict";
|
||||
|
||||
/* Range expansion, in Jsish */
|
||||
function rangeExpand(rangeExpr) {
|
||||
|
||||
function getFactors(term) {
|
||||
var matches = term.match(/(-?[0-9]+)-(-?[0-9]+)/);
|
||||
if (!matches) return {first:Number(term)};
|
||||
return {first:Number(matches[1]), last:Number(matches[2])};
|
||||
}
|
||||
|
||||
function expandTerm(term) {
|
||||
var factors = getFactors(term);
|
||||
if (factors.length < 2) return [factors.first];
|
||||
var range = [];
|
||||
for (var n = factors.first; n <= factors.last; n++) {
|
||||
range.push(n);
|
||||
}
|
||||
return range;
|
||||
}
|
||||
|
||||
var result = [];
|
||||
var terms = rangeExpr.split(",");
|
||||
for (var t in terms) {
|
||||
result = result.concat(expandTerm(terms[t]));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
; rangeExpand('-6,-3--1,3-5,7-11,14,15,17-20');
|
||||
}
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
rangeExpand('-6,-3--1,3-5,7-11,14,15,17-20') ==> [ -6, -3, -2, -1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20 ]
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
Loading…
Add table
Add a link
Reference in a new issue