32 lines
1.2 KiB
Plaintext
32 lines
1.2 KiB
Plaintext
void local fn Permute( string as CFStringRef, result as CFMutableArrayRef, current as CFStringRef )
|
|
if ( len(string) == 0 ) then MutableArrayAddObject( result, current ) : return
|
|
for NSUInteger i = 0 to len(string) - 1
|
|
unichar c = fn StringCharacterAtIndex( string, i )
|
|
CFMutableStringRef remainingStr = fn MutableStringWithString( string )
|
|
MutableStringDeleteCharacters( remainingStr, fn RangeMake( i, 1 ) )
|
|
CFStringRef newCurrent = fn StringByAppendingFormat( current, @"%C", c )
|
|
fn Permute( remainingStr, result, newCurrent )
|
|
next
|
|
end fn
|
|
|
|
local fn DoPermutations as CFStringRef
|
|
CFStringRef inputString = @"ABCD"
|
|
CFMutableArrayRef array1 = fn MutableArrayNew
|
|
fn Permute( inputString, array1, @"" )
|
|
|
|
CFArrayRef array2 = @[
|
|
@"ABCD", @"CABD", @"ACDB", @"DACB", @"BCDA", @"ACBD",
|
|
@"ADCB", @"CDAB", @"DABC", @"BCAD", @"CADB", @"CDBA",
|
|
@"CBAD", @"ABDC", @"ADBC", @"BDCA", @"DCBA", @"BACD",
|
|
@"BADC", @"BDAC", @"CBDA", @"DBCA", @"DCAB"]
|
|
|
|
CFMutableSetRef set1 = fn MutableSetWithArray( array1 )
|
|
CFSetRef set2 = fn SetWithArray( array2 )
|
|
MutableSetMinusSet( set1, set2 )
|
|
return fn SetAnyObject( set1 )
|
|
end fn = NULL
|
|
|
|
printf @"%@", fn DoPermutations
|
|
|
|
HandleEvents
|