How to resolve the algorithm Best shuffle step by step in the VBScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Best shuffle step by step in the VBScript 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 VBScript programming language
Source code in the vbscript programming language
'Best Shuffle Task
'VBScript Implementation
Function bestshuffle(s)
Dim arr:Redim arr(Len(s)-1)
'The Following Does the toCharArray() Functionality
For i = 0 To Len(s)-1
arr(i) = Mid(s, i + 1, 1)
Next
arr = shuffler(arr) 'Make this line a comment for deterministic solution
For i = 0 To UBound(arr):Do
If arr(i) <> Mid(s, i + 1, 1) Then Exit Do
For j = 0 To UBound(arr)
If arr(i) <> arr(j) And arr(i) <> Mid(s, j + 1, 1) And arr(j) <> Mid(s, i + 1, 1) Then
tmp = arr(i)
arr(i) = arr(j)
arr(j) = tmp
End If
Next
Loop While False:Next
shuffled_word = Join(arr,"")
'This section is the scorer
score = 0
For k = 1 To Len(s)
If Mid(s,k,1) = Mid(shuffled_word,k,1) Then
score = score + 1
End If
Next
bestshuffle = shuffled_word & ",(" & score & ")"
End Function
Function shuffler(array)
Set rand = CreateObject("System.Random")
For i = UBound(array) to 0 Step -1
r = rand.next_2(0, i + 1)
tmp = array(i)
array(i) = array(r)
array(r) = tmp
Next
shuffler = array
End Function
'Testing the function
word_list = Array("abracadabra","seesaw","elk","grrrrrr","up","a")
For Each word In word_list
WScript.StdOut.WriteLine word & "," & bestshuffle(word)
Next
You may also check:How to resolve the algorithm Averages/Median step by step in the LSL programming language
You may also check:How to resolve the algorithm A+B step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Test a function step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the Wren programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Python programming language