2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,43 +1,17 @@
|
|||
function createRandomBracketSequence(maxlen)
|
||||
{
|
||||
var chars = { '0' : '[' , '1' : ']' };
|
||||
function getRandomInteger(to)
|
||||
{
|
||||
return Math.floor(Math.random() * (to+1));
|
||||
}
|
||||
var n = getRandomInteger(maxlen);
|
||||
var result = [];
|
||||
for(var i = 0; i < n; i++)
|
||||
{
|
||||
result.push(chars[getRandomInteger(1)]);
|
||||
}
|
||||
return result.join("");
|
||||
function shuffle(str) {
|
||||
var a = str.split(''), b, c = a.length, d
|
||||
while (c) b = Math.random() * c-- | 0, d = a[c], a[c] = a[b], a[b] = d
|
||||
return a.join('')
|
||||
}
|
||||
|
||||
function bracketsAreBalanced(s)
|
||||
{
|
||||
var open = (arguments.length > 1) ? arguments[1] : '[';
|
||||
var close = (arguments.length > 2) ? arguments[2] : ']';
|
||||
var c = 0;
|
||||
for(var i = 0; i < s.length; i++)
|
||||
{
|
||||
var ch = s.charAt(i);
|
||||
if ( ch == open )
|
||||
{
|
||||
c++;
|
||||
}
|
||||
else if ( ch == close )
|
||||
{
|
||||
c--;
|
||||
if ( c < 0 ) return false;
|
||||
}
|
||||
}
|
||||
return c == 0;
|
||||
function isBalanced(str) {
|
||||
var a = str, b
|
||||
do { b = a, a = a.replace(/\[\]/g, '') } while (a != b)
|
||||
return !a
|
||||
}
|
||||
|
||||
var c = 0;
|
||||
while ( c < 5 ) {
|
||||
var seq = createRandomBracketSequence(8);
|
||||
alert(seq + ':\t' + bracketsAreBalanced(seq));
|
||||
c++;
|
||||
var M = 20
|
||||
while (M-- > 0) {
|
||||
var N = Math.random() * 10 | 0, bs = shuffle('['.repeat(N) + ']'.repeat(N))
|
||||
console.log('"' + bs + '" is ' + (isBalanced(bs) ? '' : 'un') + 'balanced')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,29 +1,60 @@
|
|||
function checkBalance(i) {
|
||||
while (i.length % 2 == 0) {
|
||||
j = i.replace('{}','');
|
||||
if (j == i)
|
||||
break;
|
||||
i = j;
|
||||
}
|
||||
return (i?false:true);
|
||||
}
|
||||
(() => {
|
||||
'use strict';
|
||||
|
||||
var g = 10;
|
||||
while (g--) {
|
||||
var N = 10 - Math.floor(g/2), n=N, o='';
|
||||
while (n || N) {
|
||||
if (N == 0 || n == 0) {
|
||||
o+=Array(++N).join('}') + Array(++n).join('{');
|
||||
break;
|
||||
}
|
||||
if (Math.round(Math.random()) == 1) {
|
||||
o+='}';
|
||||
N--;
|
||||
}
|
||||
else {
|
||||
o+='{';
|
||||
n--;
|
||||
}
|
||||
}
|
||||
alert(o+": "+checkBalance(o));
|
||||
}
|
||||
// Int -> String
|
||||
let randomBrackets = n => range(1, n)
|
||||
.map(() => Math.random() < 0.5 ? '[' : ']')
|
||||
.join('');
|
||||
|
||||
// imbalance :: String -> Integer
|
||||
let imbalance = strBrackets => {
|
||||
|
||||
// iDepth: initial nesting depth (0 = closed)
|
||||
// iIndex: starting character position
|
||||
|
||||
// errorIndex :: [Char] -> Int -> Int -> Int
|
||||
let errorIndex = (xs, iDepth, iIndex) => {
|
||||
if (xs.length > 0) {
|
||||
let tail = xs.slice(1),
|
||||
iNext = iDepth + (xs[0] === '[' ? 1 : -1);
|
||||
|
||||
if (iNext < 0) return iIndex; // unmatched closing bracket
|
||||
else return tail.length ? errorIndex(
|
||||
tail, iNext, iIndex + 1
|
||||
) : iNext === 0 ? -1 : iIndex; // balanced ? problem index ?
|
||||
|
||||
} else return iDepth === 0 ? -1 : iIndex;
|
||||
};
|
||||
|
||||
return errorIndex(strBrackets.split(''), 0, 0);
|
||||
};
|
||||
|
||||
|
||||
// GENERIC FUNCTION
|
||||
|
||||
// range :: Int -> Int -> [Int]
|
||||
let range = (m, n) =>
|
||||
Array.from({
|
||||
length: Math.floor(n - m) + 1
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// TESTING AND FORMATTING OUTPUT
|
||||
|
||||
let lngPairs = 6,
|
||||
strPad = Array(lngPairs * 2 + 4)
|
||||
.join(' ');
|
||||
|
||||
return range(0, lngPairs)
|
||||
.map(n => {
|
||||
let w = n * 2,
|
||||
s = randomBrackets(w),
|
||||
i = imbalance(s),
|
||||
blnOK = i === -1;
|
||||
|
||||
return "'" + s + "'" + strPad.slice(w + 2) +
|
||||
(blnOK ? 'OK' : 'problem') +
|
||||
(blnOK ? '' : '\n' + Array(i + 2)
|
||||
.join(' ') + '^');
|
||||
})
|
||||
.join('\n');
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue