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 |
CFArrayRef sortedArray = fn ArraySortedArrayUsingSelector( unsortedArray, @"localizedStandardCompare:" ) |
||
NSUInteger i, index = 0, mark, count = len(sortedArray) |
|||
⚫ | |||
CFMutableArrayRef mutArr = fn MutableArrayWithArray( sortedArray ) |
|||
⚫ | |||
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 ) ) |
|||
⚫ | |||
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 ) ) |
|||
⚫ | |||
printf @"Unsorted balls:\n\t%@\n", |
printf @"Unsorted balls:\n\t%@\n", fn ArrayComponentsJoinedByString( unsortedArray, @"" ) |
||
printf @"Sorted balls:\n\t%@", |
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> |
||