How to resolve the algorithm Order disjoint list items step by step in the Scala programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Order disjoint list items step by step in the Scala 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 Scala programming language

Source code in the scala programming language

def order[T](input: Seq[T], using: Seq[T], used: Seq[T] = Seq()): Seq[T] =
  if (input.isEmpty || used.size >= using.size) input
  else if (using diff used contains input.head)
    using(used.size) +: order(input.tail, using, used :+ input.head)
  else input.head +: order(input.tail, using, used)


val tests = List(
  "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"
)

tests.foreach{case (input, using) =>
  val done = order(input.split(" "), using.split(" "))
  println(f"""Data M: $input%-24s Order N: $using%-9s -> Result M': ${done mkString " "}""")
}


  

You may also check:How to resolve the algorithm Count in octal step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm MAC vendor lookup step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Simple windowed application step by step in the Web 68 programming language
You may also check:How to resolve the algorithm Harmonic series step by step in the Phix programming language