How to resolve the algorithm Range extraction step by step in the Phixmonti programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Range extraction step by step in the Phixmonti programming language

Table of Contents

Problem Statement

A format for expressing an ordered list of integers is to use a comma separated list of either Example The list of integers: Is accurately expressed by the range expression: (And vice-versa).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Range extraction step by step in the Phixmonti programming language

Source code in the phixmonti programming language

include ..\Utilitys.pmt

( ) var res
( )
(  0  1  2  4  6  7  8 11 12 14
  15 16 17 18 19 20 21 22 23 24
  25 27 28 29 30 31 32 33 35 36
  37 38 39 )

def append
    1 get swap -1 get rot swap
    2 tolist res swap 0 put var res
enddef

def printRes
    res len for
        get
        1 get swap 2 get nip
        over over == if
            drop print
        else
            over over - abs 1 > if "-" else "," endif
            rot print print print
        endif
        "," print
    endfor
    drop
    8 tochar print " " print
enddef
  
1 get rot swap 0 put swap
  
len 2 swap 2 tolist for
    get var num
    swap -1 get 1 + num != if
        append
        flush
    endif
    num 0 put swap
endfor
swap
append

clear

printRes

include ..\Utilitys.pmt

(  0  1  2  4  6  7  8 11 12 14
  15 16 17 18 19 20 21 22 23 24
  25 27 28 29 30 31 32 33 35 36
  37 38 39 ) 

len get var fin
2 var i 
i get fin == not
while
    i 1 - get var prev
    i get prev - 1 != if
        inf i put
        i 1 + var i
    endif
    i 1 + var i
    i get fin == not
endwhile
inf 0 put

def printEnd print "," print enddef

1 var ini

len for
    var i
    i get inf == if
        i ini - ini swap slice
        -1 get swap 1 get nip
        over over == if drop printEnd
        else over over - 1 == if printEnd printEnd
            else print "-" print printEnd
            endif
        endif 
        i 1 + var ini
    endif
endfor
8 tochar print " " print

include ..\Utilitys.pmt

(  0  1  2  4  6  7  8 11 12 14
  15 16 17 18 19 20 21 22 23 24
  25 27 28 29 30 31 32 33 35 36
  37 38 39 ) 

inf 0 put

def printEnd print "," print enddef

2 var i
1 var ini
 
i get inf == not
while
    i 1 - get var prev
    i get var act
    act prev - 1 != if
        i ini -
        dup 2 == if drop ini get prev swap printEnd printEnd else
            dup 1 == if drop prev printEnd else
                drop ini get print "-" print prev printEnd
            endif
        endif
        i var ini
    endif
    i 1 + var i
    act inf == not
endwhile
8 tochar print " " print

include ..\Utilitys.pmt

def glue    /# l o -- l #/
    var ob
    len 2 * 1 - 2 swap 2 3 tolist for
        ob swap put
    endfor
enddef

( )

(  0  1  2  4  6  7  8 11 12 14
  15 16 17 18 19 20 21 22 23 24
  25 27 28 29 30 31 32 33 35 36
  37 38 39 )

len for drop
    pop swap dup var N var M
    len for drop
        head M 1 + == if
            pop swap var M
        else
            exitfor
        endif
    endfor
    swap
    N M == if N tostr 0 put else
        N 1 + M == if N tostr 0 put M tostr 0 put else
            N tostr "-" M tostr chain chain 0 put
        endif
    endif
    swap
    len 0 == if
        drop
        exitfor
    endif
endfor

"," glue lprint

  

You may also check:How to resolve the algorithm Simple windowed application step by step in the J programming language
You may also check:How to resolve the algorithm Range expansion step by step in the Oz programming language
You may also check:How to resolve the algorithm Empty directory step by step in the NewLISP programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the F# programming language