Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
49
Task/Anagrams/FutureBasic/anagrams-1.basic
Normal file
49
Task/Anagrams/FutureBasic/anagrams-1.basic
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn Dictionary as CFArrayRef
|
||||
CFURLRef url = fn URLFileURLWithPath( @"/usr/share/dict/words" )
|
||||
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
|
||||
end fn = fn StringComponentsSeparatedByString( string, @"\n" )
|
||||
|
||||
local fn IsAnagram( wrd1 as CFStringRef, wrd2 as CFStringRef ) as BOOL
|
||||
NSUInteger i
|
||||
BOOL result = NO
|
||||
|
||||
if ( len(wrd1) != len(wrd2) ) then exit fn
|
||||
if ( fn StringCompare( wrd1, wrd2 ) == NSOrderedSame ) then exit fn
|
||||
CFMutableArrayRef mutArr1 = fn MutableArrayWithCapacity(0) : CFMutableArrayRef mutArr2 = fn MutableArrayWithCapacity(0)
|
||||
for i = 0 to len(wrd1) - 1
|
||||
MutableArrayAddObject( mutArr1, fn StringWithFormat( @"%C", fn StringCharacterAtIndex( wrd1, i ) ) )
|
||||
MutableArrayAddObject( mutArr2, fn StringWithFormat( @"%C", fn StringCharacterAtIndex( wrd2, i ) ) )
|
||||
next
|
||||
SortDescriptorRef sd = fn SortDescriptorWithKeyAndSelector( NULL, YES, @"caseInsensitiveCompare:" )
|
||||
if ( fn ArrayIsEqual( fn ArraySortedArrayUsingDescriptors( mutArr1, @[sd] ), fn ArraySortedArrayUsingDescriptors( mutArr2, @[sd] ) ) ) then result = YES
|
||||
end fn = result
|
||||
|
||||
void local fn FindAnagramsInDictionary( wd as CFStringRef, dict as CFArrayRef )
|
||||
CFStringRef string, temp
|
||||
|
||||
CFMutableArrayRef words = fn MutableArrayWithCapacity(0)
|
||||
for temp in dict
|
||||
if ( fn IsAnagram( lcase( wd ), temp ) ) then MutableArrayAddObject( words, temp )
|
||||
next
|
||||
string = fn ArrayComponentsJoinedByString( words, @", " )
|
||||
NSLogSetTextColor( fn ColorText ) : NSLog( @"Anagrams for %@:", lcase(wd) )
|
||||
NSLogSetTextColor( fn ColorSystemBlue ) : NSLog(@"%@\n",string)
|
||||
end fn
|
||||
|
||||
void local fn DoIt
|
||||
CFArrayRef dictionary = fn Dictionary
|
||||
|
||||
dispatchglobal
|
||||
CFStringRef string
|
||||
CFArrayRef words = @[@"bade",@"abet",@"beast",@"tuba",@"mace",@"scare",@"marine",@"antler",@"spare",@"leading",@"alerted",@"allergy",@"research",@"hustle",@"oriental",@"creationism",@"resistance",@"mountaineer"]
|
||||
for string in words
|
||||
fn FindAnagramsInDictionary( string, dictionary )
|
||||
next
|
||||
dispatchend
|
||||
end fn
|
||||
|
||||
fn DoIt
|
||||
|
||||
HandleEvents
|
||||
76
Task/Anagrams/FutureBasic/anagrams-2.basic
Normal file
76
Task/Anagrams/FutureBasic/anagrams-2.basic
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
|
||||
|
||||
local fn Dictionary as CFArrayRef
|
||||
CFURLRef url = fn URLWithString( @"http://wiki.puzzlers.org/pub/wordlists/unixdict.txt" )
|
||||
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
|
||||
end fn = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
|
||||
|
||||
local fn TestIndexes( array as CFArrayRef, obj as CFTypeRef, index as NSUInteger, stp as ^BOOL, userData as ptr ) as BOOL
|
||||
end fn = fn StringIsEqual( obj, userData )
|
||||
|
||||
void local fn IndexSetEnumerator( set as IndexSetRef, index as NSUInteger, stp as ^BOOL, userData as ptr )
|
||||
NSLog(@"\t%@\b",fn ArrayObjectAtIndex( userData, index ))
|
||||
end fn
|
||||
|
||||
void local fn DoIt
|
||||
CFArrayRef words
|
||||
CFMutableArrayRef sortedWords, letters
|
||||
CFStringRef string, sortedString
|
||||
IndexSetRef indexes
|
||||
long i, j, count, indexCount, maxCount = 0, length
|
||||
CFMutableDictionaryRef anagrams
|
||||
CFTimeInterval ti
|
||||
|
||||
ti = fn CACurrentMediaTime
|
||||
|
||||
NSLog(@"Searching...")
|
||||
|
||||
// create another word list with sorted letters
|
||||
words = fn Dictionary
|
||||
count = len(words)
|
||||
sortedWords = fn MutableArrayWithCapacity(count)
|
||||
for string in words
|
||||
length = len(string)
|
||||
letters = fn MutableArrayWithCapacity(length)
|
||||
for i = 0 to length - 1
|
||||
MutableArrayAddObject( letters, mid(string,i,1) )
|
||||
next
|
||||
MutableArraySortUsingSelector( letters, @"compare:" )
|
||||
sortedString = fn ArrayComponentsJoinedByString( letters, @"" )
|
||||
MutableArrayAddObject( sortedWords, sortedString )
|
||||
next
|
||||
|
||||
// search for identical sorted words
|
||||
anagrams = fn MutableDictionaryWithCapacity(0)
|
||||
for i = 0 to count - 2
|
||||
j = i + 1
|
||||
indexes = fn ArrayIndexesOfObjectsAtIndexesPassingTest( sortedWords, fn IndexSetWithIndexesInRange( fn CFRangeMake(j,count-j) ), NSEnumerationConcurrent, @fn TestIndexes, (ptr)sortedWords[i] )
|
||||
indexCount = len(indexes)
|
||||
if ( indexCount > maxCount )
|
||||
maxCount = indexCount
|
||||
MutableDictionaryRemoveAllObjects( anagrams )
|
||||
end if
|
||||
if ( indexCount == maxCount )
|
||||
MutableDictionarySetValueForKey( anagrams, indexes, words[i] )
|
||||
end if
|
||||
next
|
||||
|
||||
// show results
|
||||
NSLogClear
|
||||
for string in anagrams
|
||||
NSLog(@"%@\b",string)
|
||||
indexes = anagrams[string]
|
||||
IndexSetEnumerateIndexes( indexes, @fn IndexSetEnumerator, (ptr)words )
|
||||
NSLog(@"")
|
||||
next
|
||||
|
||||
NSLog(@"\nCalculated in %0.6fs",fn CACurrentMediaTime - ti)
|
||||
end fn
|
||||
|
||||
dispatchglobal
|
||||
fn DoIt
|
||||
dispatchend
|
||||
|
||||
HandleEvents
|
||||
Loading…
Add table
Add a link
Reference in a new issue