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
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
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
printf @"Unsorted balls:\n\t%@\n",
printf @"Sorted balls:\n\t%@",
end fn
</syntaxhighlight>
{{output}}
Line 1,873 ⟶ 1,882:
Sorted balls:
🔴🔴🔴🔴🔴🔴🔴🔴⚪️⚪️⚪️⚪️⚪️🔵🔵🔵🔵🔵🔵
</pre>
|