How to resolve the algorithm ABC problem step by step in the OpenEdge/Progress programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm ABC problem step by step in the OpenEdge/Progress 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 OpenEdge/Progress programming language

Source code in the openedge/progress programming language

FUNCTION canMakeWord RETURNS LOGICAL (INPUT pWord AS CHARACTER) FORWARD.

/* List of blocks */
DEFINE TEMP-TABLE ttBlocks NO-UNDO
    FIELD ttFaces AS CHARACTER FORMAT "x(1)" EXTENT 2
    FIELD ttUsed AS LOGICAL.

/* Fill in list of blocks */
RUN AddBlock("BO").
RUN AddBlock("XK").
RUN AddBlock("DQ").
RUN AddBlock("CP").
RUN AddBlock("NA").
RUN AddBlock("GT").
RUN AddBlock("Re").
RUN AddBlock("TG").
RUN AddBlock("QD").
RUN AddBlock("FS").
RUN AddBlock("JW").
RUN AddBlock("HU").
RUN AddBlock("VI").
RUN AddBlock("AN").
RUN AddBlock("OB").
RUN AddBlock("ER").
RUN AddBlock("FS").
RUN AddBlock("LY").
RUN AddBlock("PC").
RUN AddBlock("ZM").

DEFINE VARIABLE chWords AS CHARACTER EXTENT 7 NO-UNDO.
ASSIGN  chWords[1] = "A"
        chWords[2] = "BARK"
        chWords[3] = "BOOK"
        chWords[4] = "TREAT"
        chWords[5] = "COMMON"
        chWords[6] = "SQUAD"
        chWords[7] = "CONFUSE".

DEFINE FRAME frmResult
    WITH NO-LABELS 7 DOWN USE-TEXT.

DEFINE VARIABLE i AS INTEGER NO-UNDO.
DO i = 1 TO 7:
    DISPLAY chWords[i] + " = " + STRING(canMakeWord(chWords[i])) FORMAT "x(25)" WITH FRAME frmResult.  
    DOWN WITH FRAME frmResult.
END.


PROCEDURE AddBlock:
    DEFINE INPUT PARAMETER i-chBlockvalue AS CHARACTER NO-UNDO.

    IF (LENGTH(i-chBlockValue) <> 2)
        THEN RETURN ERROR.

    CREATE ttBlocks.
    ASSIGN  ttBlocks.ttFaces[1] = SUBSTRING(i-chBlockValue, 1, 1)
            ttBlocks.ttFaces[2] = SUBSTRING(i-chBlockValue, 2, 1).
END PROCEDURE.


FUNCTION blockInList RETURNS LOGICAL (pChar AS CHARACTER):
    /* Find first unused block in list */
    FIND FIRST ttBlocks WHERE (ttBlocks.ttFaces[1] = pChar
                               OR ttBlocks.ttFaces[2] = pChar)
                          AND NOT ttBlocks.ttUsed NO-ERROR.
    IF (AVAILABLE ttBlocks) THEN DO:
        /* found it! set to used and return true */
        ASSIGN ttBlocks.ttUsed = TRUE.
        RETURN TRUE.
    END.
    ELSE RETURN FALSE.
END FUNCTION.


FUNCTION canMakeWord RETURNS LOGICAL (INPUT pWord AS CHARACTER):
    DEFINE VARIABLE i AS INTEGER NO-UNDO.
    DEFINE VARIABLE chChar AS CHARACTER NO-UNDO.

    /* Word has to be valid */
    IF (LENGTH(pWord) = 0) 
        THEN RETURN FALSE.

    DO i = 1 TO LENGTH(pWord):
        /* get the char */
        chChar = SUBSTRING(pWord, i, 1).

        /* Check to see if this is a letter? */
        IF ((ASC(chChar) < 65) OR (ASC(chChar) > 90) AND
            (ASC(chChar) < 97) OR (ASC(chChar) > 122)) 
            THEN RETURN FALSE.

        /* Is block is list (and unused) */
        IF NOT blockInList(chChar)
            THEN RETURN FALSE.
    END.

    /* Reset all blocks */
    FOR EACH ttBlocks:
        ASSIGN ttUsed = FALSE.
    END.
    RETURN TRUE.
END FUNCTION.

  

You may also check:How to resolve the algorithm Jensen's Device step by step in the Objeck programming language
You may also check:How to resolve the algorithm Happy numbers step by step in the Ruby programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Nim programming language
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Mercury programming language
You may also check:How to resolve the algorithm Loops/Infinite step by step in the Phix programming language