How to resolve the algorithm ABC problem step by step in the MATLAB / Octave programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm ABC problem step by step in the MATLAB / Octave 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 MATLAB / Octave programming language
Source code in the matlab programming language
function testABC
combos = ['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'};
for k = 1:length(words)
possible = canMakeWord(words{k}, combos);
fprintf('Can%s make word %s.\n', char(~possible.*'NOT'), words{k})
end
end
function isPossible = canMakeWord(word, combos)
word = lower(word);
combos = lower(combos);
isPossible = true;
k = 1;
while isPossible && k <= length(word)
[r, c] = find(combos == word(k), 1);
if ~isempty(r)
combos(r, :) = '';
else
isPossible = false;
end
k = k+1;
end
end
You may also check:How to resolve the algorithm Integer sequence step by step in the Wren programming language
You may also check:How to resolve the algorithm Literals/String step by step in the Sidef programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Racket programming language
You may also check:How to resolve the algorithm Queue/Definition step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the Action! programming language