Dutch national flag problem: Difference between revisions

Content added Content deleted
(Initial FutureBasic task solution added)
(Corrected logic mistake)
Line 1,852: Line 1,852:
FB has native sort functions ideal for this task.
FB has native sort functions ideal for this task.
<syntaxhighlight lang="furturebasic">
<syntaxhighlight lang="furturebasic">

local fn SortBalls
local fn SortBalls
CFArrayRef unsortedArray = @[@"🔴",@"⚪️",@"🔵",@"🔵",@"🔵",@"🔴",@"🔴",@"⚪️",@"⚪️",@"🔵",@"🔴",@"🔴",@"🔴",@"🔵",@"⚪️",@"🔴",@"🔴",@"🔵",@"⚪️"]
CFArrayRef unsortedArray = @[@"🔴",@"⚪️",@"🔵",@"🔵",@"🔵",@"🔴",@"🔴",@"⚪️",@"⚪️",@"🔵",@"🔴",@"🔴",@"🔴",@"🔵",@"⚪️",@"🔴",@"🔴",@"🔵",@"⚪️"]
CFArrayRef sortedArr = fn ArraySortedArrayUsingSelector( unsortedArray, @"localizedStandardCompare:" )
CFArrayRef sortedArray = fn ArraySortedArrayUsingSelector( unsortedArray, @"localizedStandardCompare:" )
NSUInteger i, index = 0, mark, count = len(sortedArray)
CFStringRef unsortedString = fn ArrayComponentsJoinedByString( unsortedArray, @"" )
CFMutableArrayRef mutArr = fn MutableArrayWithArray( sortedArray )
CFStringRef sortedString = fn ArrayComponentsJoinedByString( sortedArr, @"" )
for i = 0 to count - 1
if fn StringIsEqual( mutArr[i], mutArr[i+1] ) then index++ else exit for
next
CFArrayRef firstColorArray = fn ArraySubarrayWithRange( mutArr, fn CFRangeMake( 0, index + 1 ) )
CFStringRef firstColorString = fn ArrayComponentsJoinedByString( firstColorArray, @"" )
MutableArrayRemoveObjectsInRange( mutArr, fn CFRangeMake( 0, index + 1 ) )
count = len(mutArr) : index = 0
for i = 0 to count - 1
if fn StringIsEqual( mutArr[i], mutArr[i+1] ) then index++ else exit for
next
CFArrayRef secondColorArray = fn ArraySubarrayWithRange( mutArr, fn CFRangeMake( 0, index + 1 ) )
CFStringRef secondColorString = fn ArrayComponentsJoinedByString( secondColorArray, @"" )
MutableArrayRemoveObjectsInRange( mutArr, fn CFRangeMake( 0, index + 1 ) )
CFStringRef thirdColorString = fn ArrayComponentsJoinedByString( mutArr, @"" )
printf @"Unsorted balls:\n\t%@\n", unsortedString
printf @"Unsorted balls:\n\t%@\n", fn ArrayComponentsJoinedByString( unsortedArray, @"" )
printf @"Sorted balls:\n\t%@", sortedString
printf @"Sorted balls:\n\t%@", fn StringWithFormat( @"%@%@%@", secondColorString, firstColorString, thirdColorString )
end fn
end fn

fn SortBalls

HandleEvents
</syntaxhighlight>
</syntaxhighlight>
{{output}}
{{output}}
Line 1,873: Line 1,882:


Sorted balls:
Sorted balls:
🔴🔴🔴🔴🔴🔴🔴🔴⚪️⚪️⚪️⚪️⚪️🔵🔵🔵🔵🔵🔵
⚪️⚪️⚪️⚪️⚪️🔴🔴🔴🔴🔴🔴🔴🔴🔵🔵🔵🔵🔵🔵
</pre>
</pre>