ABC problem: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 44: Line 44:


;Example:
;Example:
<syntaxhighlight lang=python> >>> can_make_word("A")
<syntaxhighlight lang="python"> >>> can_make_word("A")
True
True
>>> can_make_word("BARK")
>>> can_make_word("BARK")
Line 64: Line 64:
=={{header|11l}}==
=={{header|11l}}==
{{trans|Python}}
{{trans|Python}}
<syntaxhighlight lang=11l>F can_make_word(word)
<syntaxhighlight lang="11l">F can_make_word(word)
I word == ‘’
I word == ‘’
R 0B
R 0B
Line 83: Line 83:
=={{header|360 Assembly}}==
=={{header|360 Assembly}}==
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
<syntaxhighlight lang=360asm>* ABC Problem 21/07/2016
<syntaxhighlight lang="360asm">* ABC Problem 21/07/2016
ABC CSECT
ABC CSECT
USING ABC,R13 base register
USING ABC,R13 base register
Line 171: Line 171:


=={{header|8080 Assembly}}==
=={{header|8080 Assembly}}==
<syntaxhighlight lang=8080asm> org 100h
<syntaxhighlight lang="8080asm"> org 100h
jmp test
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 263: Line 263:
{{trans|8080 Assembly}}
{{trans|8080 Assembly}}


<syntaxhighlight lang=asm> cpu 8086
<syntaxhighlight lang="asm"> cpu 8086
bits 16
bits 16
org 100h
org 100h
Line 338: Line 338:


=={{header|8th}}==
=={{header|8th}}==
<syntaxhighlight lang=360asm>
<syntaxhighlight lang="360asm">
\ ========================================================================================
\ ========================================================================================
\ You are given a collection of ABC blocks
\ You are given a collection of ABC blocks
Line 540: Line 540:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program problemABC64.s */
/* program problemABC64.s */
Line 747: Line 747:
</pre>
</pre>
=={{header|ABAP}}==
=={{header|ABAP}}==
<syntaxhighlight lang=ABAP>
<syntaxhighlight lang="abap">
REPORT z_rosetta_abc.
REPORT z_rosetta_abc.


Line 833: Line 833:


=={{header|Action!}}==
=={{header|Action!}}==
<syntaxhighlight lang=Action!>DEFINE COUNT="20"
<syntaxhighlight lang="action!">DEFINE COUNT="20"
CHAR ARRAY sideA="BXDCNGRTQFJHVAOEFLPZ"
CHAR ARRAY sideA="BXDCNGRTQFJHVAOEFLPZ"
CHAR ARRAY sideB="OKQPATEGDSWUINBRSYCM"
CHAR ARRAY sideB="OKQPATEGDSWUINBRSYCM"
Line 906: Line 906:
Using #HASH-OFF
Using #HASH-OFF
</pre>
</pre>
<syntaxhighlight lang=acurity architect>
<syntaxhighlight lang="acurity architect">
FUNCTION bCAN_MAKE_WORD(zWord: STRING): BOOLEAN
FUNCTION bCAN_MAKE_WORD(zWord: STRING): BOOLEAN
VAR sBlockCount: SHORT
VAR sBlockCount: SHORT
Line 950: Line 950:
</pre>
</pre>


<syntaxhighlight lang=ada>with Ada.Characters.Handling;
<syntaxhighlight lang="ada">with Ada.Characters.Handling;
use Ada.Characters.Handling;
use Ada.Characters.Handling;


Line 1,043: Line 1,043:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<syntaxhighlight lang=algol68># determine whether we can spell words with a set of blocks #
<syntaxhighlight lang="algol68"># determine whether we can spell words with a set of blocks #


# construct the list of blocks #
# construct the list of blocks #
Line 1,136: Line 1,136:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<syntaxhighlight lang=algolw>% determine whether we can spell words with a set of blocks %
<syntaxhighlight lang="algolw">% determine whether we can spell words with a set of blocks %
begin
begin
% Returns true if we can spell the word using the blocks, %
% Returns true if we can spell the word using the blocks, %
Line 1,229: Line 1,229:


=={{header|Apex}}==
=={{header|Apex}}==
<syntaxhighlight lang=Java>static Boolean canMakeWord(List<String> src_blocks, String word) {
<syntaxhighlight lang="java">static Boolean canMakeWord(List<String> src_blocks, String word) {
if (String.isEmpty(word)) {
if (String.isEmpty(word)) {
return true;
return true;
Line 1,286: Line 1,286:
=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL|16.0}}
{{works with|Dyalog APL|16.0}}
<syntaxhighlight lang=APL>abc←{{0=⍴⍵:1 ⋄ 0=⍴h←⊃⍵:0 ⋄ ∇(t←1↓⍵)~¨⊃h:1 ⋄ ∇(⊂1↓h),t}⍸¨↓⍵∘.∊⍺}</syntaxhighlight>
<syntaxhighlight lang="apl">abc←{{0=⍴⍵:1 ⋄ 0=⍴h←⊃⍵:0 ⋄ ∇(t←1↓⍵)~¨⊃h:1 ⋄ ∇(⊂1↓h),t}⍸¨↓⍵∘.∊⍺}</syntaxhighlight>
{{out}}
{{out}}
<pre> )COPY dfns ucase
<pre> )COPY dfns ucase
Line 1,296: Line 1,296:
=={{header|AppleScript}}==
=={{header|AppleScript}}==
===Imperative===
===Imperative===
<syntaxhighlight lang=AppleScript>set blocks to {"bo", "xk", "dq", "cp", "na", "gt", "re", "tg", "qd", "fs", ¬
<syntaxhighlight lang="applescript">set blocks to {"bo", "xk", "dq", "cp", "na", "gt", "re", "tg", "qd", "fs", ¬
"jw", "hu", "vi", "an", "ob", "er", "fs", "ly", "pc", "zm"}
"jw", "hu", "vi", "an", "ob", "er", "fs", "ly", "pc", "zm"}


Line 1,326: Line 1,326:


===Functional===
===Functional===
<syntaxhighlight lang=AppleScript>use AppleScript version "2.4"
<syntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use framework "Foundation"


Line 1,534: Line 1,534:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
/* program problemABC.s */
/* program problemABC.s */
Line 1,737: Line 1,737:
=={{header|Arturo}}==
=={{header|Arturo}}==


<syntaxhighlight lang=rebol>blocks: map [
<syntaxhighlight lang="rebol">blocks: map [
[B O] [X K] [D Q] [C P] [N A] [G T] [R E]
[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]
[T G] [Q D] [F S] [J W] [H U] [V I] [A N]
Line 1,773: Line 1,773:


=={{header|Astro}}==
=={{header|Astro}}==
<syntaxhighlight lang=python>fun abc(s, ls):
<syntaxhighlight lang="python">fun abc(s, ls):
if ls.isempty:
if ls.isempty:
return true
return true
Line 1,789: Line 1,789:


'''Function'''
'''Function'''
<syntaxhighlight lang=autohotkey>isWordPossible(blocks, word){
<syntaxhighlight lang="autohotkey">isWordPossible(blocks, word){
o := {}
o := {}
loop, parse, blocks, `n, `r
loop, parse, blocks, `n, `r
Line 1,815: Line 1,815:


'''Test Input''' (as per question)
'''Test Input''' (as per question)
<syntaxhighlight lang=autohotkey>blocks := "
<syntaxhighlight lang="autohotkey">blocks := "
(
(
BO
BO
Line 2,012: Line 2,012:


=={{header|BaCon}}==
=={{header|BaCon}}==
<syntaxhighlight lang=qbasic>CONST info$ = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM"
<syntaxhighlight lang="qbasic">CONST info$ = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM"


DATA "A", "BARK", "BOOK", "TREAT", "Common", "Squad", "Confuse"
DATA "A", "BARK", "BOOK", "TREAT", "Common", "Squad", "Confuse"
Line 2,049: Line 2,049:
=={{header|BASIC}}==
=={{header|BASIC}}==
Works with:VB-DOS, QB64, QBasic, QuickBASIC
Works with:VB-DOS, QB64, QBasic, QuickBASIC
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ABC_Problem '
' ABC_Problem '
Line 2,179: Line 2,179:
==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
{{trans|Sinclair ZX-81 BASIC}}
{{trans|Sinclair ZX-81 BASIC}}
<syntaxhighlight lang=basic>10 W$ = "A" : GOSUB 100
<syntaxhighlight lang="basic">10 W$ = "A" : GOSUB 100
20 W$ = "BARK" : GOSUB 100
20 W$ = "BARK" : GOSUB 100
30 W$ = "BOOK" : GOSUB 100
30 W$ = "BOOK" : GOSUB 100
Line 2,216: Line 2,216:
The above greedy algorithm works on the sample data, but fails on other data - for example, it will declare that you cannot spell the word ABBA using the blocks (AB),(AB),(AC),(AC), because it will use the two AB blocks for the first two letters "AB", leaving none for the second "B". This recursive solution is more thorough about confirming negatives and handles that case correctly:
The above greedy algorithm works on the sample data, but fails on other data - for example, it will declare that you cannot spell the word ABBA using the blocks (AB),(AB),(AC),(AC), because it will use the two AB blocks for the first two letters "AB", leaving none for the second "B". This recursive solution is more thorough about confirming negatives and handles that case correctly:


<syntaxhighlight lang=basic>100 REM RECURSIVE SOLUTION
<syntaxhighlight lang="basic">100 REM RECURSIVE SOLUTION
110 MS=100:REM MAX STACK DEPTH
110 MS=100:REM MAX STACK DEPTH
120 DIM BL$(MS):REM BLOCKS LEFT
120 DIM BL$(MS):REM BLOCKS LEFT
Line 2,277: Line 2,277:
==={{header|Sinclair ZX81 BASIC}}===
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM. A nice unstructured algorithm. Unfortunately the requirement that it be case-insensitive is moot, because the ZX81 does not support lower-case letters.
Works with 1k of RAM. A nice unstructured algorithm. Unfortunately the requirement that it be case-insensitive is moot, because the ZX81 does not support lower-case letters.
<syntaxhighlight lang=basic> 10 LET B$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
<syntaxhighlight lang="basic"> 10 LET B$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
20 INPUT W$
20 INPUT W$
30 FOR I=1 TO LEN W$
30 FOR I=1 TO LEN W$
Line 2,318: Line 2,318:


=={{header|Batch File}}==
=={{header|Batch File}}==
<syntaxhighlight lang=dos>
<syntaxhighlight lang="dos">
@echo off
@echo off
::abc.bat
::abc.bat
Line 2,385: Line 2,385:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang=bbcbasic> BLOCKS$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
<syntaxhighlight lang="bbcbasic"> BLOCKS$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
PROCcan_make_word("A")
PROCcan_make_word("A")
PROCcan_make_word("BARK")
PROCcan_make_word("BARK")
Line 2,419: Line 2,419:


=={{header|BCPL}}==
=={{header|BCPL}}==
<syntaxhighlight lang=bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let canMakeWord(word) = valof
let canMakeWord(word) = valof
Line 2,466: Line 2,466:


=={{header|BQN}}==
=={{header|BQN}}==
<syntaxhighlight lang=bqn>ABC ← {
<syntaxhighlight lang="bqn">ABC ← {
Matches ← ⊑⊸(⊑∘∊¨)˜ /⊣ # blocks matching current letter
Matches ← ⊑⊸(⊑∘∊¨)˜ /⊣ # blocks matching current letter
Others ← <˘∘⍉∘(»⊸≥∨`)∘(≡⌜)/¨<∘⊣ # blocks without current matches
Others ← <˘∘⍉∘(»⊸≥∨`)∘(≡⌜)/¨<∘⊣ # blocks without current matches
Line 2,494: Line 2,494:


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<syntaxhighlight lang=bracmat>(
<syntaxhighlight lang="bracmat">(
( can-make-word
( can-make-word
= ABC blocks
= ABC blocks
Line 2,553: Line 2,553:
=={{header|C}}==
=={{header|C}}==
Recursive solution. Empty string returns true.
Recursive solution. Empty string returns true.
<syntaxhighlight lang=c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <ctype.h>
#include <ctype.h>


Line 2,610: Line 2,610:
This Method uses regular expressions to do the checking. Given that n = length of blocks string and
This Method uses regular expressions to do the checking. Given that n = length of blocks string and
m = length of word string, then CheckWord's time complexity comes out to about m*(n - (m-1)/2).
m = length of word string, then CheckWord's time complexity comes out to about m*(n - (m-1)/2).
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.IO;
using System.IO;
// Needed for the method.
// Needed for the method.
Line 2,652: Line 2,652:
</pre>
</pre>
'''Unoptimized'''
'''Unoptimized'''
<syntaxhighlight lang=csharp>using System.Collections.Generic;
<syntaxhighlight lang="csharp">using System.Collections.Generic;
using System.Linq;
using System.Linq;


Line 2,748: Line 2,748:
{{Works with|C++11}}
{{Works with|C++11}}
Build with:
Build with:
<syntaxhighlight lang=sh>g++-4.7 -Wall -std=c++0x abc.cpp</syntaxhighlight>
<syntaxhighlight lang="sh">g++-4.7 -Wall -std=c++0x abc.cpp</syntaxhighlight>
<syntaxhighlight lang=cpp>#include <iostream>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <vector>
#include <string>
#include <string>
Line 2,799: Line 2,799:
<b>module.ceylon</b>
<b>module.ceylon</b>


<syntaxhighlight lang=ceylon>
<syntaxhighlight lang="ceylon">
module rosetta.abc "1.0.0" {}
module rosetta.abc "1.0.0" {}
</syntaxhighlight>
</syntaxhighlight>
Line 2,805: Line 2,805:
<b>run.ceylon</b>
<b>run.ceylon</b>


<syntaxhighlight lang=ceylon>
<syntaxhighlight lang="ceylon">
shared void run() {
shared void run() {
printAndCanMakeWord("A", blocks);
printAndCanMakeWord("A", blocks);
Line 2,895: Line 2,895:
=={{header|Clojure}}==
=={{header|Clojure}}==
A translation of the Haskell solution.
A translation of the Haskell solution.
<syntaxhighlight lang=clojure>
<syntaxhighlight lang="clojure">
(def blocks
(def blocks
(-> "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM" (.split " ") vec))
(-> "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM" (.split " ") vec))
Line 2,928: Line 2,928:


=={{header|CLU}}==
=={{header|CLU}}==
<syntaxhighlight lang=clu>ucase = proc (s: string) returns (string)
<syntaxhighlight lang="clu">ucase = proc (s: string) returns (string)
rslt: array[char] := array[char]$predict(1,string$size(s))
rslt: array[char] := array[char]$predict(1,string$size(s))
for c: char in string$chars(s) do
for c: char in string$chars(s) do
Line 2,982: Line 2,982:


=={{header|CoffeeScript}}==
=={{header|CoffeeScript}}==
<syntaxhighlight lang=CoffeeScript>blockList = [ 'BO', 'XK', 'DQ', 'CP', 'NA', 'GT', 'RE', 'TG', 'QD', 'FS', 'JW', 'HU', 'VI', 'AN', 'OB', 'ER', 'FS', 'LY', 'PC', 'ZM' ]
<syntaxhighlight lang="coffeescript">blockList = [ 'BO', 'XK', 'DQ', 'CP', 'NA', 'GT', 'RE', 'TG', 'QD', 'FS', 'JW', 'HU', 'VI', 'AN', 'OB', 'ER', 'FS', 'LY', 'PC', 'ZM' ]


canMakeWord = (word="") ->
canMakeWord = (word="") ->
Line 3,012: Line 3,012:


=={{header|Comal}}==
=={{header|Comal}}==
<syntaxhighlight lang=comal>0010 FUNC can'make'word#(word$) CLOSED
<syntaxhighlight lang="comal">0010 FUNC can'make'word#(word$) CLOSED
0020 blocks$:=" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
0020 blocks$:=" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
0030 FOR i#:=1 TO LEN(word$) DO
0030 FOR i#:=1 TO LEN(word$) DO
Line 3,041: Line 3,041:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<syntaxhighlight lang=lisp>
<syntaxhighlight lang="lisp">
(defun word-possible-p (word blocks)
(defun word-possible-p (word blocks)
(cond
(cond
Line 3,077: Line 3,077:
=={{header|Component Pascal}}==
=={{header|Component Pascal}}==
{{Works with|BlackBox Component Builder}}
{{Works with|BlackBox Component Builder}}
<syntaxhighlight lang=oberon2>
<syntaxhighlight lang="oberon2">
MODULE ABCProblem;
MODULE ABCProblem;
IMPORT
IMPORT
Line 3,176: Line 3,176:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<syntaxhighlight lang=cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
include "strings.coh";


Line 3,235: Line 3,235:
{{trans|Python}}
{{trans|Python}}
A simple greedy algorithm is enough for the given sequence of blocks. canMakeWord is true on an empty word because you can compose it using zero blocks.
A simple greedy algorithm is enough for the given sequence of blocks. canMakeWord is true on an empty word because you can compose it using zero blocks.
<syntaxhighlight lang=d>import std.stdio, std.algorithm, std.string;
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.string;


bool canMakeWord(in string word, in string[] blocks) pure /*nothrow*/ @safe {
bool canMakeWord(in string word, in string[] blocks) pure /*nothrow*/ @safe {
Line 3,269: Line 3,269:
===@nogc Version===
===@nogc Version===
The same as the precedent version, but it avoids all heap allocations and it's lower-level and ASCII-only.
The same as the precedent version, but it avoids all heap allocations and it's lower-level and ASCII-only.
<syntaxhighlight lang=d>import std.ascii, core.stdc.stdlib;
<syntaxhighlight lang="d">import std.ascii, core.stdc.stdlib;


bool canMakeWord(in string word, in string[] blocks) nothrow @nogc
bool canMakeWord(in string word, in string[] blocks) nothrow @nogc
Line 3,312: Line 3,312:
This version is able to find the solution for the word "abba" given the blocks AB AB AC AC.
This version is able to find the solution for the word "abba" given the blocks AB AB AC AC.
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=d>import std.stdio, std.ascii, std.algorithm, std.array;
<syntaxhighlight lang="d">import std.stdio, std.ascii, std.algorithm, std.array;


alias Block = char[2];
alias Block = char[2];
Line 3,363: Line 3,363:
===Alternative Recursive Version===
===Alternative Recursive Version===
This version doesn't shuffle the input blocks, but it's more complex and it allocates an array of indexes.
This version doesn't shuffle the input blocks, but it's more complex and it allocates an array of indexes.
<syntaxhighlight lang=d>import std.stdio, std.ascii, std.algorithm, std.array, std.range;
<syntaxhighlight lang="d">import std.stdio, std.ascii, std.algorithm, std.array, std.range;


alias Block = char[2];
alias Block = char[2];
Line 3,409: Line 3,409:
=={{header|Delphi}}==
=={{header|Delphi}}==
Just to be different I implemented a block as a set of (2) char rather than as an array of (2) char.
Just to be different I implemented a block as a set of (2) char rather than as an array of (2) char.
<syntaxhighlight lang=Delphi>program ABC;
<syntaxhighlight lang="delphi">program ABC;
{$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}


Line 3,486: Line 3,486:


=={{header|Draco}}==
=={{header|Draco}}==
<syntaxhighlight lang=draco>\util.g
<syntaxhighlight lang="draco">\util.g


proc nonrec ucase(char c) char:
proc nonrec ucase(char c) char:
Line 3,552: Line 3,552:
{{trans|Swift}}
{{trans|Swift}}


<syntaxhighlight lang=dyalect>func blockable(str) {
<syntaxhighlight lang="dyalect">func blockable(str) {
var blocks = [
var blocks = [
"BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
"BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
Line 3,590: Line 3,590:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(lib 'list) ;; list-delete
(lib 'list) ;; list-delete


Line 3,626: Line 3,626:
=={{header|Ela}}==
=={{header|Ela}}==
{{trans|Haskell}}
{{trans|Haskell}}
<syntaxhighlight lang=ela>open list monad io char
<syntaxhighlight lang="ela">open list monad io char


:::IO
:::IO
Line 3,656: Line 3,656:
=={{header|Elena}}==
=={{header|Elena}}==
ELENA 5.0
ELENA 5.0
<syntaxhighlight lang=elena>import system'routines;
<syntaxhighlight lang="elena">import system'routines;
import system'collections;
import system'collections;
import extensions;
import extensions;
Line 3,716: Line 3,716:
{{trans|Erlang}}
{{trans|Erlang}}
{{works with|Elixir|1.3}}
{{works with|Elixir|1.3}}
<syntaxhighlight lang=elixir>defmodule ABC do
<syntaxhighlight lang="elixir">defmodule ABC do
def can_make_word(word, avail) do
def can_make_word(word, avail) do
can_make_word(String.upcase(word) |> to_charlist, avail, [])
can_make_word(String.upcase(word) |> to_charlist, avail, [])
Line 3,745: Line 3,745:


=={{header|Erlang}}==
=={{header|Erlang}}==
<syntaxhighlight lang=erlang>-module(abc).
<syntaxhighlight lang="erlang">-module(abc).
-export([can_make_word/1, can_make_word/2, blocks/0]).
-export([can_make_word/1, can_make_word/2, blocks/0]).


Line 3,774: Line 3,774:


=={{header|ERRE}}==
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE>
<syntaxhighlight lang="erre">
PROGRAM BLOCKS
PROGRAM BLOCKS


Line 3,807: Line 3,807:
=={{header|Euphoria}}==
=={{header|Euphoria}}==
implemented using OpenEuphoria
implemented using OpenEuphoria
<syntaxhighlight lang=Euphoria>
<syntaxhighlight lang="euphoria">
include std/text.e
include std/text.e


Line 3,859: Line 3,859:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<p>This solution does not depend on the order of the blocks, neither on the symmetry of blocks we see in the example block set. (Symmetry: if AB is a block, an A comes only with another AB|BA)</p>
<p>This solution does not depend on the order of the blocks, neither on the symmetry of blocks we see in the example block set. (Symmetry: if AB is a block, an A comes only with another AB|BA)</p>
<syntaxhighlight lang=fsharp>let rec spell_word_with blocks w =
<syntaxhighlight lang="fsharp">let rec spell_word_with blocks w =
let rec look_for_right_candidate candidates noCandidates c rest =
let rec look_for_right_candidate candidates noCandidates c rest =
match candidates with
match candidates with
Line 3,906: Line 3,906:


=={{header|Factor}}==
=={{header|Factor}}==
<syntaxhighlight lang=factor>USING: assocs combinators.short-circuit formatting grouping io
<syntaxhighlight lang="factor">USING: assocs combinators.short-circuit formatting grouping io
kernel math math.statistics qw sequences sets unicode ;
kernel math math.statistics qw sequences sets unicode ;
IN: rosetta-code.abc-problem
IN: rosetta-code.abc-problem
Line 3,972: Line 3,972:
=={{header|FBSL}}==
=={{header|FBSL}}==
This approach uses a string, blanking out the pair previously found. Probably faster than array manipulation.
This approach uses a string, blanking out the pair previously found. Probably faster than array manipulation.
<syntaxhighlight lang=qbasic>
<syntaxhighlight lang="qbasic">
#APPTYPE CONSOLE
#APPTYPE CONSOLE
SUB MAIN()
SUB MAIN()
Line 4,037: Line 4,037:
{{works with|gforth|0.7.3}}
{{works with|gforth|0.7.3}}


<syntaxhighlight lang=forth>: blockslist s" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM" ;
<syntaxhighlight lang="forth">: blockslist s" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM" ;
variable blocks
variable blocks
: allotblocks ( -- ) here blockslist dup allot here over - swap move blocks ! ;
: allotblocks ( -- ) here blockslist dup allot here over - swap move blocks ! ;
Line 4,081: Line 4,081:
=={{header|Fortran}}==
=={{header|Fortran}}==
Attempts to write the word read from unit 5. Please find the output, bash command, and gfortran compilation instructions as commentary at the start of the source, which starts right away!
Attempts to write the word read from unit 5. Please find the output, bash command, and gfortran compilation instructions as commentary at the start of the source, which starts right away!
<syntaxhighlight lang=Fortran>!-*- mode: compilation; default-directory: "/tmp/" -*-
<syntaxhighlight lang="fortran">!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Thu Jun 5 01:52:03
!Compilation started at Thu Jun 5 01:52:03
!
!
Line 4,158: Line 4,158:


The following source begins with some support routines. Subroutine PLAY inspects the collection of blocks to make various remarks, and function CANBLOCK reports on whether a word can be spelled out with the supplied blocks. The source requires only a few of the F90 features. The MODULE protocol eases communication, but the key feature is that subprograms can now declare arrays of a size determined on entry via parameters. Previously, a constant with the largest-possible size would be required.
The following source begins with some support routines. Subroutine PLAY inspects the collection of blocks to make various remarks, and function CANBLOCK reports on whether a word can be spelled out with the supplied blocks. The source requires only a few of the F90 features. The MODULE protocol eases communication, but the key feature is that subprograms can now declare arrays of a size determined on entry via parameters. Previously, a constant with the largest-possible size would be required.
<syntaxhighlight lang=Fortran>
<syntaxhighlight lang="fortran">
MODULE PLAYPEN !Messes with a set of alphabet blocks.
MODULE PLAYPEN !Messes with a set of alphabet blocks.
INTEGER MSG !Output unit number.
INTEGER MSG !Output unit number.
Line 4,461: Line 4,461:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>' version 28-01-2019
<syntaxhighlight lang="freebasic">' version 28-01-2019
' compile with: fbc -s console
' compile with: fbc -s console


Line 4,515: Line 4,515:


=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<syntaxhighlight lang=futurebasic>
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
include "NSLog.incl"


Line 4,571: Line 4,571:
=={{header|Gambas}}==
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ae860292d4588b3627d77c85bcc634ee Click this link to run this code]'''
'''[https://gambas-playground.proko.eu/?gist=ae860292d4588b3627d77c85bcc634ee Click this link to run this code]'''
<syntaxhighlight lang=gambas>Public Sub Main()
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sCheck As String[] = ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"]
Dim sCheck As String[] = ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"]
Dim sBlock As String[] = ["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"]
Dim sBlock As String[] = ["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"]
Line 4,611: Line 4,611:


=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 4,663: Line 4,663:
=={{header|Groovy}}==
=={{header|Groovy}}==
Solution:
Solution:
<syntaxhighlight lang=groovy>class ABCSolver {
<syntaxhighlight lang="groovy">class ABCSolver {
def blocks
def blocks


Line 4,677: Line 4,677:


Test:
Test:
<syntaxhighlight lang=groovy>def a = new ABCSolver(["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
<syntaxhighlight lang="groovy">def a = new ABCSolver(["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"])
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"])


Line 4,696: Line 4,696:
=={{header|Harbour}}==
=={{header|Harbour}}==
Harbour Project implements a cross-platform Clipper/xBase compiler.
Harbour Project implements a cross-platform Clipper/xBase compiler.
<syntaxhighlight lang=visualfoxpro>PROCEDURE Main()
<syntaxhighlight lang="visualfoxpro">PROCEDURE Main()


LOCAL cStr
LOCAL cStr
Line 4,741: Line 4,741:


The following function returns a list of all the solutions. Since Haskell is lazy, testing whether the list is null will only do the minimal amount of work necessary to determine whether a solution exists.
The following function returns a list of all the solutions. Since Haskell is lazy, testing whether the list is null will only do the minimal amount of work necessary to determine whether a solution exists.
<syntaxhighlight lang=haskell>import Data.List (delete)
<syntaxhighlight lang="haskell">import Data.List (delete)
import Data.Char (toUpper)
import Data.Char (toUpper)


Line 4,771: Line 4,771:
Or, in terms of the bind operator:
Or, in terms of the bind operator:


<syntaxhighlight lang=haskell>import Data.Char (toUpper)
<syntaxhighlight lang="haskell">import Data.Char (toUpper)
import Data.List (delete)
import Data.List (delete)


Line 4,823: Line 4,823:


Works in both languages:
Works in both languages:
<syntaxhighlight lang=unicon>procedure main(A)
<syntaxhighlight lang="unicon">procedure main(A)
blocks := ["bo","xk","dq","cp","na","gt","re","tg","qd","fs",
blocks := ["bo","xk","dq","cp","na","gt","re","tg","qd","fs",
"jw","hu","vi","an","ob","er","fs","ly","pc","zm",&null]
"jw","hu","vi","an","ob","er","fs","ly","pc","zm",&null]
Line 4,864: Line 4,864:
=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<syntaxhighlight lang=j>reduce=: verb define
<syntaxhighlight lang="j">reduce=: verb define
'rows cols'=. i.&.> $y
'rows cols'=. i.&.> $y
for_c. cols do.
for_c. cols do.
Line 4,876: Line 4,876:
abc=: *./@(+./)@reduce@(e."1~ ,)&toupper :: 0:</syntaxhighlight>
abc=: *./@(+./)@reduce@(e."1~ ,)&toupper :: 0:</syntaxhighlight>
'''Examples:'''
'''Examples:'''
<syntaxhighlight lang=j> Blocks=: ];._2 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM '
<syntaxhighlight lang="j"> Blocks=: ];._2 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM '
ExampleWords=: <;._2 'A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE '
ExampleWords=: <;._2 'A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE '


Line 4,892: Line 4,892:


'''Tacit version'''
'''Tacit version'''
<syntaxhighlight lang=j>delElem=: {~<@<@<
<syntaxhighlight lang="j">delElem=: {~<@<@<
uppc=:(-32*96&<*.123&>)&.(3&u:)
uppc=:(-32*96&<*.123&>)&.(3&u:)
reduc=: ] delElem 1 i.~e."0 1
reduc=: ] delElem 1 i.~e."0 1
Line 4,919: Line 4,919:
Another approach might be:
Another approach might be:


<syntaxhighlight lang=J>Blocks=: >;:'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM '
<syntaxhighlight lang="j">Blocks=: >;:'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM '
ExampleWords=: ;: 'A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE '
ExampleWords=: ;: 'A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE '


Line 4,932: Line 4,932:
Example use:
Example use:


<syntaxhighlight lang=J> Blocks canform 0{::ExampleWords
<syntaxhighlight lang="j"> Blocks canform 0{::ExampleWords
1
1
Blocks canform 1{::ExampleWords
Blocks canform 1{::ExampleWords
Line 4,955: Line 4,955:
For example:
For example:


<syntaxhighlight lang=J> Blocks canform 0{::ExampleWords
<syntaxhighlight lang="j"> Blocks canform 0{::ExampleWords
1
1
word
word
Line 4,975: Line 4,975:
{{trans|C}}
{{trans|C}}
{{works with|Java|1.6+}}
{{works with|Java|1.6+}}
<syntaxhighlight lang=java5>import java.util.Arrays;
<syntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
import java.util.List;
import java.util.List;
Line 5,025: Line 5,025:
====Imperative====
====Imperative====
The following method uses regular expressions and the string replace function to allow more support for older browsers.
The following method uses regular expressions and the string replace function to allow more support for older browsers.
<syntaxhighlight lang=javascript>var blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
<syntaxhighlight lang="javascript">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) {
function CheckWord(blocks, word) {
Line 5,076: Line 5,076:


====Functional====
====Functional====
<syntaxhighlight lang=JavaScript>(function (strWords) {
<syntaxhighlight lang="javascript">(function (strWords) {


var strBlocks =
var strBlocks =
Line 5,125: Line 5,125:
})('A bark BooK TReAT COMMON squAD conFUSE');</syntaxhighlight>
})('A bark BooK TReAT COMMON squAD conFUSE');</syntaxhighlight>
{{Out}}
{{Out}}
<syntaxhighlight lang=JavaScript>A -> NA
<syntaxhighlight lang="javascript">A -> NA
bark -> BO NA RE XK
bark -> BO NA RE XK
BooK: [no solution]
BooK: [no solution]
Line 5,135: Line 5,135:
===ES6===
===ES6===
====Imperative====
====Imperative====
<syntaxhighlight lang=javascript>let characters = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
<syntaxhighlight lang="javascript">let characters = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
let blocks = characters.split(" ").map(pair => pair.split(""));
let blocks = characters.split(" ").map(pair => pair.split(""));
Line 5,181: Line 5,181:
====Functional====
====Functional====
{{Trans|Haskell}}
{{Trans|Haskell}}
<syntaxhighlight lang=JavaScript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 5,259: Line 5,259:


=={{header|jq}}==
=={{header|jq}}==
The problem description seems to imply that if a letter, X, appears on more than one block, its partner will be the same on all blocks. This makes the problem trivial.<syntaxhighlight lang=jq>
The problem description seems to imply that if a letter, X, appears on more than one block, its partner will be the same on all blocks. This makes the problem trivial.<syntaxhighlight lang="jq">
# when_index(cond;ary) returns the index of the first element in ary
# when_index(cond;ary) returns the index of the first element in ary
# that satisfies cond; it uses a helper function that takes advantage
# that satisfies cond; it uses a helper function that takes advantage
Line 5,289: Line 5,289:
end
end
end;</syntaxhighlight>
end;</syntaxhighlight>
Task:<syntaxhighlight lang=jq>def task:
Task:<syntaxhighlight lang="jq">def task:
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"] as $blocks
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"] as $blocks
Line 5,305: Line 5,305:
=={{header|Jsish}}==
=={{header|Jsish}}==
Based on Javascript ES5 imperative solution.
Based on Javascript ES5 imperative solution.
<syntaxhighlight lang=javascript>#!/usr/bin/env jsish
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* ABC problem, in Jsish. Can word be spelled with the given letter blocks. */
/* ABC problem, in Jsish. Can word be spelled with the given letter blocks. */
var blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
var blocks = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM";
Line 5,358: Line 5,358:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang=julia>using Printf
<syntaxhighlight lang="julia">using Printf


function abc(str::AbstractString, list)
function abc(str::AbstractString, list)
Line 5,389: Line 5,389:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=scala>object ABC_block_checker {
<syntaxhighlight lang="scala">object ABC_block_checker {
fun run() {
fun run() {
println("\"\": " + blocks.canMakeWord(""))
println("\"\": " + blocks.canMakeWord(""))
Line 5,440: Line 5,440:
=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==
===Recursive solution===
===Recursive solution===
<syntaxhighlight lang=lb>
<syntaxhighlight lang="lb">
print "Rosetta Code - ABC problem (recursive solution)"
print "Rosetta Code - ABC problem (recursive solution)"
print
print
Line 5,505: Line 5,505:
</pre>
</pre>
===Procedural solution===
===Procedural solution===
<syntaxhighlight lang=lb>
<syntaxhighlight lang="lb">
print "Rosetta Code - ABC problem (procedural solution)"
print "Rosetta Code - ABC problem (procedural solution)"
print
print
Line 5,662: Line 5,662:


=={{header|Logo}}==
=={{header|Logo}}==
<syntaxhighlight lang=logo>make "blocks [[B O] [X K] [D Q] [C P] [N A] [G T] [R E] [T G] [Q D] [F S]
<syntaxhighlight lang="logo">make "blocks [[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]]
[J W] [H U] [V I] [A N] [O B] [E R] [F S] [L Y] [P C] [Z M]]


Line 5,694: Line 5,694:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang=lua>blocks = {
<syntaxhighlight lang="lua">blocks = {
{"B","O"}; {"X","K"}; {"D","Q"}; {"C","P"};
{"B","O"}; {"X","K"}; {"D","Q"}; {"C","P"};
{"N","A"}; {"G","T"}; {"R","E"}; {"T","G"};
{"N","A"}; {"G","T"}; {"R","E"}; {"T","G"};
Line 5,740: Line 5,740:




<syntaxhighlight lang=M2000 Interpreter>
<syntaxhighlight lang="m2000 interpreter">
Module ABC {
Module ABC {
can_make_word("A")
can_make_word("A")
Line 5,775: Line 5,775:


=={{header|Maple}}==
=={{header|Maple}}==
<syntaxhighlight lang=maple>canSpell := proc(w)
<syntaxhighlight lang="maple">canSpell := proc(w)
local blocks, i, j, word, letterFound;
local blocks, i, j, word, letterFound;
blocks := Array([["B", "O"], ["X", "K"], ["D", "Q"], ["C", "P"], ["N", "A"], ["G", "T"], ["R", "E"], ["T", "G"],
blocks := Array([["B", "O"], ["X", "K"], ["D", "Q"], ["C", "P"], ["N", "A"], ["G", "T"], ["R", "E"], ["T", "G"],
Line 5,810: Line 5,810:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica>
<syntaxhighlight lang="mathematica">
blocks=Partition[Characters[ToLowerCase["BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"]],2];
blocks=Partition[Characters[ToLowerCase["BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"]],2];
ClearAll[DoStep,ABCBlockQ]
ClearAll[DoStep,ABCBlockQ]
Line 5,843: Line 5,843:


=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=MATLAB>function testABC
<syntaxhighlight lang="matlab">function testABC
combos = ['BO' ; 'XK' ; 'DQ' ; 'CP' ; 'NA' ; 'GT' ; 'RE' ; 'TG' ; 'QD' ; ...
combos = ['BO' ; 'XK' ; 'DQ' ; 'CP' ; 'NA' ; 'GT' ; 'RE' ; 'TG' ; 'QD' ; ...
'FS' ; 'JW' ; 'HU' ; 'VI' ; 'AN' ; 'OB' ; 'ER' ; 'FS' ; 'LY' ; ...
'FS' ; 'JW' ; 'HU' ; 'VI' ; 'AN' ; 'OB' ; 'ER' ; 'FS' ; 'LY' ; ...
Line 5,884: Line 5,884:
Recursively checks if the word is possible if a block is removed from the array.
Recursively checks if the word is possible if a block is removed from the array.


<syntaxhighlight lang=MAXScript>
<syntaxhighlight lang="maxscript">
-- This is the blocks array
-- This is the blocks array
global GlobalBlocks = #("BO","XK","DQ","CP","NA", \
global GlobalBlocks = #("BO","XK","DQ","CP","NA", \
Line 5,986: Line 5,986:


'''Output:'''
'''Output:'''
<syntaxhighlight lang=MAXScript>
<syntaxhighlight lang="maxscript">
iswordpossible "a"
iswordpossible "a"
true
true
Line 6,005: Line 6,005:


=== Non-recursive ===
=== Non-recursive ===
<syntaxhighlight lang=MAXScript>
<syntaxhighlight lang="maxscript">
fn isWordPossible2 word =
fn isWordPossible2 word =
(
(
Line 6,042: Line 6,042:
Then:
Then:


<syntaxhighlight lang=MAXScript>
<syntaxhighlight lang="maxscript">
iswordpossible "water"
iswordpossible "water"
true
true
Line 6,052: Line 6,052:


=={{header|Mercury}}==
=={{header|Mercury}}==
<syntaxhighlight lang=Mercury>:- module abc.
<syntaxhighlight lang="mercury">:- module abc.
:- interface.
:- interface.
:- import_module io.
:- import_module io.
Line 6,090: Line 6,090:


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<syntaxhighlight lang=MiniScript>allBlocks = ["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"]
<syntaxhighlight lang="miniscript">allBlocks = ["BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS", "JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM"]
swap = function(list, index1, index2)
swap = function(list, index1, index2)
Line 6,120: Line 6,120:
=={{header|Nim}}==
=={{header|Nim}}==
{{works with|Nim|0.20.0}}
{{works with|Nim|0.20.0}}
<syntaxhighlight lang=nim>import std / strutils
<syntaxhighlight lang="nim">import std / strutils


func canMakeWord(blocks: seq[string]; word: string): bool =
func canMakeWord(blocks: seq[string]; word: string): bool =
Line 6,164: Line 6,164:
=={{header|Oberon-2}}==
=={{header|Oberon-2}}==
Works with oo2c Version 2
Works with oo2c Version 2
<syntaxhighlight lang=oberon2>
<syntaxhighlight lang="oberon2">
MODULE ABCBlocks;
MODULE ABCBlocks;
IMPORT
IMPORT
Line 6,262: Line 6,262:
=={{header|Objeck}}==
=={{header|Objeck}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=objeck>class Abc {
<syntaxhighlight lang="objeck">class Abc {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
blocks := ["BO", "XK", "DQ", "CP", "NA",
blocks := ["BO", "XK", "DQ", "CP", "NA",
Line 6,320: Line 6,320:


=={{header|OCaml}}==
=={{header|OCaml}}==
<syntaxhighlight lang=ocaml>let blocks = [
<syntaxhighlight lang="ocaml">let blocks = [
('B', 'O'); ('X', 'K'); ('D', 'Q'); ('C', 'P');
('B', 'O'); ('X', 'K'); ('D', 'Q'); ('C', 'P');
('N', 'A'); ('G', 'T'); ('R', 'E'); ('T', 'G');
('N', 'A'); ('G', 'T'); ('R', 'E'); ('T', 'G');
Line 6,375: Line 6,375:
=={{header|Oforth}}==
=={{header|Oforth}}==


<syntaxhighlight lang=Oforth>import: mapping
<syntaxhighlight lang="oforth">import: mapping


["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"]
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"]
Line 6,398: Line 6,398:
=={{header|OpenEdge/Progress}}==
=={{header|OpenEdge/Progress}}==


<syntaxhighlight lang=Progress (Openedge ABL)>FUNCTION canMakeWord RETURNS LOGICAL (INPUT pWord AS CHARACTER) FORWARD.
<syntaxhighlight lang="progress (openedge abl)">FUNCTION canMakeWord RETURNS LOGICAL (INPUT pWord AS CHARACTER) FORWARD.


/* List of blocks */
/* List of blocks */
Line 6,514: Line 6,514:


=={{header|Order}}==
=={{header|Order}}==
<syntaxhighlight lang=Order>#include <order/interpreter.h>
<syntaxhighlight lang="order">#include <order/interpreter.h>
#include <order/lib.h>
#include <order/lib.h>


Line 6,684: Line 6,684:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<syntaxhighlight lang=parigp>BLOCKS = "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM";
<syntaxhighlight lang="parigp">BLOCKS = "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM";
WORDS = ["A","Bark","BOOK","Treat","COMMON","SQUAD","conFUSE"];
WORDS = ["A","Bark","BOOK","Treat","COMMON","SQUAD","conFUSE"];


Line 6,715: Line 6,715:
{{works with|Free Pascal|2.6.2}}
{{works with|Free Pascal|2.6.2}}


<syntaxhighlight lang=Pascal>
<syntaxhighlight lang="pascal">
#!/usr/bin/instantfpc
#!/usr/bin/instantfpc
//program ABCProblem;
//program ABCProblem;
Line 6,805: Line 6,805:
=={{header|Perl}}==
=={{header|Perl}}==
Recursive solution that can handle characters appearing on different blocks:
Recursive solution that can handle characters appearing on different blocks:
<syntaxhighlight lang=perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use warnings;
use strict;
use strict;
Line 6,832: Line 6,832:
}</syntaxhighlight>
}</syntaxhighlight>
<p>Testing:
<p>Testing:
<syntaxhighlight lang=perl>use Test::More tests => 8;
<syntaxhighlight lang="perl">use Test::More tests => 8;


my @blocks1 = qw(BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM);
my @blocks1 = qw(BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM);
Line 6,847: Line 6,847:
</syntaxhighlight>
</syntaxhighlight>
===Regex based alternate===
===Regex based alternate===
<syntaxhighlight lang=perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/ABC_Problem
use strict; # https://rosettacode.org/wiki/ABC_Problem
Line 6,877: Line 6,877:
=={{header|Phix}}==
=={{header|Phix}}==
Recursive solution which also solves the extra problems on the discussion page.
Recursive solution which also solves the extra problems on the discussion page.
<!--<syntaxhighlight lang=Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #004080;">sequence</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">used</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">words</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">used</span>
Line 6,930: Line 6,930:
=={{header|PHP}}==
=={{header|PHP}}==


<syntaxhighlight lang=PHP>
<syntaxhighlight lang="php">
<?php
<?php
$words = array("A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "Confuse");
$words = array("A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "Confuse");
Line 6,973: Line 6,973:
=={{header|Picat}}==
=={{header|Picat}}==
Showing both a Picat style version (check_word/2) and a Prolog style recursive version (check_word2/2). go2/0 generates all possible solutions (using fail/0) to backtrack.
Showing both a Picat style version (check_word/2) and a Prolog style recursive version (check_word2/2). go2/0 generates all possible solutions (using fail/0) to backtrack.
<syntaxhighlight lang=Picat>go =>
<syntaxhighlight lang="picat">go =>
test_it(check_word),
test_it(check_word),
test_it(check_word2),
test_it(check_word2),
Line 7,156: Line 7,156:
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
Mapping and recursion.
Mapping and recursion.
<syntaxhighlight lang=PicoLisp>(setq *Blocks
<syntaxhighlight lang="picolisp">(setq *Blocks
'((B O) (X K) (D Q) (C P) (N A) (G T) (R E)
'((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)
(T G) (Q D) (F S) (J W) (H U) (V I) (A N)
Line 7,192: Line 7,192:
=={{header|PL/I}}==
=={{header|PL/I}}==
===version 1===
===version 1===
<syntaxhighlight lang=pli>ABC: procedure options (main); /* 12 January 2014 */
<syntaxhighlight lang="pli">ABC: procedure options (main); /* 12 January 2014 */


declare word character (20) varying, blocks character (200) varying initial
declare word character (20) varying, blocks character (200) varying initial
Line 7,229: Line 7,229:


===version 2===
===version 2===
<syntaxhighlight lang=pli>*process source attributes xref or(!) options nest;
<syntaxhighlight lang="pli">*process source attributes xref or(!) options nest;
abc: Proc Options(main);
abc: Proc Options(main);
/* REXX --------------------------------------------------------------
/* REXX --------------------------------------------------------------
Line 7,361: Line 7,361:


=={{header|PL/M}}==
=={{header|PL/M}}==
<syntaxhighlight lang=plm>100H:
<syntaxhighlight lang="plm">100H:


/* ABC PROBLEM ON $-TERMINATED STRING */
/* ABC PROBLEM ON $-TERMINATED STRING */
Line 7,433: Line 7,433:
Works with PowerBASIC 6 Console Compiler
Works with PowerBASIC 6 Console Compiler


<syntaxhighlight lang=PowerBASIC>#COMPILE EXE
<syntaxhighlight lang="powerbasic">#COMPILE EXE
#DIM ALL
#DIM ALL
'
'
Line 7,602: Line 7,602:


=={{header|PowerShell}}==
=={{header|PowerShell}}==
<syntaxhighlight lang=powershell><#
<syntaxhighlight lang="powershell"><#
.Synopsis
.Synopsis
ABC Problem
ABC Problem
Line 7,784: Line 7,784:
Works with SWI-Prolog 6.5.3
Works with SWI-Prolog 6.5.3


<syntaxhighlight lang=Prolog>abc_problem :-
<syntaxhighlight lang="prolog">abc_problem :-
maplist(abc_problem, ['', 'A', bark, bOOk, treAT, 'COmmon', sQuaD, 'CONFUSE']).
maplist(abc_problem, ['', 'A', bark, bOOk, treAT, 'COmmon', sQuaD, 'CONFUSE']).


Line 7,827: Line 7,827:
{{works with|SWI Prolog 7}}
{{works with|SWI Prolog 7}}


<syntaxhighlight lang=Prolog>:- use_module([ library(chr),
<syntaxhighlight lang="prolog">:- use_module([ library(chr),
abathslib(protelog/composer) ]).
abathslib(protelog/composer) ]).


Line 7,849: Line 7,849:
Demonstration:
Demonstration:


<syntaxhighlight lang=Prolog>?- can_build_word("A").
<syntaxhighlight lang="prolog">?- can_build_word("A").
true.
true.
?- can_build_word("BARK").
?- can_build_word("BARK").
Line 7,866: Line 7,866:
=={{header|PureBasic}}==
=={{header|PureBasic}}==
===PureBasic: Iterative===
===PureBasic: Iterative===
<syntaxhighlight lang=purebasic>EnableExplicit
<syntaxhighlight lang="purebasic">EnableExplicit
#LETTERS = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM "
#LETTERS = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM "


Line 7,900: Line 7,900:


===PureBasic: Recursive===
===PureBasic: Recursive===
<syntaxhighlight lang=purebasic>#LETTERS = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM "
<syntaxhighlight lang="purebasic">#LETTERS = "BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM "


Macro test(t)
Macro test(t)
Line 7,932: Line 7,932:


===Python: Iterative, with tests===
===Python: Iterative, with tests===
<syntaxhighlight lang=python>
<syntaxhighlight lang="python">
'''
'''
Note that this code is broken, e.g., it won't work when
Note that this code is broken, e.g., it won't work when
Line 8,007: Line 8,007:


===Python: Recursive===
===Python: Recursive===
<syntaxhighlight lang=python>BLOCKS = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split()
<syntaxhighlight lang="python">BLOCKS = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split()


def _abc(word, blocks):
def _abc(word, blocks):
Line 8,044: Line 8,044:


===Python: Recursive, telling how===
===Python: Recursive, telling how===
<syntaxhighlight lang=python>def mkword(w, b):
<syntaxhighlight lang="python">def mkword(w, b):
if not w: return []
if not w: return []


Line 8,076: Line 8,076:
=={{header|q}}==
=={{header|q}}==
The possibility of ‘backtracking’, discussed in the FORTRAN solution above (and not tested by the example set) makes this a classic tree search: wherever there is a choice of blocks from which to pick the next letter, each choice must be tested.
The possibility of ‘backtracking’, discussed in the FORTRAN solution above (and not tested by the example set) makes this a classic tree search: wherever there is a choice of blocks from which to pick the next letter, each choice must be tested.
<syntaxhighlight lang=q>BLOCKS:string`BO`XK`DQ`CP`NA`GT`RE`TG`QD`FS`JW`HU`VI`AN`OB`ER`FS`LY`PC`ZM
<syntaxhighlight lang="q">BLOCKS:string`BO`XK`DQ`CP`NA`GT`RE`TG`QD`FS`JW`HU`VI`AN`OB`ER`FS`LY`PC`ZM
WORDS:string`A`BARK`BOOK`TREAT`COMMON`SQUAD`CONFUSE
WORDS:string`A`BARK`BOOK`TREAT`COMMON`SQUAD`CONFUSE


Line 8,084: Line 8,084:
any(1_s).z.s/:b(til count b)except/:where found] }</syntaxhighlight>
any(1_s).z.s/:b(til count b)except/:where found] }</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=q>q)WORDS cmw\:BLOCKS
<syntaxhighlight lang="q">q)WORDS cmw\:BLOCKS
1101011b</syntaxhighlight>
1101011b</syntaxhighlight>
The first expression tests whether the string <code>s</code> is empty. If so, the result is true. This matches two cases: either the string is empty and can be made from any set of blocks; or all its letters have been matched and there is nothing more to check.
The first expression tests whether the string <code>s</code> is empty. If so, the result is true. This matches two cases: either the string is empty and can be made from any set of blocks; or all its letters have been matched and there is nothing more to check.
Line 8,093: Line 8,093:


To meet the requirement for case-insensitivity and to display the results, apply the above within a wrapper.
To meet the requirement for case-insensitivity and to display the results, apply the above within a wrapper.
<syntaxhighlight lang=q>Words:string`A`bark`BOOK`Treat`COMMON`squad`CONFUSE
<syntaxhighlight lang="q">Words:string`A`bark`BOOK`Treat`COMMON`squad`CONFUSE
cmwi:{(`$x), `false`true cmw . upper each(x;y) }</syntaxhighlight>
cmwi:{(`$x), `false`true cmw . upper each(x;y) }</syntaxhighlight>
{{out}}
{{out}}
<syntaxhighlight lang=q>q)Words cmwi\:BLOCKS
<syntaxhighlight lang="q">q)Words cmwi\:BLOCKS
A true
A true
bark true
bark true
Line 8,115: Line 8,115:
This solution assumes the constraint that if a letter appears on more than one block those blocks are identical (as in the example set) so backtracking is not required.
This solution assumes the constraint that if a letter appears on more than one block those blocks are identical (as in the example set) so backtracking is not required.


<syntaxhighlight lang=Quackery>[ $ "BOXKDQCPNAGTRETGQDFS"
<syntaxhighlight lang="quackery">[ $ "BOXKDQCPNAGTRETGQDFS"
$ "JWHUVIANOBERFSLYPCZM"
$ "JWHUVIANOBERFSLYPCZM"
join ] constant is blocks ( --> $ )
join ] constant is blocks ( --> $ )
Line 8,179: Line 8,179:
This solution does not assume the constraint that if a letter appears on more than one block those blocks are identical (as in the example set) so backtracking is required.
This solution does not assume the constraint that if a letter appears on more than one block those blocks are identical (as in the example set) so backtracking is required.


<syntaxhighlight lang=Quackery>[ ' [ 0 ] swap
<syntaxhighlight lang="quackery">[ ' [ 0 ] swap
witheach
witheach
[ over -1 peek
[ over -1 peek
Line 8,239: Line 8,239:
Vectorised function for R which will take a character vector and return a logical vector of equal length with TRUE and FALSE as appropriate for words which can/cannot be made with the blocks.
Vectorised function for R which will take a character vector and return a logical vector of equal length with TRUE and FALSE as appropriate for words which can/cannot be made with the blocks.


<syntaxhighlight lang=R>blocks <- rbind(c("B","O"),
<syntaxhighlight lang="r">blocks <- rbind(c("B","O"),
c("X","K"),
c("X","K"),
c("D","Q"),
c("D","Q"),
Line 8,289: Line 8,289:
===Without recursion===
===Without recursion===
Second version without recursion and giving every unique combination of blocks for each word:
Second version without recursion and giving every unique combination of blocks for each word:
<syntaxhighlight lang=R>canMakeNoRecursion <- function(x) {
<syntaxhighlight lang="r">canMakeNoRecursion <- function(x) {
x <- toupper(x)
x <- toupper(x)
charList <- strsplit(x, character(0))
charList <- strsplit(x, character(0))
Line 8,393: Line 8,393:
So '(can-make-word? "")' is true for me.
So '(can-make-word? "")' is true for me.


<syntaxhighlight lang=racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define block-strings
(define block-strings
(list "BO" "XK" "DQ" "CP" "NA"
(list "BO" "XK" "DQ" "CP" "NA"
Line 8,451: Line 8,451:
{{works with|rakudo|6.0.c}}
{{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.
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.
<syntaxhighlight lang=perl6>multi can-spell-word(Str $word, @blocks) {
<syntaxhighlight lang="raku" line>multi can-spell-word(Str $word, @blocks) {
my @regex = @blocks.map({ my @c = .comb; rx/<@c>/ }).grep: { .ACCEPTS($word.uc) }
my @regex = @blocks.map({ my @c = .comb; rx/<@c>/ }).grep: { .ACCEPTS($word.uc) }
can-spell-word $word.uc.comb.list, @regex;
can-spell-word $word.uc.comb.list, @regex;
Line 8,482: Line 8,482:


=={{header|RapidQ}}==
=={{header|RapidQ}}==
<syntaxhighlight lang=vb>dim Blocks as string
<syntaxhighlight lang="vb">dim Blocks as string
dim InWord as string
dim InWord as string


Line 8,520: Line 8,520:


=={{header|Red}}==
=={{header|Red}}==
<syntaxhighlight lang=Red>Red []
<syntaxhighlight lang="red">Red []
test: func [ s][
test: func [ s][
p: copy "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
p: copy "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
Line 8,550: Line 8,550:
=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<syntaxhighlight lang=rexx>/*REXX pgm finds if words can be spelt from a pool of toy blocks (each having 2 letters)*/
<syntaxhighlight lang="rexx">/*REXX pgm finds if words can be spelt from a pool of toy blocks (each having 2 letters)*/
list= 'A bark bOOk treat common squaD conFuse' /*words can be: upper/lower/mixed case*/
list= 'A bark bOOk treat common squaD conFuse' /*words can be: upper/lower/mixed case*/
blocks= 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'
blocks= 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'
Line 8,583: Line 8,583:


===version 2===
===version 2===
<syntaxhighlight lang=rexx>/* REXX ---------------------------------------------------------------
<syntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 10.01.2014 Walter Pachl counts the number of possible ways
* 10.01.2014 Walter Pachl counts the number of possible ways
* 12.01.2014 corrected date and output
* 12.01.2014 corrected date and output
Line 8,766: Line 8,766:


=={{header|Ring}}==
=={{header|Ring}}==
<syntaxhighlight lang=ring>Blocks = [ :BO, :XK, :DQ, :CP, :NA, :GT, :RE, :TG, :QD, :FS, :JW, :HU, :VI, :AN, :OB, :ER, :FS, :LY, :PC, :ZM ]
<syntaxhighlight lang="ring">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 ]
Words = [ :A, :BARK, :BOOK, :TREAT, :COMMON, :SQUAD, :CONFUSE ]


Line 8,810: Line 8,810:
=={{header|Ruby}}==
=={{header|Ruby}}==
This one uses a case insensitive regular expression. The 'sub!' method substitutes the first substring it finds and returns nil if nothing is found.
This one uses a case insensitive regular expression. The 'sub!' method substitutes the first substring it finds and returns nil if nothing is found.
<syntaxhighlight lang=ruby>words = %w(A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE) << ""
<syntaxhighlight lang="ruby">words = %w(A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE) << ""


words.each do |word|
words.each do |word|
Line 8,831: Line 8,831:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<syntaxhighlight lang=unbasic>blocks$ = "BO,XK,DQ,CP,NA,GT,RE,TG,QD,FS,JW,HU,VI,AN,OB,ER,FS,LY,PC,ZM"
<syntaxhighlight lang="unbasic">blocks$ = "BO,XK,DQ,CP,NA,GT,RE,TG,QD,FS,JW,HU,VI,AN,OB,ER,FS,LY,PC,ZM"
makeWord$ = "A,BARK,BOOK,TREAT,COMMON,SQUAD,Confuse"
makeWord$ = "A,BARK,BOOK,TREAT,COMMON,SQUAD,Confuse"
b = int((len(blocks$) /3) + 1)
b = int((len(blocks$) /3) + 1)
Line 8,866: Line 8,866:
=={{header|Rust}}==
=={{header|Rust}}==
This implementation uses a backtracking search.
This implementation uses a backtracking search.
<syntaxhighlight lang=rust>use std::iter::repeat;
<syntaxhighlight lang="rust">use std::iter::repeat;


fn rec_can_make_word(index: usize, word: &str, blocks: &[&str], used: &mut[bool]) -> bool {
fn rec_can_make_word(index: usize, word: &str, blocks: &[&str], used: &mut[bool]) -> bool {
Line 8,908: Line 8,908:


=={{header|Scala}}==
=={{header|Scala}}==
{{libheader|Scala}}<syntaxhighlight lang=Scala>object AbcBlocks extends App {
{{libheader|Scala}}<syntaxhighlight lang="scala">object AbcBlocks extends App {


protected class Block(face1: Char, face2: Char) {
protected class Block(face1: Char, face2: Char) {
Line 8,961: Line 8,961:
=={{header|Scheme}}==
=={{header|Scheme}}==
In R5RS:
In R5RS:
<syntaxhighlight lang=scheme>(define *blocks*
<syntaxhighlight lang="scheme">(define *blocks*
'((#\B #\O) (#\X #\K) (#\D #\Q) (#\C #\P) (#\N #\A)
'((#\B #\O) (#\X #\K) (#\D #\Q) (#\C #\P) (#\N #\A)
(#\G #\T) (#\R #\E) (#\T #\G) (#\Q #\D) (#\F #\S)
(#\G #\T) (#\R #\E) (#\T #\G) (#\Q #\D) (#\F #\S)
Line 9,014: Line 9,014:


=={{header|Seed7}}==
=={{header|Seed7}}==
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func boolean: canMakeWords (in array string: blocks, in string: word) is func
const func boolean: canMakeWords (in array string: blocks, in string: word) is func
Line 9,062: Line 9,062:


=={{header|SenseTalk}}==
=={{header|SenseTalk}}==
<syntaxhighlight lang=sensetalk>function CanMakeWord word
<syntaxhighlight lang="sensetalk">function CanMakeWord word


put [
put [
Line 9,104: Line 9,104:
end CanMakeWord</syntaxhighlight>
end CanMakeWord</syntaxhighlight>


<syntaxhighlight lang=sensetalk>repeat with each item word in [
<syntaxhighlight lang="sensetalk">repeat with each item word in [
"A",
"A",
"BARK",
"BARK",
Line 9,118: Line 9,118:
=={{header|SequenceL}}==
=={{header|SequenceL}}==
===Recursive Search Version===
===Recursive Search Version===
<syntaxhighlight lang=sequencel>import <Utilities/Conversion.sl>;
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
import <Utilities/Sequence.sl>;


Line 9,161: Line 9,161:


===RegEx Version ===
===RegEx Version ===
<syntaxhighlight lang=sequencel>import <Utilities/Conversion.sl>;
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
import <Utilities/Sequence.sl>;
import <RegEx/RegEx.sl>;
import <RegEx/RegEx.sl>;
Line 9,195: Line 9,195:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Perl}}
{{trans|Perl}}
<syntaxhighlight lang=ruby>func can_make_word(word, blocks) {
<syntaxhighlight lang="ruby">func can_make_word(word, blocks) {


blocks.map! { |b| b.uc.chars.sort.join }.freq!
blocks.map! { |b| b.uc.chars.sort.join }.freq!
Line 9,214: Line 9,214:


Tests:
Tests:
<syntaxhighlight lang=ruby>var b1 = %w(BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM)
<syntaxhighlight lang="ruby">var b1 = %w(BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM)
var b2 = %w(US TZ AO QA)
var b2 = %w(US TZ AO QA)


Line 9,247: Line 9,247:


=={{header|Simula}}==
=={{header|Simula}}==
<syntaxhighlight lang=simula>COMMENT ABC PROBLEM;
<syntaxhighlight lang="simula">COMMENT ABC PROBLEM;
BEGIN
BEGIN


Line 9,356: Line 9,356:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==
Recursive solution. Tested in Pharo.
Recursive solution. Tested in Pharo.
<syntaxhighlight lang=smalltalk>
<syntaxhighlight lang="smalltalk">
ABCPuzzle>>test
ABCPuzzle>>test
#('A' 'BARK' 'BOOK' 'TreaT' 'COMMON' 'sQUAD' 'CONFuSE') do: [ :each |
#('A' 'BARK' 'BOOK' 'TreaT' 'COMMON' 'sQUAD' 'CONFuSE') do: [ :each |
Line 9,394: Line 9,394:
=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang=SNOBOL4>
<syntaxhighlight lang="snobol4">
* Program: abc.sbl,
* Program: abc.sbl,
* To run: sbl -r abc.sbl
* To run: sbl -r abc.sbl
Line 9,483: Line 9,483:
=={{header|SPAD}}==
=={{header|SPAD}}==
{{works with|FriCAS, OpenAxiom, Axiom}}
{{works with|FriCAS, OpenAxiom, Axiom}}
<syntaxhighlight lang=SPAD>
<syntaxhighlight lang="spad">
blocks:List Tuple Symbol:= _
blocks:List Tuple Symbol:= _
[(B,O),(X,K),(D,Q),(C,P),(N,A),(G,T),(R,E),(T,G),(Q,D),(F,S), _
[(B,O),(X,K),(D,Q),(C,P),(N,A),(G,T),(R,E),(T,G),(Q,D),(F,S), _
Line 9,522: Line 9,522:


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<syntaxhighlight lang=OCaML>
<syntaxhighlight lang="ocaml">
val BLOCKS = [(#"B",#"O"), (#"X",#"K"), (#"D",#"Q"), (#"C",#"P"), (#"N",#"A"), (#"G",#"T"),
val BLOCKS = [(#"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"),
(#"R",#"E"), (#"T",#"G"), (#"Q",#"D"), (#"F",#"S"), (#"J",#"W"), (#"H",#"U"), (#"V",#"I"),
Line 9,563: Line 9,563:


=={{header|Swift}}==
=={{header|Swift}}==
<syntaxhighlight lang=Swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func Blockable(str: String) -> Bool {
func Blockable(str: String) -> Bool {
Line 9,608: Line 9,608:


{{works with|Swift|3.0.2}}
{{works with|Swift|3.0.2}}
<syntaxhighlight lang=Swift>import Swift
<syntaxhighlight lang="swift">import Swift


func canMake(word: String) -> Bool {
func canMake(word: String) -> Bool {
Line 9,643: Line 9,643:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
{{works with|Tcl|8.6}}
<syntaxhighlight lang=tcl>package require Tcl 8.6
<syntaxhighlight lang="tcl">package require Tcl 8.6


proc abc {word {blocks {BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM}}} {
proc abc {word {blocks {BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM}}} {
Line 9,677: Line 9,677:


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<syntaxhighlight lang=tuscript>set words = "A'BARK'BOOK'TREAT'COMMON'SQUAD'CONFUSE"
<syntaxhighlight lang="tuscript">set words = "A'BARK'BOOK'TREAT'COMMON'SQUAD'CONFUSE"
set result = *
set result = *
loop word = words
loop word = words
Line 9,706: Line 9,706:
=={{header|TXR}}==
=={{header|TXR}}==


<syntaxhighlight lang=txr>@(do
<syntaxhighlight lang="txr">@(do
(defvar blocks '((B O) (X K) (D Q) (C P) (N A) (G T) (R E) (T G)
(defvar blocks '((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)
(Q D) (F S) (J W) (H U) (V I) (A N) (O B) (E R)
Line 9,785: Line 9,785:
This is example is a slight modification of the C and C++ examples. To avoid warning "<bold>warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]</bold> the strings added to char were individually prefixed with (char*). Swap is used instead of SWAP. Return 0 was not not needed.
This is example is a slight modification of the C and C++ examples. To avoid warning "<bold>warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wwritable-strings]</bold> the strings added to char were individually prefixed with (char*). Swap is used instead of SWAP. Return 0 was not not needed.


<syntaxhighlight lang=Cpp>
<syntaxhighlight lang="cpp">
#include <Core/Core.h>
#include <Core/Core.h>
#include <stdio.h>
#include <stdio.h>
Line 9,915: Line 9,915:
{{works with|bash}}
{{works with|bash}}


<syntaxhighlight lang=bash>can_build_word() {
<syntaxhighlight lang="bash">can_build_word() {
if [[ $1 ]]; then
if [[ $1 ]]; then
can_build_word_rec "$1" BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM
can_build_word_rec "$1" BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM
Line 9,969: Line 9,969:
'''String-based solution'''
'''String-based solution'''


<syntaxhighlight lang=UTFool>
<syntaxhighlight lang="utfool">
···
···
http://rosettacode.org/wiki/ABC_Problem
http://rosettacode.org/wiki/ABC_Problem
Line 10,001: Line 10,001:
'''Collection-based solution'''
'''Collection-based solution'''


<syntaxhighlight lang=UTFool>
<syntaxhighlight lang="utfool">
···
···
http://rosettacode.org/wiki/ABC_Problem
http://rosettacode.org/wiki/ABC_Problem
Line 10,040: Line 10,040:
=={{header|VBA}}==
=={{header|VBA}}==


<syntaxhighlight lang=vb>
<syntaxhighlight lang="vb">
Option Explicit
Option Explicit


Line 10,089: Line 10,089:
{{trans|Go}}
{{trans|Go}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var r // recursive
var r // recursive
Line 10,131: Line 10,131:


=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>string 0;
<syntaxhighlight lang="xpl0">string 0;


char Side1, Side2;
char Side1, Side2;
Line 10,173: Line 10,173:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic>letters$ = "BO,XK,DQ,CP,NA,GT,RE,TG,QD,FS,JW,HU,VI,AN,OB,ER,FS,LY,PC,ZM"
<syntaxhighlight lang="yabasic">letters$ = "BO,XK,DQ,CP,NA,GT,RE,TG,QD,FS,JW,HU,VI,AN,OB,ER,FS,LY,PC,ZM"


sub canMake(letters$, word$)
sub canMake(letters$, word$)
Line 10,204: Line 10,204:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|C}}
{{trans|C}}
<syntaxhighlight lang=zkl>var blocks=T("BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
<syntaxhighlight lang="zkl">var blocks=T("BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM", );
"JW", "HU", "VI", "AN", "OB", "ER", "FS", "LY", "PC", "ZM", );


Line 10,237: Line 10,237:


=={{header|zonnon}}==
=={{header|zonnon}}==
<syntaxhighlight lang=zonnon>
<syntaxhighlight lang="zonnon">
module Main;
module Main;
type
type
Line 10,337: Line 10,337:


=={{header|ZX Spectrum Basic}}==
=={{header|ZX Spectrum Basic}}==
<syntaxhighlight lang=zxbasic>10 LET b$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
<syntaxhighlight lang="zxbasic">10 LET b$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
20 READ p
20 READ p
30 FOR c=1 TO p
30 FOR c=1 TO p