ABC problem: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 1,136:
SQUAD - 1
CONFUSE - 1</pre>
 
=={{header|AWK}}==
Here are 2 slightly different versions:
Line 1,283 ⟶ 1,284:
pi@raspberrypi:~/Documents/rosettacode $
</pre>
 
=={{header|Batch File}}==
<lang dos>
@echo off
::abc.bat
::
::Batch file to evaluate if a given string can be represented with a set of
::20 2-faced blocks.
::
 
::Check if a string was provided
if "%1"=="" goto ERROR
 
::Define blocks. Separate blocks by ':', and terminat with '::'
set "FACES=BO:XK:DQ:CP:NA:GT:RE:TG:QD:FS:JW:HU:VI:AN:OB:ER:FS:LY:PC:ZM::"
set INPUT=%1
set "COUNTER=0"
 
::The main loop steps through the input string, checking if an available
::block exists for each character
:LOOP_MAIN
 
::Get character, increase counter, and test if there are still characters
call set "char=%%INPUT:~%COUNTER%,1%%"
set /a "COUNTER+=1"
if "%CHAR%"=="" goto LOOP_MAIN_END
 
set "OFFSET=0"
:LOOP_2
 
::Read in two characters (one block)
call set "BLOCK=%%FACES%:~%OFFSET%,2%%"
 
::Test if the all blocks were checked. If so, no match was found
if "%BLOCK%"==":" goto FAIL
 
::Test if current input string character is in the current block
if /i "%BLOCK:~0,1%"=="%CHAR%" goto FOUND
if /i "%BLOCK:~1,1%"=="%CHAR%" goto FOUND
 
::Increase offset to point to the next block
set /a "OFFSET+=3"
 
goto LOOP_2
:LOOP_2_END
 
::If found, blank out the block used
:FOUND
call set "FACES=%%FACES:%BLOCK%:= :%%"
 
goto LOOP_MAIN
:LOOP_MAIN_END
 
echo %0: It is possible to write the '%INPUT%' with my blocks.
goto END
 
:FAIL
echo %0: It is NOT possible to write the '%INPUT%' with my blocks.
goto END
 
:ERROR
echo %0: Please enter a string to evaluate
echo.
 
:END
</lang>
 
=={{header|BaCon}}==
Line 1,593 ⟶ 1,528:
{{out}}
<pre>YES</pre>
 
=={{header|Batch File}}==
<lang dos>
@echo off
::abc.bat
::
::Batch file to evaluate if a given string can be represented with a set of
::20 2-faced blocks.
::
 
::Check if a string was provided
if "%1"=="" goto ERROR
 
::Define blocks. Separate blocks by ':', and terminat with '::'
set "FACES=BO:XK:DQ:CP:NA:GT:RE:TG:QD:FS:JW:HU:VI:AN:OB:ER:FS:LY:PC:ZM::"
set INPUT=%1
set "COUNTER=0"
 
::The main loop steps through the input string, checking if an available
::block exists for each character
:LOOP_MAIN
 
::Get character, increase counter, and test if there are still characters
call set "char=%%INPUT:~%COUNTER%,1%%"
set /a "COUNTER+=1"
if "%CHAR%"=="" goto LOOP_MAIN_END
 
set "OFFSET=0"
:LOOP_2
 
::Read in two characters (one block)
call set "BLOCK=%%FACES%:~%OFFSET%,2%%"
 
::Test if the all blocks were checked. If so, no match was found
if "%BLOCK%"==":" goto FAIL
 
::Test if current input string character is in the current block
if /i "%BLOCK:~0,1%"=="%CHAR%" goto FOUND
if /i "%BLOCK:~1,1%"=="%CHAR%" goto FOUND
 
::Increase offset to point to the next block
set /a "OFFSET+=3"
 
goto LOOP_2
:LOOP_2_END
 
::If found, blank out the block used
:FOUND
call set "FACES=%%FACES:%BLOCK%:= :%%"
 
goto LOOP_MAIN
:LOOP_MAIN_END
 
echo %0: It is possible to write the '%INPUT%' with my blocks.
goto END
 
:FAIL
echo %0: It is NOT possible to write the '%INPUT%' with my blocks.
goto END
 
:ERROR
echo %0: Please enter a string to evaluate
echo.
 
:END
</lang>
 
=={{header|BBC BASIC}}==
Line 1,740 ⟶ 1,741:
SQUAD 1
Confuse 1
</pre>
 
 
=={{header|C++}}==
Uses C++11. Build with g++-4.7 -Wall -std=c++0x abc.cpp
<lang cpp>#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cctype>
 
 
typedef std::pair<char,char> item_t;
typedef std::vector<item_t> list_t;
 
bool can_make_word(const std::string& w, const list_t& vals) {
std::set<uint32_t> used;
while (used.size() < w.size()) {
const char c = toupper(w[used.size()]);
uint32_t x = used.size();
for (uint32_t i = 0, ii = vals.size(); i < ii; ++i) {
if (used.find(i) == used.end()) {
if (toupper(vals[i].first) == c || toupper(vals[i].second) == c) {
used.insert(i);
break;
}
}
}
if (x == used.size()) break;
}
return used.size() == w.size();
}
 
 
int main() {
list_t vals{ {'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'} };
std::vector<std::string> words{"A","BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE"};
for (const std::string& w : words) {
std::cout << w << ": " << std::boolalpha << can_make_word(w,vals) << ".\n";
}
 
}</lang>
 
{{out}}
<pre>A: true.
BARK: true.
BOOK: false.
TREAT: true.
COMMON: false.
SQUAD: true.
CONFUSE: true.
</pre>
 
Line 1,931 ⟶ 1,881:
SQUAD :True
CONFUSE :True</pre>
 
=={{header|C++}}==
Uses C++11. Build with g++-4.7 -Wall -std=c++0x abc.cpp
<lang cpp>#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cctype>
 
 
typedef std::pair<char,char> item_t;
typedef std::vector<item_t> list_t;
 
bool can_make_word(const std::string& w, const list_t& vals) {
std::set<uint32_t> used;
while (used.size() < w.size()) {
const char c = toupper(w[used.size()]);
uint32_t x = used.size();
for (uint32_t i = 0, ii = vals.size(); i < ii; ++i) {
if (used.find(i) == used.end()) {
if (toupper(vals[i].first) == c || toupper(vals[i].second) == c) {
used.insert(i);
break;
}
}
}
if (x == used.size()) break;
}
return used.size() == w.size();
}
 
 
int main() {
list_t vals{ {'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'} };
std::vector<std::string> words{"A","BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE"};
for (const std::string& w : words) {
std::cout << w << ": " << std::boolalpha << can_make_word(w,vals) << ".\n";
}
 
}</lang>
 
{{out}}
<pre>A: true.
BARK: true.
BOOK: false.
TREAT: true.
COMMON: false.
SQUAD: true.
CONFUSE: true.
</pre>
 
=={{header|Ceylon}}==
Line 2,229:
confuse:> $TRUE
</pre>
 
=={{header|D}}==
===Basic Version===
Line 2,590 ⟶ 2,591:
("A",true)
("",true)</pre>
 
=={{header|Elena}}==
ELENA 5.0
Line 3,347 ⟶ 3,349:
T T T CONFUSE
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>' version 28-01-2019
Line 4,575 ⟶ 4,578:
canMake("SQUAD"): true
canMake("CONFUSE"): true</pre>
 
=={{header|Maple}}==
<lang maple>canSpell := proc(w)
local blocks, i, j, word, letterFound;
blocks := Array([["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"]]);
word := StringTools[UpperCase](convert(w, string));
for i to length(word) do
letterFound := false;
for j to numelems(blocks)/2 do
if not letterFound and (substring(word, i) = blocks[j,1] or substring(word, i) = blocks[j,2]) then
blocks[j,1] := undefined;
blocks[j,2] := undefined;
letterFound := true;
end if;
end do;
if not letterFound then
return false;
end if;
end do;
return true;
end proc:
 
seq(printf("%a: %a\n", i, canSpell(i)), i in [a, Bark, bOok, treat, COMMON, squad, confuse]);</lang>
{{out}}
<pre>
a: true
Bark: true
bOok: false
treat: true
COMMON: false
squad: true
confuse: true
</pre>
 
=={{header|M2000 Interpreter}}==
Line 4,648 ⟶ 4,616:
CONFUSE True
</pre >
 
=={{header|Maple}}==
<lang maple>canSpell := proc(w)
local blocks, i, j, word, letterFound;
blocks := Array([["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"]]);
word := StringTools[UpperCase](convert(w, string));
for i to length(word) do
letterFound := false;
for j to numelems(blocks)/2 do
if not letterFound and (substring(word, i) = blocks[j,1] or substring(word, i) = blocks[j,2]) then
blocks[j,1] := undefined;
blocks[j,2] := undefined;
letterFound := true;
end if;
end do;
if not letterFound then
return false;
end if;
end do;
return true;
end proc:
 
seq(printf("%a: %a\n", i, canSpell(i)), i in [a, Bark, bOok, treat, COMMON, squad, confuse]);</lang>
{{out}}
<pre>
a: true
Bark: true
bOok: false
treat: true
COMMON: false
squad: true
confuse: true
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 5,370 ⟶ 5,373:
SQUAD 1
conFUSE 1</pre>
 
 
=={{header|Pascal}}==
Line 5,507 ⟶ 5,509:
is(can_make_word('auto', @blocks2), 1);
</lang>
 
=={{header|Perl 6}}==
{{works with|rakudo|6.0.c}}
Blocks are stored as precompiled regexes. We do an initial pass on the blockset to include in the list only those regexes that match somewhere in the current word. Conveniently, regexes scan the word for us.
<lang perl6>multi can-spell-word(Str $word, @blocks) {
my @regex = @blocks.map({ my @c = .comb; rx/<@c>/ }).grep: { .ACCEPTS($word.uc) }
can-spell-word $word.uc.comb.list, @regex;
}
multi can-spell-word([$head,*@tail], @regex) {
for @regex -> $re {
if $head ~~ $re {
return True unless @tail;
return False if @regex == 1;
return True if can-spell-word @tail, list @regex.grep: * !=== $re;
}
}
False;
}
my @b = <BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM>;
for <A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE> {
say "$_ &can-spell-word($_, @b)";
}</lang>
{{out}}
<pre>A True
BaRK True
BOoK False
tREaT True
COmMOn False
SqUAD True
CoNfuSE True</pre>
 
=={{header|Phix}}==
Line 6,707 ⟶ 6,676:
Can we make: "SQUAD" ? yes
Can we make: "CONFUSE"? yes</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|6.0.c}}
Blocks are stored as precompiled regexes. We do an initial pass on the blockset to include in the list only those regexes that match somewhere in the current word. Conveniently, regexes scan the word for us.
<lang perl6>multi can-spell-word(Str $word, @blocks) {
my @regex = @blocks.map({ my @c = .comb; rx/<@c>/ }).grep: { .ACCEPTS($word.uc) }
can-spell-word $word.uc.comb.list, @regex;
}
multi can-spell-word([$head,*@tail], @regex) {
for @regex -> $re {
if $head ~~ $re {
return True unless @tail;
return False if @regex == 1;
return True if can-spell-word @tail, list @regex.grep: * !=== $re;
}
}
False;
}
my @b = <BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM>;
for <A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE> {
say "$_ &can-spell-word($_, @b)";
}</lang>
{{out}}
<pre>A True
BaRK True
BOoK False
tREaT True
COmMOn False
SqUAD True
CoNfuSE True</pre>
 
=={{header|RapidQ}}==
Line 6,745 ⟶ 6,748:
Can make: CONFUSE = TRUE
</pre>
 
=={{header|Red}}==
<lang Red>Red []
Line 6,773 ⟶ 6,777:
conFUsE : true
</pre>
 
=={{header|REXX}}==
===version 1===
Line 7,470 ⟶ 7,475:
auto -> true
</pre>
 
=={{header|Simula}}==
<lang simula>COMMENT ABC PROBLEM;
Line 7,655 ⟶ 7,661:
 
There is optimization potential of course.
 
 
=={{header|Swift}}==
10,333

edits