How to resolve the algorithm Greatest element of a list step by step in the M2000 Interpreter programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest element of a list step by step in the M2000 Interpreter programming language

Table of Contents

Problem Statement

Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greatest element of a list step by step in the M2000 Interpreter programming language

Source code in the m2000 programming language

Module TestThis {
      Print "Search a tuple type list (is an array also)"
      A=(,)
      For i=1 to Random(1,10)
      Append A, (Random(1,100),)
      Next
      Print Len(A)
      Print A
      Print A#max()
      
      Print "Search an array"
      B=lambda->Random(1,100)
      Rem Dim A(1 to Random(1,10))<
      Dim A(1 to Random(1,10))<{=Random(1,100)}()
      Print Len(A())
      Print A()
      Print A()#max()
      
      \\ #max() skip non numeric values
      Rem Print (1,"100",3)#max()=3
      
      Print "Search an inventory list"
      Inventory C
      for i=1 to Random(1,10)
            do
                  key=random(10000)
            until not exist(c, key)
            \\ we can put a number as string
            if random(1,2)=1 then Append c, key:=B() else Append c, key:=str$(B())
      Next
      
      \\ if inventory item is string with a number work fine
      Function MaxItem(a) {
            k=each(a,2)
            val=a(0!)
            while k
                  \\ using stack of values
                  \\ over -equal to over 1 - copy value from 1 to top, means double the top value
                  \\ number - pop top value
                  \\ drop -equal to drop 1 : drop top value 
                  Push a(k^!): Over : If Number>val then Read Val else drop
                  Rem If a(k^!)>Val Then Val=a(k^!)
            end while
            =val
      }
      Print Len(C)
      Print C
      Print MaxItem(C)
      
      Print "Search a stack object"
      \\ a stack object is the same as the stack of values
      \\ which always is present
      D=stack
      I=0
      J=Random(1,10)
      \\ Stack stackobjext {}
      \\ hide current stack and attach the D stack
      Stack D {
            Push B() : I++ : IF I>J Else Loop
      }
      \\ if stack item isn't numeric we get a run time error
      Function MaxItemStack(a) {
            Stack a {env$=envelope$()}
            if replace$("N","", env$)<>"" then error "only numbers allowed"
            k=each(a,2)
            val=Stackitem(a,1)
            while k
                  If Stackitem(k)>val then Val=stackitem(k)
            end while
            =val
      }
      Print Len(D)
      Print D
      Print MaxItemStack(D)     	
}
TestThis

  

You may also check:How to resolve the algorithm Bitmap step by step in the MAXScript programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the D programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the Elixir programming language
You may also check:How to resolve the algorithm Hofstadter-Conway $10,000 sequence step by step in the Erlang programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the Ring programming language