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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "./fmt" for Fmt

var r // recursive
r = Fn.new { |word, bl|
    if (word == "") return true
    var c = word.bytes[0] | 32
    for (i in 0...bl.count) {
        var b = bl[i] 
        if (c == b.bytes[0] | 32 || c == b.bytes[1] | 32) {
            bl[i] = bl[0]
            bl[0] = b
            if (r.call(word[1..-1], bl[1..-1])) return true
            var t = bl[i]
            bl[i] = bl[0]
            bl[0] = t
        }
    }
    return false
}

var newSpeller = Fn.new { |blocks|
    var bl = blocks.split(" ")
    return Fn.new { |word| r.call(word, bl) }
}

var sp = newSpeller.call("BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM")
for (word in ["A", "BARK", "BOOK", "TREAT", "COMMON", "SQUAD", "CONFUSE"]) {
    Fmt.print("$-7s $s", word, sp.call(word))
}

  

You may also check:How to resolve the algorithm Transliterate English text using the Greek alphabet step by step in the Raku programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the E programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Send an unknown method call step by step in the Go programming language
You may also check:How to resolve the algorithm Infinity step by step in the Argile programming language