Dutch national flag problem: Difference between revisions
Content deleted Content added
Improved example moved to main page |
|||
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"> |
||
_red = 0 |
|||
local fn SortBalls |
|||
_white = 1 |
|||
CFArrayRef unsortedArray = @[@"🔴",@"⚪️",@"🔵",@"🔵",@"🔵",@"🔴",@"🔴",@"⚪️",@"⚪️",@"🔵",@"🔴",@"🔴",@"🔴",@"🔵",@"⚪️",@"🔴",@"🔴",@"🔵",@"⚪️"] |
|||
_blue = 2 |
|||
CFArrayRef sortedArray = fn ArraySortedArrayUsingSelector( unsortedArray, @"localizedStandardCompare:" ) |
|||
_unsorted = 4 |
|||
NSUInteger i, index = 0, mark, count = len(sortedArray) |
|||
CFMutableArrayRef mutArr = fn MutableArrayWithArray( sortedArray ) |
|||
local fn SortRandomBalls |
|||
⚫ | |||
'~'1 |
|||
if fn StringIsEqual( mutArr[i], mutArr[i+1] ) then index++ else exit for |
|||
NSUInteger i |
|||
⚫ | |||
CFArrayRef ballColorsArr = @[@"🔴",@"⚪️",@"🔵"] |
|||
CFArrayRef firstColorArray = fn ArraySubarrayWithRange( mutArr, fn CFRangeMake( 0, index + 1 ) ) |
|||
CFStringRef firstColorString = fn ArrayComponentsJoinedByString( firstColorArray, @"" ) |
|||
⚫ | |||
MutableArrayRemoveObjectsInRange( mutArr, fn CFRangeMake( 0, index + 1 ) ) |
|||
short r = rnd(3)-1 |
|||
count = len(mutArr) : index = 0 |
|||
mda _unsorted(i) = ballColorsArr[r] |
|||
for i = 0 to count - 1 |
|||
if r == 0 then mda_add _red(i) = ballColorsArr[r] |
|||
if fn StringIsEqual( mutArr[i], mutArr[i+1] ) then index++ else exit for |
|||
if r == 1 then mda_add _white(i) = ballColorsArr[r] |
|||
next |
|||
if r == 2 then mda_add _blue(i) = ballColorsArr[r] |
|||
CFArrayRef secondColorArray = fn ArraySubarrayWithRange( mutArr, fn CFRangeMake( 0, index + 1 ) ) |
|||
⚫ | |||
CFStringRef secondColorString = fn ArrayComponentsJoinedByString( secondColorArray, @"" ) |
|||
CFStringRef unsorted = fn StringByReplacingOccurrencesOfString( mda_text _unsorted, @"\n", @"" ) |
|||
MutableArrayRemoveObjectsInRange( mutArr, fn CFRangeMake( 0, index + 1 ) ) |
|||
CFStringRef redStr = fn StringByReplacingOccurrencesOfString( mda_text _red, @"\n", @"" ) |
|||
CFStringRef whiteStr = fn StringByReplacingOccurrencesOfString( mda_text _white, @"\n", @"" ) |
|||
CFStringRef blueStr = fn StringByReplacingOccurrencesOfString( mda_text _blue, @"\n", @"" ) |
|||
printf @"Unsorted balls:\n\t%@\n", fn ArrayComponentsJoinedByString( unsortedArray, @"" ) |
|||
CFStringRef sorted = fn StringWithFormat( @"%@%@%@", redStr, whiteStr, blueStr ) |
|||
printf @"Unsorted balls:\n\t%@\n", unsorted |
|||
printf @"Sorted balls:\n\t%@", sorted |
|||
end fn |
end fn |
||
random |
|||
fn SortBalls |
|||
fn SortRandomBalls |
|||
NSLog( @"%@", fn WindowPrintViewString( 1 ) ) |
|||
HandleEvents |
HandleEvents |
||
Line 1,883: | Line 1,889: | ||
<pre> |
<pre> |
||
Unsorted balls: |
Unsorted balls: |
||
⚪️🔴🔵🔴🔴⚪️🔴🔴🔵🔵⚪️⚪️🔵🔴🔵🔵🔵🔴🔵⚪️ |
|||
🔴⚪️🔵🔵🔵🔴🔴⚪️⚪️🔵🔴🔴🔴🔵⚪️🔴🔴🔵⚪️ |
|||
Sorted balls: |
Sorted balls: |
||
🔴🔴🔴🔴🔴🔴🔴⚪️⚪️⚪️⚪️⚪️🔵🔵🔵🔵🔵🔵🔵🔵 |
|||
🔴🔴🔴🔴🔴🔴🔴🔴⚪️⚪️⚪️⚪️⚪️🔵🔵🔵🔵🔵🔵 |
|||
</pre> |
</pre> |
||