How to resolve the algorithm Search a list step by step in the S-lang programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Search a list step by step in the S-lang programming language
Table of Contents
Problem Statement
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack). Raise an exception if the needle is missing. If there is more than one occurrence then return the smallest index to the needle. Return the largest index to a needle that has multiple occurrences in the haystack.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list step by step in the S-lang programming language
Source code in the s-lang programming language
variable haystack = ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo","Ronald"];
define find(needle)
{
variable i = where(haystack == needle);
if (length(i)) {
% print(sprintf("%s: first=%d, last=%d", needle, i[0], i[-1]));
return(i[0], i[-1]);
}
else
throw ApplicationError, "an exception";
}
($1, $2) = find("Ronald"); % returns 3, 9
($1, $2) = find("McDonald"); % throws ApplicationError, labelled "an exception"
You may also check:How to resolve the algorithm Window creation step by step in the C++ programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the HicEst programming language
You may also check:How to resolve the algorithm Flatten a list step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Oberon-2 programming language