How to resolve the algorithm Range consolidation step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range consolidation step by step in the Action! programming language

Table of Contents

Problem Statement

Define a range of numbers   R,   with bounds   b0   and   b1   covering all numbers between and including both bounds.

That range can be shown as:

Given two ranges, the act of consolidation between them compares the two ranges:

Given   N   ranges where   N > 2   then the result is the same as repeatedly replacing all combinations of two ranges by their consolidation until no further consolidation between range pairs is possible. If   N < 2   then range consolidation has no strict meaning and the input can be returned.

Let a normalized range display show the smaller bound to the left;   and show the range with the smaller lower bound to the left of other ranges when showing multiple ranges. Output the normalized result of applying consolidation to these five sets of ranges: Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range consolidation step by step in the Action! programming language

Source code in the action! programming language

INCLUDE "H6:REALMATH.ACT"

DEFINE PTR="CARD"
DEFINE RANGESIZE="12"
DEFINE LOW_="+0"
DEFINE HIGH_="+6"
TYPE Range=[CARD l1,l2,l3,h1,h2,h3]

PROC Inverse(Range POINTER r)
  REAL tmp

  RealAssign(r LOW_,tmp)
  RealAssign(r HIGH_,r LOW_)
  RealAssign(tmp,r HIGH_)
RETURN

PROC Normalize(Range POINTER r)
  IF RealLess(r HIGH_,r LOW_) THEN
    Inverse(r)
  FI
RETURN

INT FUNC Compare(Range Pointer r1,r2)
  IF RealLess(r1 LOW_,r2 LOW_) THEN
    RETURN (-1)
  ELSEIF RealLess(r2 LOW_,r1 LOW_) THEN
    RETURN (1)
  ELSEIF RealLess(r1 HIGH_,r2 HIGH_) THEN
    RETURN (-1)
  ELSEIF RealLess(r2 HIGH_,r1 HIGH_) THEN
    RETURN (1)
  FI
RETURN (0)

PTR FUNC GetItemAddr(PTR data INT index)
RETURN (data+index*RANGESIZE)

PROC Swap(Range POINTER r1,r2)
  REAL tmp

  RealAssign(r1 LOW_,tmp)
  RealAssign(r2 LOW_,r1 LOW_)
  RealAssign(tmp, r2 LOW_)
  RealAssign(r1 HIGH_,tmp)
  RealAssign(r2 HIGH_,r1 HIGH_)
  RealAssign(tmp, r2 HIGH_)
RETURN

PROC Sort(PTR data INT count)
  INT i,j,minpos
  Range POINTER r1,r2

  FOR i=0 TO count-2
  DO
    minpos=i
    FOR j=i+1 TO count-1
    DO
      r1=GetItemAddr(data,minpos)
      r2=GetItemAddr(data,j)
      IF Compare(r1,r2)>0 THEN
        minpos=j
      FI
    OD
    
    IF minpos#i THEN
      r1=GetItemAddr(data,minpos)
      r2=GetItemAddr(data,i)
      Swap(r1,r2)
    FI
  OD
RETURN

PROC Consolidate(PTR data INT POINTER count)
  INT i,j,newCount
  Range POINTER r1,r2

  FOR i=0 TO count^-1
  DO
    r1=GetItemAddr(data,i)
    Normalize(r1)
  OD
  Sort(data,count^)

  newCount=0 i=0
  WHILE i
  DO
    j=i+1
    WHILE j
    DO
      r1=GetItemAddr(data,i)
      r2=GetItemAddr(data,j)
      IF RealLess(r1 HIGH_,r2 LOW_) THEN
        EXIT
      ELSEIF RealLess(r1 HIGH_,r2 HIGH_) THEN
        RealAssign(r2 HIGH_,r1 HIGH_)
      FI
      j==+1
    OD
    r1=GetItemAddr(data,i)
    r2=GetItemAddr(data,newCount)
    RealAssign(r1 LOW_,r2 LOW_)
    RealAssign(r1 HIGH_,r2 HIGH_)
    newCount==+1
    i=j
  OD
  count^=newCount
RETURN

PROC PrintRanges(PTR data INT count)
  INT i
  Range POINTER r

  FOR i=0 TO count-1
  DO
    IF i>0 THEN Put(' ) FI
    r=GetItemAddr(data,i)
    Put('[) PrintR(r LOW_)
    Put(',) PrintR(r HIGH_) Put('])
  OD
RETURN

PROC Append(PTR data INT POINTER count
  CHAR ARRAY sLow,sHigh)
  Range POINTER r

  r=GetItemAddr(data,count^)
  ValR(sLow,r LOW_)
  ValR(sHigh,r High_)
  count^=count^+1
RETURN

INT FUNC InitData(BYTE case PTR data)
  INT count

  count=0
  IF case=0 THEN
    Append(data,@count,"1.1","2.2")
  ELSEIF case=1 THEN
    Append(data,@count,"6.1","7.2")
    Append(data,@count,"7.2","8.3")
  ELSEIF case=2 THEN
    Append(data,@count,"4","3")
    Append(data,@count,"2","1")
  ELSEIF case=3 THEN
    Append(data,@count,"4","3")
    Append(data,@count,"2","1")
    Append(data,@count,"-1","-2")
    Append(data,@count,"3.9","10")
  ELSEIF case=4 THEN
    Append(data,@count,"1","3")
    Append(data,@count,"-6","-1")
    Append(data,@count,"-4","-5")
    Append(data,@count,"8","2")
    Append(data,@count,"-6","-6")
  FI
RETURN (count)

PROC Main()
  BYTE ARRAY data(100)
  INT count
  BYTE i

  Put(125) PutE() ;clear the screen
  FOR i=0 TO 4
  DO
    count=InitData(i,data)
    PrintRanges(data,count)
    Print(" -> ")
    Consolidate(data,@count)
    PrintRanges(data,count)
    PutE() PutE()
  OD
RETURN

  

You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Swift programming language
You may also check:How to resolve the algorithm Peano curve step by step in the C programming language
You may also check:How to resolve the algorithm Bitmap/Flood fill step by step in the C++ programming language
You may also check:How to resolve the algorithm Forward difference step by step in the Lua programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the Erlang programming language