Best shuffle: Difference between revisions
Content deleted Content added
Per request, replaced PowerShell with sample that meets task requirements |
|||
Line 2,316: | Line 2,316: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{ |
{{works with|PowerShell|3}} |
||
<lang PowerShell># Calculate best possible shuffle score for a given string |
|||
{{incorrect|PowerShell|Seasaw should have a score of 0, which it clearly doesn't have.}} |
|||
# (Split out into separate function so we can use it separately in our output) |
|||
<lang PowerShell> |
|||
function |
function Get-BestScore ( [string]$String ) |
||
{ |
|||
foreach($string in $strings){ |
|||
# Convert to array of characters, group identical characters, |
|||
⚫ | |||
# sort by frequecy, get size of first group |
|||
$sa2 = Get-Random -InputObject $sa1 -Count ([int]::MaxValue) |
|||
$MostRepeats = $String.ToCharArray() | |
|||
Group | |
|||
echo $string |
|||
Sort Count -Descending | |
|||
⚫ | |||
Select -First 1 -ExpandProperty Count |
|||
} |
|||
⚫ | |||
# Return count of most repeated character minus all other characters (math simplified) |
|||
Best-Shuffle "abracadabra", "seesaw", "pop", "grrrrrr", "up", "a" |
|||
return [math]::Max( 0, 2 * $MostRepeats - $String.Length ) |
|||
⚫ | |||
} |
|||
Output: |
|||
function Get-BestShuffle ( [string]$String ) |
|||
<pre> |
|||
{ |
|||
arbcardaaba |
|||
# Convert to arrays of characters, one for comparison, one for manipulation |
|||
aesesw |
|||
⚫ | |||
opp |
|||
$S2 = $String.ToCharArray() |
|||
rrgrrrr |
|||
pu |
|||
# Calculate best possible score as our goal |
|||
a |
|||
$BestScore = Get-BestScore $String |
|||
</pre> |
|||
# Unshuffled string has score equal to number of characters |
|||
$Length = $String.Length |
|||
$Score = $Length |
|||
# While still striving for perfection... |
|||
While ( $Score -gt $BestScore ) |
|||
{ |
|||
# For each character |
|||
ForEach ( $i in 0..($Length-1) ) |
|||
{ |
|||
# If the shuffled character still matches the original character... |
|||
If ( $S1[$i] -eq $S2[$i] ) |
|||
{ |
|||
# Swap it with a random character |
|||
# (Random character $j may be the same as or may even be |
|||
# character $i. The minor impact on speed was traded for |
|||
# a simple solution to guarantee randomness.) |
|||
$j = Get-Random -Maximum $Length |
|||
$S2[$i], $S2[$j] = $S2[$j], $S2[$i] |
|||
} |
|||
} |
|||
# Count the number of indexes where the two arrays match |
|||
$Score = ( 0..($Length-1) ).Where({ $S1[$_] -eq $S2[$_] }).Count |
|||
} |
|||
# Put it back into a string |
|||
$Shuffle = ( [string[]]$S2 -join '' ) |
|||
return $Shuffle |
|||
⚫ | |||
<lang PowerShell>ForEach ( $String in ( 'abracadabra', 'seesaw', 'elk', 'grrrrrr', 'up', 'a' ) ) |
|||
{ |
|||
$Shuffle = Get-BestShuffle $String |
|||
$Score = Get-BestScore $String |
|||
"$String, $Shuffle, ($Score)" |
|||
}</lang> |
|||
{{out}} |
|||
<pre>abracadabra, craradabaab, (0) |
|||
seesaw, ewsase, (0) |
|||
elk, kel, (0) |
|||
grrrrrr, rrrrrrg, (5) |
|||
up, pu, (0) |
|||
a, a, (1)</pre> |
|||
=={{header|Prolog}}== |
=={{header|Prolog}}== |