How to resolve the algorithm Order two numerical lists step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Order two numerical lists step by step in the Action! programming language

Table of Contents

Problem Statement

Write a function that orders two lists or arrays filled with numbers. The function should accept two lists as arguments and return true if the first list should be ordered before the second, and false otherwise. The order is determined by lexicographic order: Comparing the first element of each list. If the first elements are equal, then the second elements should be compared, and so on, until one of the list has no more elements. If the first list runs out of elements the result is true. If the second list or both run out of elements the result is false. Note: further clarification of lexicographical ordering is expounded on the talk page here and here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Order two numerical lists step by step in the Action! programming language

Source code in the action! programming language

INT FUNC Compare(INT ARRAY x INT xLen INT ARRAY y INT yLen)
  INT i,len

  len=xLen
  IF len>yLen THEN
    len=yLen
  FI
  FOR i=0 TO len-1
  DO
    IF x(i)>y(i) THEN
      RETURN (1)
    ELSEIF x(i)
      RETURN (-1)
    FI
  OD
  IF xLen>yLen THEN
    RETURN (1)
  ELSEIF xLen
    RETURN (-1)
  FI
RETURN (0)

BYTE FUNC Less(INT ARRAY x INT xLen INT ARRAY y INT yLen)
  IF Compare(x,xLen,y,yLen)<0 THEN
    RETURN (1)
  FI
RETURN (0)

PROC PrintArray(INT ARRAY x INT len)
  INT i

  Put('[)
  FOR i=0 TO len-1
  DO
    PrintI(x(i))
    IF i
      Put(' )
    FI
  OD
  Put('])
RETURN

PROC Test(INT ARRAY x INT xLen INT ARRAY y INT yLen)
  PrintArray(x,xLen)
  IF Less(x,xLen,y,yLen) THEN
     Print(" < ")
  ELSE
    Print(" >= ")
  FI
  PrintArray(y,yLen) PutE()
RETURN

PROC Main()
  INT ARRAY a=[1 2 1 5 2]
  INT ARRAY b=[1 2 1 5 2 2]
  INT ARRAY c=[1 2 3 4 5]
  INT ARRAY d=[1 2 3 4 5]
  
  Test(a,5,b,6)
  Test(b,6,a,5)
  Test(b,6,c,5)
  Test(c,5,b,6)
  Test(c,5,d,5)
RETURN

  

You may also check:How to resolve the algorithm Hex words step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Fusc sequence step by step in the Vala programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Guess the number step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the CLU programming language