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

Published on 12 May 2024 09:40 PM

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

Source code in the m2000 programming language

Module Checkit {
      Flush ' empty stack
      Inventory Queue Haystack=  "foo", "bar", "baz", "quux", "quuux", "quuuux", "bazola", "ztesch", "foo", "bar", "thud", "grunt"
      Append  Haystack, "foo", "bar", "bletch", "foo", "bar", "fum", "fred", "jim", "sheila", "barney", "flarp", "zxc"
      Append  Haystack,  "spqr", "wombat", "shme", "foo", "bar", "baz", "bongo", "spam", "eggs", "snork", "foo", "bar"
      Append  Haystack,  "zot", "blarg", "wibble", "toto", "titi", "tata", "tutu", "pippo", "pluto", "paperino", "aap"
      Append  Haystack,  "noot", "mies", "oogle", "foogle", "boogle", "zork", "gork", "bork"
      \\ Inventories are objects and we have access to properties using COM model 
      With HayStack, "index" as index
      Inventory Queue HayStackRev
      N=Each(HayStack, -1, 1)
      While N {
            Append HayStackRev, Eval$(N, N^)
      }
      With HayStackRev, "index" as indexRev
      Print Len(HayStack)
      Print Len(HayStackRev)
      local needle$
      \\ Print all elements using columns
      Print haystack
      Repeat {
                Input "Word to search for? (Leave blank to exit) ", needle$
                If needle$ <> "" Then {
                          If Exist(haystackrev,lcase$(needle$) ) Then {
                              Print "Found "; CHR$(34); needle$; CHR$(34); " at index "; STR$(len(haystackrev)-indexrev,"")
                              
                              If Exist(haystack,lcase$(needle$) ) Then  {
                                    if len(haystackrev)-1<>indexrev+index then {
                                                Print "Found "; CHR$(34); needle$; CHR$(34); " at index "; STR$(Len(haystack)-index,"")
                                    }
                              }
                        } Else  Print CHR$(34); needle$; CHR$(34); " not found"    
            } Else Exit
      } Always     
}
CheckIt

Module CheckThis {      
      Inventory Queue Haystack=  "foo", "bar", "baz", "quux", "quuux", "quuuux", "bazola", "ztesch", "foo", "bar", "thud", "grunt"
      Append  Haystack, "foo", "bar", "bletch", "foo", "bar", "fum", "fred", "jim", "sheila", "barney", "flarp", "zxc"
      Append  Haystack,  "spqr", "wombat", "shme", "foo", "bar", "baz", "bongo", "spam", "eggs", "snork", "foo", "bar"
      Append  Haystack,  "zot", "blarg", "wibble", "toto", "titi", "tata", "tutu", "pippo", "pluto", "paperino", "aap"
      Append  Haystack,  "noot", "mies", "oogle", "foogle", "boogle", "zork", "gork", "bork"
      \\ Print all list
      Print Haystack
      \\ inventory queue can get same keys
      \\ inventory use hashtable.
      \\ Inventory put same keys in a linked list, so we can found easy
      Do
             Input "Word to search for? (Leave blank press enter to exit) ", needle$
             if needle$="" then exit
             n=1
             s$=lcase$(needle$)
             While exist(Haystack, s$, n)
                    \\ number, key  and position (zero based convert to one based)
                    Print n, Eval$(HayStack!), Eval(HayStack!)+1
                    n++
             End While
             If n=1 Then Print needle$;" not found"
      Always
}
CheckThis

  

You may also check:How to resolve the algorithm Random number generator (included) step by step in the DWScript programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Clojure programming language
You may also check:How to resolve the algorithm Playfair cipher step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Empty string step by step in the Lang programming language
You may also check:How to resolve the algorithm Pairs with common factors step by step in the FreeBASIC programming language