Dutch national flag problem: Difference between revisions

Corrected logic mistake
(Initial FutureBasic task solution added)
(Corrected logic mistake)
Line 1,852:
FB has native sort functions ideal for this task.
<syntaxhighlight lang="furturebasic">
 
local fn SortBalls
CFArrayRef unsortedArray = @[@"🔴",@"⚪️",@"🔵",@"🔵",@"🔵",@"🔴",@"🔴",@"⚪️",@"⚪️",@"🔵",@"🔴",@"🔴",@"🔴",@"🔵",@"⚪️",@"🔴",@"🔴",@"🔵",@"⚪️"]
CFArrayRef sortedArrsortedArray = 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 sortedStringfirstColorString = fn ArrayComponentsJoinedByString( sortedArrfirstColorArray, @"" )
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 unsortedString thirdColorString = fn ArrayComponentsJoinedByString( unsortedArraymutArr, @"" )
printf @"Unsorted balls:\n\t%@\n", unsortedStringfn ArrayComponentsJoinedByString( unsortedArray, @"" )
printf @"Sorted balls:\n\t%@", sortedStringfn StringWithFormat( @"%@%@%@", secondColorString, firstColorString, thirdColorString )
end fn
 
fn SortBalls
 
HandleEvents
</syntaxhighlight>
{{output}}
Line 1,873 ⟶ 1,882:
 
Sorted balls:
🔴🔴🔴🔴🔴🔴🔴🔴⚪️⚪️⚪️⚪️⚪️🔵🔵🔵🔵🔵🔵
⚪️⚪️⚪️⚪️⚪️🔴🔴🔴🔴🔴🔴🔴🔴🔵🔵🔵🔵🔵🔵
</pre>
 
717

edits