ABC problem: Difference between revisions

m
 
(11 intermediate revisions by 8 users not shown)
Line 831:
True
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="ABC">HOW TO REPORT word can.be.made.with blocks:
FOR letter IN upper word:
IF NO block IN blocks HAS letter in block: FAIL
REMOVE block FROM blocks
SUCCEED
 
PUT {"BO";"XK";"DQ";"CP";"NA";"GT";"RE";"TG";"QD";"FS"} IN blocks
PUT {"JW";"HU";"VI";"AN";"OB";"ER";"FS";"LY";"PC";"ZM"} IN blocks2
FOR block IN blocks2: INSERT block IN blocks
 
PUT {"A";"BARK";"BOOK";"treat";"common";"Squad";"CoNfUsE"} IN words
 
FOR word IN words:
WRITE word, ": "
SELECT:
word can.be.made.with blocks: WRITE "yes"/
ELSE: WRITE "no"/</syntaxhighlight>
{{out}}
<pre>A: yes
BARK: yes
BOOK: no
CoNfUsE: yes
Squad: yes
common: no
treat: yes</pre>
 
=={{header|Action!}}==
Line 3,697 ⟶ 3,724:
"sQuAd" can be spelled with blocks.
"Confuse" can be spelled with blocks.</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
b$[][] = [ [ "B" "O" ] [ "X" "K" ] [ "D" "Q" ] [ "C" "P" ] [ "N" "A" ] [ "G" "T" ] [ "R" "E" ] [ "T" "G" ] [ "Q" "D" ] [ "F" "S" ] [ "J" "W" ] [ "H" "U" ] [ "V" "I" ] [ "A" "N" ] [ "O" "B" ] [ "E" "R" ] [ "F" "S" ] [ "L" "Y" ] [ "P" "C" ] [ "Z" "M" ] ]
len b[] len b$[][]
global w$[] cnt .
#
proc backtr wi . .
if wi > len w$[]
cnt += 1
return
.
for i = 1 to len b$[][]
if b[i] = 0 and (b$[i][1] = w$[wi] or b$[i][2] = w$[wi])
b[i] = 1
backtr wi + 1
b[i] = 0
.
.
.
for s$ in [ "A" "BARK" "BOOK" "TREAT" "COMMON" "SQUAD" "CONFUSE" ]
w$[] = strchars s$
cnt = 0
backtr 1
print s$ & " can be spelled in " & cnt & " ways"
.
</syntaxhighlight>
 
{{out}}
<pre>
A can be spelled in 2 ways
BARK can be spelled in 8 ways
BOOK can be spelled in 0 ways
TREAT can be spelled in 8 ways
COMMON can be spelled in 0 ways
SQUAD can be spelled in 8 ways
CONFUSE can be spelled in 32 ways
</pre>
 
=={{header|EchoLisp}}==
Line 3,777 ⟶ 3,842:
var list := ArrayList.load(blocks);
^ nil == (cast string(self)).toUpper().seekEach::(ch)
{
var index := list.indexOfElement
Line 3,806 ⟶ 3,871:
e.next();
words.forEach::(word)
{
console.printLine("can make '",word,"' : ",word.canMakeWordFrom(blocks));
Line 4,813 ⟶ 4,878:
found = instr$( 1, blocks, ch )
select found
case > 3: mid$( blocks, found and -2, 2 ) = "__" : text , , fn ColorblueColorYellow
rect fill ( x, y + 5.5, 15, 15 ), fn ColorWithRGB( .98, .85, .52, .75 )ColorBrown
case 0: text , , fn ColorWithRGB( 0, 0, .4, .3 )ColorLightGray
case < 4: blocks=" ,;BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM": x=3: y+=26: ch=""
end select
Line 4,824 ⟶ 4,889:
 
window 1, @"ABC problem in FutureBasic", ( 0, 0, 300, 300 )
fn blocks( "a baRk booK;treat;,COMMON; squad; Confused; comparable, incomparable, nondeductibles" )
handleevents
 
</syntaxhighlight>
{{output}}
[[File:FB output for ABC--W problemon Br.png]]
 
=={{header|Gambas}}==
Line 5,126 ⟶ 5,191:
=={{header|Insitux}}==
<syntaxhighlight lang="insitux">
(function in-block? c
(var find-idx #(when (let found (find % %1)) (idx %1 found)))
(function in-block? c (when (let block-idx (find-idx (substr? (upper-case c)) rem-blocks)) (var! rem-blocks drop block-idx)))
(var! rem-blocks drop block-idx)))
 
(function can-make-word word
Line 5,133 ⟶ 5,199:
(.. and (map in-block? word)))
 
(join ", " (map #(str % " =-> " (can-make-word %)) ["A" "bark" "Book" "TREAT" "Common" "squaD" "CoNFuSe"])) ; Notice case insensitivity
(map #(str % " => " (can-make-word %)))
 
(join ", "))
</syntaxhighlight>
{{out}}
Line 9,155 ⟶ 9,222:
conFUsE : true
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Each Show (<Blocks>) <Words>>;
};
 
Each {
s.F (e.Arg) = ;
s.F (e.Arg) t.I e.R = <Mu s.F t.I e.Arg> <Each s.F (e.Arg) e.R>;
};
 
Show {
(e.Word) e.Blocks = <Prout e.Word ': ' <CanMakeWord (e.Word) e.Blocks>>;
};
 
Blocks {
= ('BO') ('XK') ('DQ') ('CP') ('NA')
('GT') ('RE') ('TG') ('QD') ('FS')
('JW') ('HU') ('VI') ('AN') ('OB')
('ER') ('FS') ('LY') ('PC') ('ZM');
};
 
Words {
= ('A') ('BARK') ('BOOK') ('TREAT')
('common') ('squad') ('CoNfUsE');
};
 
CanMakeWord {
(e.Word) e.Blocks = <CanMakeWord1 (<Upper e.Word>) e.Blocks>;
}
 
CanMakeWord1 {
() e.Blocks = T;
(s.Ltr e.Word) e.Blocks1 (e.X s.Ltr e.Y) e.Blocks2
= <CanMakeWord1 (e.Word) e.Blocks1 e.Blocks2>;
(e.Word) e.Blocks = F;
};</syntaxhighlight>
{{out}}
<pre>A: T
BARK: T
BOOK: F
TREAT: T
common: F
squad: T
CoNfUsE: T</pre>
 
=={{header|REXX}}==
Line 9,825 ⟶ 9,937:
end repeat</syntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program ABC_problem;
blocks := ["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"];
 
words := ["A","BARK","BOOK","treat","common","Squad","CoNfUsE"];
 
loop for word in words do
print(rpad(word, 8), can_make_word(word, blocks));
end loop;
 
proc can_make_word(word, blocks);
loop for letter in word do
if exists block = blocks(i) | to_upper(letter) in block then
blocks(i) := "";
else
return false;
end if;
end loop;
return true;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>A #T
BARK #T
BOOK #F
treat #T
common #F
Squad #T
CoNfUsE #T</pre>
=={{header|SequenceL}}==
===Recursive Search Version===
Line 10,269 ⟶ 10,411:
<pre>val it = [true, true, false, true, false, true, true]: bool list
val it = [true, false, true]: bool list
</pre>
 
 
=={{header|SuperCollider }}==
{{works with|SuperCollider|3.13.0}}
Submitted to Rosetta Code 2024-06-18 by: Music Coder.
<syntaxhighlight lang="supercollider">
// ==========================================================================
// START:SuperCollider solution to Rosetta Code TASK: ABC_problem
// ==========================================================================
(
/* ## BY: Music Coder : 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>
 
Line 10,538 ⟶ 10,784:
For x = 0 To Len (a)-1 : @b(x) = Or(Peek(a, x), Ord(" ")) : Next
' push words onto stack
Push Dup("A"), Dup("Bark"), Dup("Book"), Dup("Treat"), "Common", "Squad", "Confuse"
Push Dup("Common"), Dup("Squad"), Dup("Confuse")
 
Do While Used() ' as long as words on the stack
Line 10,925 ⟶ 11,170:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var r // recursive
Line 10,952 ⟶ 11,197:
var sp = newSpeller.call("BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM")
for (word in ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"]) {
SystemFmt.print("%(Fmt.$-7s $s(-7", word)), %(sp.call(word))")
}</syntaxhighlight>
 
10

edits