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

Published on 12 May 2024 09:40 PM

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

Source code in the action! programming language

DEFINE PTR="CARD"

INT FUNC Search(PTR ARRAY texts INT count CHAR ARRAY text)
  INT i

  FOR i=0 TO count-1
  DO
    IF SCompare(texts(i),text)=0 THEN
      RETURN (i)
    FI
  OD
RETURN (-1)

PROC Test(PTR ARRAY texts INT count CHAR ARRAY text)
  INT index

  index=Search(texts,count,text)
  IF index=-1 THEN
    PrintF("""%S"" is not in haystack.%E",text)
  ELSE
    PrintF("""%S"" is on index %I in haystack.%E",text,index)
  FI
RETURN

PROC Main()
  PTR ARRAY texts(7)

  texts(0)="Monday"
  texts(1)="Tuesday"
  texts(2)="Wednesday"
  texts(3)="Thursday"
  texts(4)="Friday"
  texts(5)="Saturday"
  texts(6)="Sunday"

  Test(texts,7,"Monday")
  Test(texts,7,"Sunday")
  Test(texts,7,"Thursday")
  Test(texts,7,"Weekend")
RETURN

  

You may also check:How to resolve the algorithm Send an unknown method call step by step in the Pike programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the Logo programming language
You may also check:How to resolve the algorithm Balanced ternary step by step in the Glagol programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Toka programming language
You may also check:How to resolve the algorithm Palindrome dates step by step in the QB64 programming language