How to resolve the algorithm Search a list step by step in the Ceylon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Search a list step by step in the Ceylon 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 Ceylon programming language

Source code in the ceylon programming language

shared test void searchAListTask() {
    value haystack = [
            "Zig", "Zag", "Wally", "Ronald", "Bush",
            "Krusty", "Charlie", "Bush", "Bozo"];

    assert(exists firstIdx = haystack.firstOccurrence("Bush"));
    assert(exists lastIdx = haystack.lastOccurrence("Bush"));

    assertEquals(firstIdx, 4);
    assertEquals(lastIdx, 7);
}


  

You may also check:How to resolve the algorithm Sort an integer array step by step in the R programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the 360 Assembly programming language
You may also check:How to resolve the algorithm Send email step by step in the Perl programming language
You may also check:How to resolve the algorithm Check output device is a terminal step by step in the Wren programming language
You may also check:How to resolve the algorithm Compound data type step by step in the J programming language