How to resolve the algorithm Order disjoint list items step by step in the Tcl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Order disjoint list items step by step in the Tcl 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 Tcl programming language
Source code in the tcl programming language
proc orderDisjoint {theList theOrderList} {
foreach item $theOrderList {incr n($item)}
set is {}
set i 0
foreach item $theList {
if {[info exist n($item)] && [incr n($item) -1] >= 0} {
lappend is $i
}
incr i
}
foreach item $theOrderList i $is {lset theList $i $item}
return $theList
}
proc orderDisjoint {theList theOrderList} {
foreach item $theOrderList {incr n($item)}
set is -
set i 0
foreach item $theList {
if {[info exist n($item)] && [incr n($item) -1] >= 0} {
lappend is $i
}
incr i
}
set i 0
foreach item $theOrderList {
if {[incr n($item)] <= 1} {
lset theList [lindex $is [incr i]] $item
}
}
return $theList
}
foreach {items order} {
"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"
} {
puts "'$items' with '$order' => '[orderDisjoint $items $order]'"
}
You may also check:How to resolve the algorithm Convert decimal number to rational step by step in the J programming language
You may also check:How to resolve the algorithm Multiple regression step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Even or odd step by step in the Mercury programming language
You may also check:How to resolve the algorithm Mandelbrot set step by step in the AWK programming language
You may also check:How to resolve the algorithm Fractal tree step by step in the Amazing Hopper programming language