How to resolve the algorithm Ordered words step by step in the Lang5 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ordered words step by step in the Lang5 programming language

Table of Contents

Problem Statement

An   ordered word   is a word in which the letters appear in alphabetic order. Examples include   abbey   and   dirt. Find and display all the ordered words in the dictionary   unixdict.txt   that have the longest word length. (Examples that access the dictionary file locally assume that you have downloaded this file yourself.)
The display needs to be shown on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ordered words step by step in the Lang5 programming language

Source code in the lang5 programming language

: >string-index
    "" split
    "&'0123456789abcdefghijklmnopqrstuvwxyz" "" split
    swap index collapse ;
: chars  "" split length swap drop ;
: cr  "\n" . ;
: nip  swap drop ;
: ordered?
    dup grade subscript != '+ reduce if 0 else -1 then ;

: filtering
    [] '_ set
    0 do read 
        2dup chars
        <=
        if  dup >string-index ordered?
            if   2dup chars
                 <
                 if   nip dup chars swap
                      [] '_ set
                 then
                 _ swap append '_ set
                 '. .                       # progress dot
            else drop
            then
        else drop
        then
    eof if break then loop

    cr _ . cr
    ;

: ordered-words
    '< 'unixdict.txt open 'fh set
    fh fin filtering fh close ;

ordered-words

  

You may also check:How to resolve the algorithm Jewels and stones step by step in the Go programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Octave programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the TUSCRIPT programming language
You may also check:How to resolve the algorithm Greedy algorithm for Egyptian fractions step by step in the Perl programming language
You may also check:How to resolve the algorithm Empty program step by step in the Standard ML programming language