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

Published on 12 May 2024 09:40 PM

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

Source code in the phixmonti programming language

"mouse" "hat" "cup" "deodorant" "television"
"soap" "methamphetamine" "severed cat heads" "cup"
pstack
stklen tolist reverse
0 tolist var t

"Enter string to search: " input var s nl
true
while
    head s == if
        len t swap 0 put var t
    endif
    tail nip len
endwhile
drop

t len not if
    "String not found in list" print
else
    reverse
    "First index for " print s print " : " print 1 get print
    len 1 > if
        nl "Last index for " print s print " : " print len get print
    endif
endif
drop

include Utilitys.pmt

0 var acum
0 var p
"Zag" var word

def search
    word find var p p
enddef

( "Zig" "Zag" "Wally" "Ronald" "Bush" "Krusty" "Charlie" "Bush" "Boz" "Zag" )

dup print nl nl

search
while
    p acum + var acum
    ( word " located in position " acum ) lprint nl
    len p - p 1 + swap slice nip
    search
endwhile

  

You may also check:How to resolve the algorithm N-queens problem step by step in the Frink programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Ada programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Hy programming language
You may also check:How to resolve the algorithm Padovan sequence step by step in the Rust programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the TXR programming language