ABC problem: Difference between revisions

Content added Content deleted
(Add ABC)
(SuperCollider solution to Rosetta Code TASK: ABC_problem)
Line 10,412: Line 10,412:
val it = [true, false, true]: bool list
val it = [true, false, true]: bool list
</pre>
</pre>


=={{header|SuperCollider }}==
{{works with|SuperCollider|3.13.0}}
Submitted to Rosetta Code 2024-06-18 by: MusicCoder.
<syntaxhighlight lang="SuperCollider">
// ==========================================================================
// START-SuperCollider solution to Rosetta Code TASK: ABC problem
// ==========================================================================
(
/* ## BY: MusicCoder : 2024-06-18 ##
https://rosettacode.org/wiki/ABC_problem
Given a list of blocks - with two letters on each block
and given a list of words - report which words can/cannot be constructed
Case of letters can be ignored.

Approach: two loops - nested:
outer loop: iterate over each character in the word:
feed the current character into the inner loop
inner loop: iterate over the list of unused blocks:
if the current character is found on a block:
remove the block from the list
and exit the inner loop
if the end of the inner loop is reached
mark this word as failed
and exit both loops

NOTE: since 'block' is a METHOD in supercollider (used to created a breakable loop)
to avoid confusion the data items with two letters will be called 'tiles'.
*/

// FUNCTION: to solve the problem
var canMakeWord = {|rawWord, tileList|
var word = rawWord.toUpper; // make word upper case;
var unused = List.newFrom(tileList); // list of unused tiles

var madeWord = block {|outerBreak| // to BREAK from outer-loop assign to variable outerBreak
word.do {|char| // loop over characters in the word ...
// inner-block
block {|innerBreak| // to BREAK from inner-loop assign to variable innerBreak
unused.do {|tile, index| // loop over blocks in the unused list
if (tile.contains(char), // if we find a tile for this character ...
{ unused.removeAt(index); // remove the tile from the 'unused' list
innerBreak.value(1); // ... and BREAK from the inner loop.
// we don't care about the value assigned to innerBreak -- we just need to BREAK
}); // end-of: if
// continue to the next tile
}; // end-of: do inner-loop
// have gone through all of the tiles for current char without a match!
outerBreak.value(false); // set block=false => madeWord and BREAK from outer-loop
}; // end-of: inner-block
}; // endof: do outer-loop;
true; // set block value to true => madeWord = true;
}; // end-of: outer-block;

madeWord; // return true or false
};

// FUNCTION: Demonstrate and test the solution
var demoAndTest = {|tilesString, wordsList, shouldFailList|
// split the blocks-string into a set of strings & make upper-case
// NOTICE the strange split notation $space to split on space
var tileList = List.newFrom(tilesString.toUpper.split($ ));
// make a SET of words that should fail - so we can look them up easily
var shouldFailSet = Set.newFrom(shouldFailList);

wordsList.do({|word| // loop over the list of words
// CALL the function: canMakeWord
var made = canMakeWord.(word, tileList);
// align words true/false
var word_made = if (made, "true ", "FALSE");
// check to see if this word is in the should-fail-set
var shouldFail = shouldFailSet.includes(word.toUpper);
// did the make function have the expected result?
var pass = if ( (made && shouldFail) || (not(made) && not(shouldFail)), "FAILED", "passed");
// wrap word in single-quotes and align
var padWord = ("'"++word++"'").padRight(12);
postf("word=% word-made=% test:%\n", padWord, word_made, pass);
});
};


// input data
var tiles_string = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
var words_list = ["a", "BARK", "booK", "tReat", "cOmmOn", "squad", "ConFuseD"];
var should_fail = ["BOOK", "COMMON"];
// function to demostrate and test the solution
demoAndTest.(tiles_string, words_list, should_fail);
"\n";
)
// ==========================================================================
// **END-SuperCollider solution to Rosetta Code TASK: ABC problem
// ==========================================================================
</syntaxhighlight>
{{out}}
<pre>
word='a' word-made=true test:passed
word='BARK' word-made=true test:passed
word='booK' word-made=FALSE test:passed
word='tReat' word-made=true test:passed
word='cOmmOn' word-made=FALSE test:passed
word='squad' word-made=true test:passed
word='ConFuseD' word-made=true test:passed
</pre>





=={{header|Swift}}==
=={{header|Swift}}==