Best shuffle: Difference between revisions

Content added Content deleted
m (Added indents to improve readability)
Line 1,977: Line 1,977:
=={{header|FutureBasic}}==
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
<syntaxhighlight lang="futurebasic">

include "Tlbx GameplayKit.incl"
include "Tlbx GameplayKit.incl"
include "NSLog.incl"
include "NSLog.incl"


local fn ShuffleString( string as CFStringRef ) as CFStringRef
local fn ShuffleString( string as CFStringRef ) as CFStringRef
NSInteger i
NSInteger i

CFMutableArrayRef mutArr = fn MutableArrayWithCapacity( 0 )
CFMutableArrayRef mutArr = fn MutableArrayWithCapacity( 0 )
for i = 0 to fn StringLength( string ) - 1
for i = 0 to fn StringLength( string ) - 1
MutableArrayAddObject( mutArr, fn StringSubstringWithRange( string, fn CFRangeMake( i, 1 ) ) )
MutableArrayAddObject( mutArr, fn StringSubstringWithRange( string, fn CFRangeMake( i, 1 ) ) )
next
next
CFArrayRef shuffledArr = fn GKRandomSourceArrayByShufflingObjectsInArray( fn GKRandomSourceInit, mutArr )
CFArrayRef shuffledArr = fn GKRandomSourceArrayByShufflingObjectsInArray( fn GKRandomSourceInit, mutArr )
end fn = fn ArrayComponentsJoinedByString( shuffledArr, @"" )
end fn = fn ArrayComponentsJoinedByString( shuffledArr, @"" )




local fn StringDifferences( string1 as CFStringRef, string2 as CFStringRef ) as NSInteger
local fn StringDifferences( string1 as CFStringRef, string2 as CFStringRef ) as NSInteger
NSInteger i, unchangedPosition = 0
NSInteger i, unchangedPosition = 0

if fn StringLength( string1 ) != fn StringLength( string2 ) then NSLog( @"Strings must be of equal length." ) : exit fn
if fn StringLength( string1 ) != fn StringLength( string2 ) then NSLog( @"Strings must be of equal length." ) : exit fn

for i = 0 to fn StringLength( string1 ) -1
for i = 0 to fn StringLength( string1 ) -1
CFStringRef tempStr1 = fn StringSubstringWithRange( string1, fn CFRangeMake( i, 1 ) )
CFStringRef tempStr1 = fn StringSubstringWithRange( string1, fn CFRangeMake( i, 1 ) )
CFStringRef tempStr2 = fn StringSubstringWithRange( string2, fn CFRangeMake( i, 1 ) )
CFStringRef tempStr2 = fn StringSubstringWithRange( string2, fn CFRangeMake( i, 1 ) )
if fn StringIsEqual( tempStr1, tempStr2 ) == YES then unchangedPosition++
if fn StringIsEqual( tempStr1, tempStr2 ) == YES then unchangedPosition++
next
next
end fn = unchangedPosition
end fn = unchangedPosition


Line 2,012: Line 2,011:


for i = 0 to 3
for i = 0 to 3
for j = 0 to count - 1
for j = 0 to count - 1
originalStr = stringArr[j]
originalStr = stringArr[j]
shuffledStr = fn ShuffleString( stringArr[j] )
shuffledStr = fn ShuffleString( stringArr[j] )
NSLog( @"%@, %@, (%ld)", originalStr, shuffledStr, fn StringDifferences( originalStr, shuffledStr ) )
NSLog( @"%@, %@, (%ld)", originalStr, shuffledStr, fn StringDifferences( originalStr, shuffledStr ) )
next
next
NSLog( @"\n" )
NSLog( @"\n" )
next
next


Line 2,052: Line 2,051:
a, a, (1)
a, a, (1)
</pre>
</pre>

=={{header|Go}}==
=={{header|Go}}==
{{trans|Icon and Unicon}}
{{trans|Icon and Unicon}}