How to resolve the algorithm ABC problem step by step in the Cowgol programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm ABC problem step by step in the Cowgol programming language

Table of Contents

Problem Statement

You are given a collection of ABC blocks   (maybe like the ones you had when you were a kid).
There are twenty blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:

Write a function that takes a string (word) and determines whether the word can be spelled with the given collection of blocks.

The rules are simple:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm ABC problem step by step in the Cowgol programming language

Source code in the cowgol programming language

include "cowgol.coh";
include "strings.coh";

sub can_make_word(word: [uint8]): (r: uint8) is
    var blocks: [uint8] := "BOXKDQCPNAGTRETGQDFSJWHUVIANOBERFSLYPCZM";
    
    # Initialize blocks array
    var avl: uint8[41];
    CopyString(blocks, &avl[0]);
    
    r := 1;
    loop
        var letter := [word];
        word := @next word;
        if letter == 0 then break; end if;
        
        # find current letter in blocks
        var i: @indexof avl := 0;
        loop    
            var block := avl[i];
            if block == 0 then
                # no block, this word cannot be formed
                r := 0;
                return;
            elseif block == letter then
                # we found it, blank it out
                avl[i] := ' ';
                avl[i^1] := ' '; # and the other letter on the block too
                break;
            end if;
            i := i + 1;
        end loop;
    end loop;
end sub;

# test a list of words
var words: [uint8][] := {"A","BARK","BOOK","TREAT","COMMON","SQUAD","CONFUSE"};
var resp: [uint8][] := {": No\n", ": Yes\n"};
var i: @indexof words := 0;
while i < @sizeof words loop
    print(words[i]);
    print(resp[can_make_word(words[i])]);
    i := i + 1;
end loop;

  

You may also check:How to resolve the algorithm Program termination step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the GW-BASIC programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Diego programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the Erlang programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Haskell programming language