How to resolve the algorithm Order disjoint list items step by step in the Sidef programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Order disjoint list items step by step in the Sidef 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 Sidef programming language
Source code in the sidef programming language
func dsort(m, n) {
var h = Hash()
n.each {|item| h{item} := 0 ++ }
m.map {|item| h{item} := 0 -- > 0 ? n.shift : item}
}
<<'EOT'.lines.each { |line|
the cat sat on the mat | mat cat
the cat sat on the mat | cat mat
A B C A B C A B C | C A C A
A B C A B D A B E | E A D A
A B | B
A B | B A
A B B A | B A
EOT
var (a, b) = line.split('|').map{.words}...
say "#{a.join(' ')} | #{b.join(' ')} -> #{dsort(a.clone, b.clone).join(' ')}"
}
You may also check:How to resolve the algorithm Catalan numbers step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Almkvist-Giullera formula for pi step by step in the Factor programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Forth programming language
You may also check:How to resolve the algorithm Combinations step by step in the SETL programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the Julia programming language