Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
37
Task/ABC-Problem/JavaScript/abc-problem-1.js
Normal file
37
Task/ABC-Problem/JavaScript/abc-problem-1.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
var blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
|
||||
|
||||
function CheckWord(blocks, word) {
|
||||
// Makes sure that word only contains letters.
|
||||
if(word !== /([a-z]*)/i.exec(word)[1]) return false;
|
||||
// Loops through each character to see if a block exists.
|
||||
for(var i = 0; i < word.length; ++i)
|
||||
{
|
||||
// Gets the ith character.
|
||||
var letter = word.charAt(i);
|
||||
// Stores the length of the blocks to determine if a block was removed.
|
||||
var length = blocks.length;
|
||||
// The regexp gets constructed by eval to allow more browsers to use the function.
|
||||
var reg = eval("/([a-z]"+letter+"|"+letter+"[a-z])/i");
|
||||
// This does the same as above, but some browsers do not support...
|
||||
//var reg = new RegExp("([a-z]"+letter+"|"+letter+"[a-z])", "i");
|
||||
// Removes all occurrences of the match.
|
||||
blocks = blocks.replace(reg, "");
|
||||
// If the length did not change then a block did not exist.
|
||||
if(blocks.length === length) return false;
|
||||
}
|
||||
// If every character has passed then return true.
|
||||
return true;
|
||||
};
|
||||
|
||||
var words = [
|
||||
"A",
|
||||
"BARK",
|
||||
"BOOK",
|
||||
"TREAT",
|
||||
"COMMON",
|
||||
"SQUAD",
|
||||
"CONFUSE"
|
||||
];
|
||||
|
||||
for(var i = 0;i<words.length;++i)
|
||||
console.log(words[i] + ": " + CheckWord(blocks, words[i]));
|
||||
48
Task/ABC-Problem/JavaScript/abc-problem-2.js
Normal file
48
Task/ABC-Problem/JavaScript/abc-problem-2.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
(function (strWords) {
|
||||
|
||||
var strBlocks =
|
||||
'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM',
|
||||
blocks = strBlocks.split(' ');
|
||||
|
||||
function abc(lstBlocks, strWord) {
|
||||
var lngChars = strWord.length;
|
||||
|
||||
if (!lngChars) return [];
|
||||
|
||||
var b = lstBlocks[0],
|
||||
c = strWord[0];
|
||||
|
||||
return chain(lstBlocks, function (b) {
|
||||
return (b.indexOf(c.toUpperCase()) !== -1) ? [
|
||||
(b + ' ').concat(
|
||||
abc(removed(b, lstBlocks), strWord.slice(1)))
|
||||
] : [];
|
||||
})
|
||||
}
|
||||
|
||||
// Monadic bind (chain) for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// a -> [a] -> [a]
|
||||
function removed(x, xs) {
|
||||
var h = xs.length ? xs[0] : null,
|
||||
t = h ? xs.slice(1) : [];
|
||||
|
||||
return h ? (
|
||||
h === x ? t : [h].concat(removed(x, t))
|
||||
) : [];
|
||||
}
|
||||
|
||||
function solution(strWord) {
|
||||
var strAttempt = abc(blocks, strWord)[0].split(',')[0];
|
||||
|
||||
// two chars per block plus one space -> 3
|
||||
return strWord + ((strAttempt.length === strWord.length * 3) ?
|
||||
' -> ' + strAttempt : ': [no solution]');
|
||||
}
|
||||
|
||||
return strWords.split(' ').map(solution).join('\n');
|
||||
|
||||
})('A bark BooK TReAT COMMON squAD conFUSE');
|
||||
7
Task/ABC-Problem/JavaScript/abc-problem-3.js
Normal file
7
Task/ABC-Problem/JavaScript/abc-problem-3.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
A -> NA
|
||||
bark -> BO NA RE XK
|
||||
BooK: [no solution]
|
||||
TReAT -> GT RE ER NA TG
|
||||
COMMON: [no solution]
|
||||
squAD -> FS DQ HU NA QD
|
||||
conFUSE -> CP BO NA FS HU FS RE
|
||||
Loading…
Add table
Add a link
Reference in a new issue