include "NSLog.incl" begin globals NSInteger maxPathLength = 0 NSInteger maxPathLengthCount = 0 CFMutableStringRef maxPathExample end globals void local fn SetUp maxPathExample = fn MutableStringWithCapacity( 500 ) CFArrayRef names = @[@"audino", @"bagon", @"baltoy", @"banette", @"bidoof", @"braviary", @"bronzor", @"carracosta", @"charmeleon", @"cresselia", @"croagunk", @"darmanitan", @"deino", @"emboar", @"emolga", @"exeggcute", @"gabite", @"girafarig", @"gulpin", @"haxorus", @"heatmor", @"heatran", @"ivysaur", @"jellicent", @"jumpluff", @"kangaskhan", @"kricketune", @"landorus", @"ledyba", @"loudred", @"lumineon", @"lunatone", @"machamp", @"magnezone", @"mamoswine", @"nosepass", @"petilil", @"pidgeotto", @"pikachu", @"pinsir", @"poliwrath", @"poochyena", @"porygon2", @"porygonz", @"registeel", @"relicanth", @"remoraid", @"rufflet", @"sableye", @"scolipede", @"scrafty", @"seaking", @"sealeo", @"silcoon", @"simisear", @"snivy", @"snorlax", @"spoink", @"starly", @"tirtouga", @"trapinch", @"treecko", @"tyrogue", @"vigoroth", @"vulpix", @"wailord", @"wartortle", @"whismur", @"wingull", @"yamask"] AppSetProperty( @"pokemonNames", names ) end fn void local fn Recursive( part as CFMutableArrayRef, offset as NSInteger ) NSInteger i if ( offset > maxPathLength ) maxPathLength = offset maxPathLengthCount = 1 else if ( offset == maxPathLength ) maxPathLengthCount++ MutableStringSetString( maxPathExample, @"" ) for i = 0 to offset - 1 if (i % 5 == 0) then MutableStringAppendString( maxPathExample, @"\n ") else MutableStringAppendString( maxPathExample, @" " ) MutableStringAppendString( maxPathExample, part[i] ) next end if CFStringRef previousPart = part[offset - 1] NSUInteger lastCharacterIndex = len(previousPart) - 1 unichar lastChar = fn StringcharacterAtIndex( previousPart, lastCharacterIndex ) for i = offset to fn ArrayCount( part ) - 1 if ( fn StringCharacterAtIndex( part[i], 0 ) == lastChar ) CFStringRef tmp = part[offset] part[offset] = part[i] part[i] = tmp fn Recursive( part, offset + 1 ) part[i] = part[offset] part[offset] = tmp end if next end fn void local fn LastLetterFirstLetter CFArrayRef names = fn AppProperty( @"pokemonNames" ) CFMutableArrayRef mutableNames = fn MutableArrayWithArray( names ) for NSInteger i = 0 to fn ArrayCount( mutableNames ) - 1 CFStringRef tmp = mutableNames[0] mutableNames[0] = mutableNames[i] mutableNames[i] = tmp fn Recursive( mutableNames, 1 ) mutableNames[i] = mutableNames[0] mutableNames[0] = tmp next CFTimeInterval t = fn CACurrentMediaTime MutableStringReplaceAllOccurrencesOfString( maxPathExample, @" ", @"\n" ) MutableStringReplaceAllOccurrencesOfString( maxPathExample, @"\n\n", @"\n" ) MutableStringReplaceAllOccurrencesOfString( maxPathExample, @"\n\n", @"\n" ) MutableStringReplaceAllOccurrencesOfString( maxPathExample, @"\n", @"\n\t" ) NSLog( @"Maximum path length: %ld", (long)maxPathLength ) NSLog( @"Paths of that length: %ld", (long)maxPathLengthCount ) NSLog( @"One solution:\n %@", maxPathExample ) NSLog( @"\nCompute time: %.3f ms", (fn CACurrentMediaTime-t)*1000 ) end fn fn SetUp fn LastLetterFirstLetter HandleEvents