How to resolve the algorithm Best shuffle step by step in the Icon and Unicon programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Best shuffle step by step in the Icon and Unicon programming language

Table of Contents

Problem Statement

Shuffle the characters of a string in such a way that as many of the character values are in a different position as possible. A shuffle that produces a randomized result among the best choices is to be preferred. A deterministic approach that produces the same sequence every time is acceptable as an alternative. Display the result as follows: The score gives the number of positions whose character value did not change.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Best shuffle step by step in the Icon and Unicon programming language

Source code in the icon programming language

# every !t :=: ?t    # Uncomment to get a random best shuffling


procedure main(args)
    while scram := bestShuffle(line := read()) do
        write(line," -> ",scram," (",unchanged(line,scram),")")
end

procedure bestShuffle(s)
    t := s
    # every !t :=: ?t    # Uncomment to get a random best shuffling
    every i := 1 to *t do
        every j := (1 to i-1) | (i+1 to *t) do
           if (t[i] ~== s[j]) & (s[i] ~== t[j]) then break t[i] :=: t[j]
    return t
end        

procedure unchanged(s1,s2)      # Number of unchanged elements
    every (count := 0) +:= (s1[i := 1 to *s1] == s2[i], 1)
    return count
end


  

You may also check:How to resolve the algorithm Create a file step by step in the OCaml programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Delphi programming language
You may also check:How to resolve the algorithm Penney's game step by step in the Clojure programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the SQL PL programming language
You may also check:How to resolve the algorithm XML/Output step by step in the D programming language