How to resolve the algorithm Order disjoint list items step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Order disjoint list items step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

Given   M   as a list of items and another list   N   of items chosen from   M,   create   M'   as a list with the first occurrences of items from   N   sorted to be in one of the set of indices of their original occurrence in   M   but in the order given by their order in   N. That is, items in   N   are taken from   M   without replacement, then the corresponding positions in   M'   are filled by successive items from   N.

The words not in   N   are left in their original positions.

If there are duplications then only the first instances in   M   up to as many as are mentioned in   N   are potentially re-ordered.

Is ordered as:

Show the output, here, for at least the following inputs:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Order disjoint list items step by step in the Icon and Unicon programming language

Source code in the icon programming language

procedure main(A)
    every write(" -> ",odli("the cat sat on the mat","mat cat"))
    every write(" -> ",odli("the cat sat on the mat","cat mat"))
    every write(" -> ",odli("A B C A B C A B C","C A C A"))
    every write(" -> ",odli("A B C A B D A B E","E A D A"))
    every write(" -> ",odli("A B","B"))
    every write(" -> ",odli("A B","B A"))
    every write(" -> ",odli("A B B A","B A"))
end

procedure odli(M,N)
    writes(M," :: ",N)
    Mp := "" 
    P := N ||:= " "
    (M||" ") ? while item := tab(upto(' '))||move(1) do {
            if find(item,P) then {
                P ?:= 1(tab(find(item)),move(*item))||tab(0)
                N ?:= (item := tab(upto(' '))||move(1), tab(0))
                }
            Mp ||:= item
            }
    return Mp
end


  

You may also check:How to resolve the algorithm Associative array/Creation step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Comments step by step in the Wart programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the PL/SQL programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the Lang5 programming language
You may also check:How to resolve the algorithm Introspection step by step in the Nim programming language