ABC problem: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 44:
 
;Example:
<syntaxhighlight lang="python"> >>> can_make_word("A")
True
>>> can_make_word("BARK")
Line 64:
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F can_make_word(word)
I word == ‘’
R 0B
Line 83:
=={{header|360 Assembly}}==
The program uses one ASSIST macro (XPRNT) to keep the code as short as possible.
<syntaxhighlight lang="360asm">* ABC Problem 21/07/2016
ABC CSECT
USING ABC,R13 base register
Line 171:
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="8080asm"> org 100h
jmp test
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 263:
{{trans|8080 Assembly}}
 
<syntaxhighlight lang="asm"> cpu 8086
bits 16
org 100h
Line 338:
 
=={{header|8th}}==
<syntaxhighlight lang="360asm">
\ ========================================================================================
\ You are given a collection of ABC blocks
Line 540:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program problemABC64.s */
Line 747:
</pre>
=={{header|ABAP}}==
<syntaxhighlight lang=ABAP"abap">
REPORT z_rosetta_abc.
 
Line 833:
 
=={{header|Action!}}==
<syntaxhighlight lang=Action"action!">DEFINE COUNT="20"
CHAR ARRAY sideA="BXDCNGRTQFJHVAOEFLPZ"
CHAR ARRAY sideB="OKQPATEGDSWUINBRSYCM"
Line 906:
Using #HASH-OFF
</pre>
<syntaxhighlight lang="acurity architect">
FUNCTION bCAN_MAKE_WORD(zWord: STRING): BOOLEAN
VAR sBlockCount: SHORT
Line 950:
</pre>
 
<syntaxhighlight lang="ada">with Ada.Characters.Handling;
use Ada.Characters.Handling;
 
Line 1,043:
=={{header|ALGOL 68}}==
{{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 #
 
# construct the list of blocks #
Line 1,136:
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">% determine whether we can spell words with a set of blocks %
begin
% Returns true if we can spell the word using the blocks, %
Line 1,229:
 
=={{header|Apex}}==
<syntaxhighlight lang=Java"java">static Boolean canMakeWord(List<String> src_blocks, String word) {
if (String.isEmpty(word)) {
return true;
Line 1,286:
=={{header|APL}}==
{{works with|Dyalog APL|16.0}}
<syntaxhighlight lang=APL"apl">abc←{{0=⍴⍵:1 ⋄ 0=⍴h←⊃⍵:0 ⋄ ∇(t←1↓⍵)~¨⊃h:1 ⋄ ∇(⊂1↓h),t}⍸¨↓⍵∘.∊⍺}</syntaxhighlight>
{{out}}
<pre> )COPY dfns ucase
Line 1,296:
=={{header|AppleScript}}==
===Imperative===
<syntaxhighlight lang=AppleScript"applescript">set blocks to {"bo", "xk", "dq", "cp", "na", "gt", "re", "tg", "qd", "fs", ¬
"jw", "hu", "vi", "an", "ob", "er", "fs", "ly", "pc", "zm"}
 
Line 1,326:
 
===Functional===
<syntaxhighlight lang=AppleScript"applescript">use AppleScript version "2.4"
use framework "Foundation"
 
Line 1,534:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI */
/* program problemABC.s */
Line 1,737:
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">blocks: map [
[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]
Line 1,773:
 
=={{header|Astro}}==
<syntaxhighlight lang="python">fun abc(s, ls):
if ls.isempty:
return true
Line 1,789:
 
'''Function'''
<syntaxhighlight lang="autohotkey">isWordPossible(blocks, word){
o := {}
loop, parse, blocks, `n, `r
Line 1,815:
 
'''Test Input''' (as per question)
<syntaxhighlight lang="autohotkey">blocks := "
(
BO
Line 2,012:
 
=={{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"
 
DATA "A", "BARK", "BOOK", "TREAT", "Common", "Squad", "Confuse"
Line 2,049:
=={{header|BASIC}}==
Works with:VB-DOS, QB64, QBasic, QuickBASIC
<syntaxhighlight lang="qbasic">
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ABC_Problem '
Line 2,179:
==={{header|Commodore BASIC}}===
{{trans|Sinclair ZX-81 BASIC}}
<syntaxhighlight lang="basic">10 W$ = "A" : GOSUB 100
20 W$ = "BARK" : GOSUB 100
30 W$ = "BOOK" : GOSUB 100
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:
 
<syntaxhighlight lang="basic">100 REM RECURSIVE SOLUTION
110 MS=100:REM MAX STACK DEPTH
120 DIM BL$(MS):REM BLOCKS LEFT
Line 2,277:
==={{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.
<syntaxhighlight lang="basic"> 10 LET B$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
20 INPUT W$
30 FOR I=1 TO LEN W$
Line 2,318:
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">
@echo off
::abc.bat
Line 2,385:
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> BLOCKS$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
PROCcan_make_word("A")
PROCcan_make_word("BARK")
Line 2,419:
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let canMakeWord(word) = valof
Line 2,466:
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">ABC ← {
Matches ← ⊑⊸(⊑∘∊¨)˜ /⊣ # blocks matching current letter
Others ← <˘∘⍉∘(»⊸≥∨`)∘(≡⌜)/¨<∘⊣ # blocks without current matches
Line 2,494:
 
=={{header|Bracmat}}==
<syntaxhighlight lang="bracmat">(
( can-make-word
= ABC blocks
Line 2,553:
=={{header|C}}==
Recursive solution. Empty string returns true.
<syntaxhighlight lang="c">#include <stdio.h>
#include <ctype.h>
 
Line 2,610:
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).
<syntaxhighlight lang="csharp">using System;
using System.IO;
// Needed for the method.
Line 2,652:
</pre>
'''Unoptimized'''
<syntaxhighlight lang="csharp">using System.Collections.Generic;
using System.Linq;
 
Line 2,748:
{{Works with|C++11}}
Build with:
<syntaxhighlight lang="sh">g++-4.7 -Wall -std=c++0x abc.cpp</syntaxhighlight>
<syntaxhighlight lang="cpp">#include <iostream>
#include <vector>
#include <string>
Line 2,799:
<b>module.ceylon</b>
 
<syntaxhighlight lang="ceylon">
module rosetta.abc "1.0.0" {}
</syntaxhighlight>
Line 2,805:
<b>run.ceylon</b>
 
<syntaxhighlight lang="ceylon">
shared void run() {
printAndCanMakeWord("A", blocks);
Line 2,895:
=={{header|Clojure}}==
A translation of the Haskell solution.
<syntaxhighlight lang="clojure">
(def blocks
(-> "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:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">ucase = proc (s: string) returns (string)
rslt: array[char] := array[char]$predict(1,string$size(s))
for c: char in string$chars(s) do
Line 2,982:
 
=={{header|CoffeeScript}}==
<syntaxhighlight lang=CoffeeScript"coffeescript">blockList = [ 'BO', 'XK', 'DQ', 'CP', 'NA', 'GT', 'RE', 'TG', 'QD', 'FS', 'JW', 'HU', 'VI', 'AN', 'OB', 'ER', 'FS', 'LY', 'PC', 'ZM' ]
 
canMakeWord = (word="") ->
Line 3,012:
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 FUNC can'make'word#(word$) CLOSED
0020 blocks$:=" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
0030 FOR i#:=1 TO LEN(word$) DO
Line 3,041:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defun word-possible-p (word blocks)
(cond
Line 3,077:
=={{header|Component Pascal}}==
{{Works with|BlackBox Component Builder}}
<syntaxhighlight lang="oberon2">
MODULE ABCProblem;
IMPORT
Line 3,176:
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
 
Line 3,235:
{{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.
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.string;
 
bool canMakeWord(in string word, in string[] blocks) pure /*nothrow*/ @safe {
Line 3,269:
===@nogc Version===
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;
 
bool canMakeWord(in string word, in string[] blocks) nothrow @nogc
Line 3,312:
This version is able to find the solution for the word "abba" given the blocks AB AB AC AC.
{{trans|C}}
<syntaxhighlight lang="d">import std.stdio, std.ascii, std.algorithm, std.array;
 
alias Block = char[2];
Line 3,363:
===Alternative Recursive Version===
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;
 
alias Block = char[2];
Line 3,409:
=={{header|Delphi}}==
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"delphi">program ABC;
{$APPTYPE CONSOLE}
 
Line 3,486:
 
=={{header|Draco}}==
<syntaxhighlight lang="draco">\util.g
 
proc nonrec ucase(char c) char:
Line 3,552:
{{trans|Swift}}
 
<syntaxhighlight lang="dyalect">func blockable(str) {
var blocks = [
"BO", "XK", "DQ", "CP", "NA", "GT", "RE", "TG", "QD", "FS",
Line 3,590:
 
=={{header|EchoLisp}}==
<syntaxhighlight lang="scheme">
(lib 'list) ;; list-delete
 
Line 3,626:
=={{header|Ela}}==
{{trans|Haskell}}
<syntaxhighlight lang="ela">open list monad io char
 
:::IO
Line 3,656:
=={{header|Elena}}==
ELENA 5.0
<syntaxhighlight lang="elena">import system'routines;
import system'collections;
import extensions;
Line 3,716:
{{trans|Erlang}}
{{works with|Elixir|1.3}}
<syntaxhighlight lang="elixir">defmodule ABC do
def can_make_word(word, avail) do
can_make_word(String.upcase(word) |> to_charlist, avail, [])
Line 3,745:
 
=={{header|Erlang}}==
<syntaxhighlight lang="erlang">-module(abc).
-export([can_make_word/1, can_make_word/2, blocks/0]).
 
Line 3,774:
 
=={{header|ERRE}}==
<syntaxhighlight lang=ERRE"erre">
PROGRAM BLOCKS
 
Line 3,807:
=={{header|Euphoria}}==
implemented using OpenEuphoria
<syntaxhighlight lang=Euphoria"euphoria">
include std/text.e
 
Line 3,859:
=={{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>
<syntaxhighlight lang="fsharp">let rec spell_word_with blocks w =
let rec look_for_right_candidate candidates noCandidates c rest =
match candidates with
Line 3,906:
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: assocs combinators.short-circuit formatting grouping io
kernel math math.statistics qw sequences sets unicode ;
IN: rosetta-code.abc-problem
Line 3,972:
=={{header|FBSL}}==
This approach uses a string, blanking out the pair previously found. Probably faster than array manipulation.
<syntaxhighlight lang="qbasic">
#APPTYPE CONSOLE
SUB MAIN()
Line 4,037:
{{works with|gforth|0.7.3}}
 
<syntaxhighlight lang="forth">: blockslist s" BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM" ;
variable blocks
: allotblocks ( -- ) here blockslist dup allot here over - swap move blocks ! ;
Line 4,081:
=={{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!
<syntaxhighlight lang=Fortran"fortran">!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Thu Jun 5 01:52:03
!
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.
<syntaxhighlight lang=Fortran"fortran">
MODULE PLAYPEN !Messes with a set of alphabet blocks.
INTEGER MSG !Output unit number.
Line 4,461:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 28-01-2019
' compile with: fbc -s console
 
Line 4,515:
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
Line 4,571:
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ae860292d4588b3627d77c85bcc634ee Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
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"]
Line 4,611:
 
=={{header|Go}}==
<syntaxhighlight lang="go">package main
 
import (
Line 4,663:
=={{header|Groovy}}==
Solution:
<syntaxhighlight lang="groovy">class ABCSolver {
def blocks
 
Line 4,677:
 
Test:
<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"])
 
Line 4,696:
=={{header|Harbour}}==
Harbour Project implements a cross-platform Clipper/xBase compiler.
<syntaxhighlight lang="visualfoxpro">PROCEDURE Main()
 
LOCAL cStr
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.
<syntaxhighlight lang="haskell">import Data.List (delete)
import Data.Char (toUpper)
 
Line 4,771:
Or, in terms of the bind operator:
 
<syntaxhighlight lang="haskell">import Data.Char (toUpper)
import Data.List (delete)
 
Line 4,823:
 
Works in both languages:
<syntaxhighlight lang="unicon">procedure main(A)
blocks := ["bo","xk","dq","cp","na","gt","re","tg","qd","fs",
"jw","hu","vi","an","ob","er","fs","ly","pc","zm",&null]
Line 4,864:
=={{header|J}}==
'''Solution:'''
<syntaxhighlight lang="j">reduce=: verb define
'rows cols'=. i.&.> $y
for_c. cols do.
Line 4,876:
abc=: *./@(+./)@reduce@(e."1~ ,)&toupper :: 0:</syntaxhighlight>
'''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 '
ExampleWords=: <;._2 'A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE '
 
Line 4,892:
 
'''Tacit version'''
<syntaxhighlight lang="j">delElem=: {~<@<@<
uppc=:(-32*96&<*.123&>)&.(3&u:)
reduc=: ] delElem 1 i.~e."0 1
Line 4,919:
Another approach might be:
 
<syntaxhighlight lang=J"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 '
 
Line 4,932:
Example use:
 
<syntaxhighlight lang=J"j"> Blocks canform 0{::ExampleWords
1
Blocks canform 1{::ExampleWords
Line 4,955:
For example:
 
<syntaxhighlight lang=J"j"> Blocks canform 0{::ExampleWords
1
word
Line 4,975:
{{trans|C}}
{{works with|Java|1.6+}}
<syntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Line 5,025:
====Imperative====
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";
 
function CheckWord(blocks, word) {
Line 5,076:
 
====Functional====
<syntaxhighlight lang=JavaScript"javascript">(function (strWords) {
 
var strBlocks =
Line 5,125:
})('A bark BooK TReAT COMMON squAD conFUSE');</syntaxhighlight>
{{Out}}
<syntaxhighlight lang=JavaScript"javascript">A -> NA
bark -> BO NA RE XK
BooK: [no solution]
Line 5,135:
===ES6===
====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";
let blocks = characters.split(" ").map(pair => pair.split(""));
Line 5,181:
====Functional====
{{Trans|Haskell}}
<syntaxhighlight lang=JavaScript"javascript">(() => {
"use strict";
 
Line 5,259:
 
=={{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">
# when_index(cond;ary) returns the index of the first element in ary
# that satisfies cond; it uses a helper function that takes advantage
Line 5,289:
end
end;</syntaxhighlight>
Task:<syntaxhighlight lang="jq">def task:
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"] as $blocks
Line 5,305:
=={{header|Jsish}}==
Based on Javascript ES5 imperative solution.
<syntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* 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";
Line 5,358:
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">using Printf
 
function abc(str::AbstractString, list)
Line 5,389:
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">object ABC_block_checker {
fun run() {
println("\"\": " + blocks.canMakeWord(""))
Line 5,440:
=={{header|Liberty BASIC}}==
===Recursive solution===
<syntaxhighlight lang="lb">
print "Rosetta Code - ABC problem (recursive solution)"
print
Line 5,505:
</pre>
===Procedural solution===
<syntaxhighlight lang="lb">
print "Rosetta Code - ABC problem (procedural solution)"
print
Line 5,662:
 
=={{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]
[J W] [H U] [V I] [A N] [O B] [E R] [F S] [L Y] [P C] [Z M]]
 
Line 5,694:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">blocks = {
{"B","O"}; {"X","K"}; {"D","Q"}; {"C","P"};
{"N","A"}; {"G","T"}; {"R","E"}; {"T","G"};
Line 5,740:
 
 
<syntaxhighlight lang=M2000"m2000 Interpreterinterpreter">
Module ABC {
can_make_word("A")
Line 5,775:
 
=={{header|Maple}}==
<syntaxhighlight 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"],
Line 5,810:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">
blocks=Partition[Characters[ToLowerCase["BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"]],2];
ClearAll[DoStep,ABCBlockQ]
Line 5,843:
 
=={{header|MATLAB}} / {{header|Octave}}==
<syntaxhighlight lang=MATLAB"matlab">function testABC
combos = ['BO' ; 'XK' ; 'DQ' ; 'CP' ; 'NA' ; 'GT' ; 'RE' ; 'TG' ; 'QD' ; ...
'FS' ; 'JW' ; 'HU' ; 'VI' ; 'AN' ; 'OB' ; 'ER' ; 'FS' ; 'LY' ; ...
Line 5,884:
Recursively checks if the word is possible if a block is removed from the array.
 
<syntaxhighlight lang=MAXScript"maxscript">
-- This is the blocks array
global GlobalBlocks = #("BO","XK","DQ","CP","NA", \
Line 5,986:
 
'''Output:'''
<syntaxhighlight lang=MAXScript"maxscript">
iswordpossible "a"
true
Line 6,005:
 
=== Non-recursive ===
<syntaxhighlight lang=MAXScript"maxscript">
fn isWordPossible2 word =
(
Line 6,042:
Then:
 
<syntaxhighlight lang=MAXScript"maxscript">
iswordpossible "water"
true
Line 6,052:
 
=={{header|Mercury}}==
<syntaxhighlight lang=Mercury"mercury">:- module abc.
:- interface.
:- import_module io.
Line 6,090:
 
=={{header|MiniScript}}==
<syntaxhighlight lang=MiniScript"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)
Line 6,120:
=={{header|Nim}}==
{{works with|Nim|0.20.0}}
<syntaxhighlight lang="nim">import std / strutils
 
func canMakeWord(blocks: seq[string]; word: string): bool =
Line 6,164:
=={{header|Oberon-2}}==
Works with oo2c Version 2
<syntaxhighlight lang="oberon2">
MODULE ABCBlocks;
IMPORT
Line 6,262:
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class Abc {
function : Main(args : String[]) ~ Nil {
blocks := ["BO", "XK", "DQ", "CP", "NA",
Line 6,320:
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let blocks = [
('B', 'O'); ('X', 'K'); ('D', 'Q'); ('C', 'P');
('N', 'A'); ('G', 'T'); ('R', 'E'); ('T', 'G');
Line 6,375:
=={{header|Oforth}}==
 
<syntaxhighlight lang=Oforth"oforth">import: mapping
 
["BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS","JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"]
Line 6,398:
=={{header|OpenEdge/Progress}}==
 
<syntaxhighlight lang=Progress"progress (Openedgeopenedge ABLabl)">FUNCTION canMakeWord RETURNS LOGICAL (INPUT pWord AS CHARACTER) FORWARD.
 
/* List of blocks */
Line 6,514:
 
=={{header|Order}}==
<syntaxhighlight lang=Order"order">#include <order/interpreter.h>
#include <order/lib.h>
 
Line 6,684:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">BLOCKS = "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM";
WORDS = ["A","Bark","BOOK","Treat","COMMON","SQUAD","conFUSE"];
 
Line 6,715:
{{works with|Free Pascal|2.6.2}}
 
<syntaxhighlight lang=Pascal"pascal">
#!/usr/bin/instantfpc
//program ABCProblem;
Line 6,805:
=={{header|Perl}}==
Recursive solution that can handle characters appearing on different blocks:
<syntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 6,832:
}</syntaxhighlight>
<p>Testing:
<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);
Line 6,847:
</syntaxhighlight>
===Regex based alternate===
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/ABC_Problem
Line 6,877:
=={{header|Phix}}==
Recursive solution which also solves the extra problems on the discussion page.
<!--<syntaxhighlight lang=Phix"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>
Line 6,930:
=={{header|PHP}}==
 
<syntaxhighlight lang=PHP"php">
<?php
$words = array("A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "Confuse");
Line 6,973:
=={{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.
<syntaxhighlight lang=Picat"picat">go =>
test_it(check_word),
test_it(check_word2),
Line 7,156:
=={{header|PicoLisp}}==
Mapping and recursion.
<syntaxhighlight lang=PicoLisp"picolisp">(setq *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)
Line 7,192:
=={{header|PL/I}}==
===version 1===
<syntaxhighlight lang="pli">ABC: procedure options (main); /* 12 January 2014 */
 
declare word character (20) varying, blocks character (200) varying initial
Line 7,229:
 
===version 2===
<syntaxhighlight lang="pli">*process source attributes xref or(!) options nest;
abc: Proc Options(main);
/* REXX --------------------------------------------------------------
Line 7,361:
 
=={{header|PL/M}}==
<syntaxhighlight lang="plm">100H:
 
/* ABC PROBLEM ON $-TERMINATED STRING */
Line 7,433:
Works with PowerBASIC 6 Console Compiler
 
<syntaxhighlight lang=PowerBASIC"powerbasic">#COMPILE EXE
#DIM ALL
'
Line 7,602:
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell"><#
.Synopsis
ABC Problem
Line 7,784:
Works with SWI-Prolog 6.5.3
 
<syntaxhighlight lang=Prolog"prolog">abc_problem :-
maplist(abc_problem, ['', 'A', bark, bOOk, treAT, 'COmmon', sQuaD, 'CONFUSE']).
 
Line 7,827:
{{works with|SWI Prolog 7}}
 
<syntaxhighlight lang=Prolog"prolog">:- use_module([ library(chr),
abathslib(protelog/composer) ]).
 
Line 7,849:
Demonstration:
 
<syntaxhighlight lang=Prolog"prolog">?- can_build_word("A").
true.
?- can_build_word("BARK").
Line 7,866:
=={{header|PureBasic}}==
===PureBasic: Iterative===
<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 "
 
Line 7,900:
 
===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 "
 
Macro test(t)
Line 7,932:
 
===Python: Iterative, with tests===
<syntaxhighlight lang="python">
'''
Note that this code is broken, e.g., it won't work when
Line 8,007:
 
===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()
 
def _abc(word, blocks):
Line 8,044:
 
===Python: Recursive, telling how===
<syntaxhighlight lang="python">def mkword(w, b):
if not w: return []
 
Line 8,076:
=={{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.
<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
 
Line 8,084:
any(1_s).z.s/:b(til count b)except/:where found] }</syntaxhighlight>
{{out}}
<syntaxhighlight lang="q">q)WORDS cmw\:BLOCKS
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.
Line 8,093:
 
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
cmwi:{(`$x), `false`true cmw . upper each(x;y) }</syntaxhighlight>
{{out}}
<syntaxhighlight lang="q">q)Words cmwi\:BLOCKS
A true
bark true
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.
 
<syntaxhighlight lang=Quackery"quackery">[ $ "BOXKDQCPNAGTRETGQDFS"
$ "JWHUVIANOBERFSLYPCZM"
join ] constant is blocks ( --> $ )
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.
 
<syntaxhighlight lang=Quackery"quackery">[ ' [ 0 ] swap
witheach
[ over -1 peek
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.
 
<syntaxhighlight lang=R"r">blocks <- rbind(c("B","O"),
c("X","K"),
c("D","Q"),
Line 8,289:
===Without recursion===
Second version without recursion and giving every unique combination of blocks for each word:
<syntaxhighlight lang=R"r">canMakeNoRecursion <- function(x) {
x <- toupper(x)
charList <- strsplit(x, character(0))
Line 8,393:
So '(can-make-word? "")' is true for me.
 
<syntaxhighlight lang="racket">#lang racket
(define block-strings
(list "BO" "XK" "DQ" "CP" "NA"
Line 8,451:
{{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.
<syntaxhighlight lang=perl6"raku" line>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;
Line 8,482:
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">dim Blocks as string
dim InWord as string
 
Line 8,520:
 
=={{header|Red}}==
<syntaxhighlight lang=Red"red">Red []
test: func [ s][
p: copy "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
Line 8,550:
=={{header|REXX}}==
===version 1===
<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*/
blocks= 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'
Line 8,583:
 
===version 2===
<syntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 10.01.2014 Walter Pachl counts the number of possible ways
* 12.01.2014 corrected date and output
Line 8,766:
 
=={{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 ]
Words = [ :A, :BARK, :BOOK, :TREAT, :COMMON, :SQUAD, :CONFUSE ]
 
Line 8,810:
=={{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.
<syntaxhighlight lang="ruby">words = %w(A BaRK BOoK tREaT COmMOn SqUAD CoNfuSE) << ""
 
words.each do |word|
Line 8,831:
 
=={{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"
makeWord$ = "A,BARK,BOOK,TREAT,COMMON,SQUAD,Confuse"
b = int((len(blocks$) /3) + 1)
Line 8,866:
=={{header|Rust}}==
This implementation uses a backtracking search.
<syntaxhighlight lang="rust">use std::iter::repeat;
 
fn rec_can_make_word(index: usize, word: &str, blocks: &[&str], used: &mut[bool]) -> bool {
Line 8,908:
 
=={{header|Scala}}==
{{libheader|Scala}}<syntaxhighlight lang=Scala"scala">object AbcBlocks extends App {
 
protected class Block(face1: Char, face2: Char) {
Line 8,961:
=={{header|Scheme}}==
In R5RS:
<syntaxhighlight lang="scheme">(define *blocks*
'((#\B #\O) (#\X #\K) (#\D #\Q) (#\C #\P) (#\N #\A)
(#\G #\T) (#\R #\E) (#\T #\G) (#\Q #\D) (#\F #\S)
Line 9,014:
 
=={{header|Seed7}}==
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func boolean: canMakeWords (in array string: blocks, in string: word) is func
Line 9,062:
 
=={{header|SenseTalk}}==
<syntaxhighlight lang="sensetalk">function CanMakeWord word
 
put [
Line 9,104:
end CanMakeWord</syntaxhighlight>
 
<syntaxhighlight lang="sensetalk">repeat with each item word in [
"A",
"BARK",
Line 9,118:
=={{header|SequenceL}}==
===Recursive Search Version===
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
 
Line 9,161:
 
===RegEx Version ===
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>;
import <Utilities/Sequence.sl>;
import <RegEx/RegEx.sl>;
Line 9,195:
=={{header|Sidef}}==
{{trans|Perl}}
<syntaxhighlight lang="ruby">func can_make_word(word, blocks) {
 
blocks.map! { |b| b.uc.chars.sort.join }.freq!
Line 9,214:
 
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)
var b2 = %w(US TZ AO QA)
 
Line 9,247:
 
=={{header|Simula}}==
<syntaxhighlight lang="simula">COMMENT ABC PROBLEM;
BEGIN
 
Line 9,356:
=={{header|Smalltalk}}==
Recursive solution. Tested in Pharo.
<syntaxhighlight lang="smalltalk">
ABCPuzzle>>test
#('A' 'BARK' 'BOOK' 'TreaT' 'COMMON' 'sQUAD' 'CONFuSE') do: [ :each |
Line 9,394:
=={{header|SNOBOL4}}==
{{works with|SNOBOL4, SPITBOL for Linux}}
<syntaxhighlight lang=SNOBOL4"snobol4">
* Program: abc.sbl,
* To run: sbl -r abc.sbl
Line 9,483:
=={{header|SPAD}}==
{{works with|FriCAS, OpenAxiom, Axiom}}
<syntaxhighlight lang=SPAD"spad">
blocks:List Tuple Symbol:= _
[(B,O),(X,K),(D,Q),(C,P),(N,A),(G,T),(R,E),(T,G),(Q,D),(F,S), _
Line 9,522:
 
=={{header|Standard ML}}==
<syntaxhighlight lang=OCaML"ocaml">
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"),
Line 9,563:
 
=={{header|Swift}}==
<syntaxhighlight lang=Swift"swift">import Foundation
 
func Blockable(str: String) -> Bool {
Line 9,608:
 
{{works with|Swift|3.0.2}}
<syntaxhighlight lang=Swift"swift">import Swift
 
func canMake(word: String) -> Bool {
Line 9,643:
=={{header|Tcl}}==
{{works with|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}}} {
Line 9,677:
 
=={{header|TUSCRIPT}}==
<syntaxhighlight lang="tuscript">set words = "A'BARK'BOOK'TREAT'COMMON'SQUAD'CONFUSE"
set result = *
loop word = words
Line 9,706:
=={{header|TXR}}==
 
<syntaxhighlight lang="txr">@(do
(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)
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.
 
<syntaxhighlight lang=Cpp"cpp">
#include <Core/Core.h>
#include <stdio.h>
Line 9,915:
{{works with|bash}}
 
<syntaxhighlight lang="bash">can_build_word() {
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
Line 9,969:
'''String-based solution'''
 
<syntaxhighlight lang=UTFool"utfool">
···
http://rosettacode.org/wiki/ABC_Problem
Line 10,001:
'''Collection-based solution'''
 
<syntaxhighlight lang=UTFool"utfool">
···
http://rosettacode.org/wiki/ABC_Problem
Line 10,040:
=={{header|VBA}}==
 
<syntaxhighlight lang="vb">
Option Explicit
 
Line 10,089:
{{trans|Go}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var r // recursive
Line 10,131:
 
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0"xpl0">string 0;
 
char Side1, Side2;
Line 10,173:
 
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic"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$)
Line 10,204:
=={{header|zkl}}==
{{trans|C}}
<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", );
 
Line 10,237:
 
=={{header|zonnon}}==
<syntaxhighlight lang="zonnon">
module Main;
type
Line 10,337:
 
=={{header|ZX Spectrum Basic}}==
<syntaxhighlight lang="zxbasic">10 LET b$="BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM"
20 READ p
30 FOR c=1 TO p
10,327

edits