Anagrams: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) m (→{{header|11l}}: `sorted(String)` now returns `String`, not `Array`) |
|||
Line 3,997: | Line 3,997: | ||
This first example is a hybrid using FB's native dynamic global array combined with Core Foundation functions: |
This first example is a hybrid using FB's native dynamic global array combined with Core Foundation functions: |
||
<syntaxhighlight lang="futurebasic"> |
<syntaxhighlight lang="futurebasic"> |
||
include " |
include "NSLog.incl" |
||
local fn Dictionary as CFArrayRef |
|||
def tab 9 |
|||
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 |
|||
begin globals |
|||
NSUInteger i |
|||
dim dynamic gDictionary(_maxLong) as Str255 |
|||
BOOL result = NO |
|||
end globals |
|||
if ( len(wrd1) != len(wrd2) ) then exit fn |
|||
local fn IsAnagram( word1 as Str31, word2 as Str31 ) as Boolean |
|||
if ( fn StringCompare( wrd1, wrd2 ) == NSOrderedSame ) then exit fn |
|||
dim as long i, j, h, q |
|||
CFMutableArrayRef mutArr1 = fn MutableArrayWithCapacity(0) : CFMutableArrayRef mutArr2 = fn MutableArrayWithCapacity(0) |
|||
dim as Boolean result |
|||
for i = 0 to len(wrd1) - 1 |
|||
MutableArrayAddObject( mutArr1, fn StringWithFormat( @"%C", fn StringCharacterAtIndex( wrd1, i ) ) ) |
|||
if word1[0] != word2[0] then result = _false : exit fn |
|||
MutableArrayAddObject( mutArr2, fn StringWithFormat( @"%C", fn StringCharacterAtIndex( wrd2, i ) ) ) |
|||
for i = 0 to word1[0] |
|||
h = 0 : q = 0 |
|||
for j = 0 to word1[0] |
|||
if word1[i] == word1[j] then h++ |
|||
if word1[i] == word2[j] then q++ |
|||
next |
next |
||
SortDescriptorRef sd = fn SortDescriptorWithKeyAndSelector( NULL, YES, @"caseInsensitiveCompare:" ) |
|||
if h != q then result = _false : exit fn |
|||
if ( fn ArrayIsEqual( fn ArraySortedArrayUsingDescriptors( mutArr1, @[sd] ), fn ArraySortedArrayUsingDescriptors( mutArr2, @[sd] ) ) ) then result = YES |
|||
next |
|||
result = _true |
|||
end fn = result |
end fn = result |
||
void local fn FindAnagramsInDictionary( wd as CFStringRef, dict as CFArrayRef ) |
|||
local fn LoadDictionaryToArray |
|||
CFStringRef string, temp |
|||
'~'1 |
|||
dim as CFURLRef url |
|||
CFMutableArrayRef words = fn MutableArrayWithCapacity(0) |
|||
dim as CFArrayRef arr |
|||
for temp in dict |
|||
if ( fn IsAnagram( lcase( wd ), temp ) ) then MutableArrayAddObject( words, temp ) |
|||
dim as CFIndex elements |
|||
next |
|||
dim as Handle h |
|||
string = fn ArrayComponentsJoinedByString( words, @", " ) |
|||
dim as Str255 s |
|||
NSLogSetTextColor( fn ColorText ) : NSLog( @"Anagrams for %@:", lcase(wd) ) |
|||
dim as long fileLen, i |
|||
NSLogSetTextColor( fn ColorSystemBlue ) : NSLog(@"%@\n",string) |
|||
kill dynamic gDictionary |
|||
url = fn CFURLCreateWithFileSystemPath( _kCFAllocatorDefault, @"/usr/share/dict/words", _kCFURLPOSIXPathStyle, _false ) |
|||
open "i", 2, url |
|||
fileLen = lof(2, 1) |
|||
h = fn NewHandleClear( fileLen ) |
|||
if ( h ) |
|||
read file 2, [h], fileLen |
|||
cfStr = fn CFStringCreateWithBytes( _kCFAllocatorDefault, #[h], fn GetHandleSize(h), _kCFStringEncodingMacRoman, _false ) |
|||
if ( cfStr ) |
|||
arr = fn CFStringCreateArrayBySeparatingStrings( _kCFAllocatorDefault, cfStr, @"\n" ) |
|||
CFRelease( cfStr ) |
|||
elements = fn CFArrayGetCount( arr ) |
|||
for i = 0 to elements - 1 |
|||
temp = fn CFArrayGetValueAtIndex( arr, i ) |
|||
fn CFStringGetPascalString( temp, @s, 256, _kCFStringEncodingMacRoman ) |
|||
gDictionary(i) = s |
|||
next |
|||
CFRelease( arr ) |
|||
end if |
|||
fn DisposeH( h ) |
|||
end if |
|||
close #2 |
|||
CFRelease( url ) |
|||
end fn |
end fn |
||
void local fn DoIt |
|||
local fn FindAnagrams( whichWord as Str31 ) |
|||
CFArrayRef dictionary = fn Dictionary |
|||
dim as long elements, i |
|||
dispatchglobal |
|||
print "Anagrams for "; UCase$(whichWord); ":", |
|||
CFStringRef string |
|||
elements = fn DynamicNextElement( dynamic( gDictionary ) ) |
|||
CFArrayRef words = @[@"bade",@"abet",@"beast",@"tuba",@"mace",@"scare",@"marine",@"antler",@"spare",@"leading",@"alerted",@"allergy",@"research",@"hustle",@"oriental",@"creationism",@"resistance",@"mountaineer"] |
|||
for i = 0 to elements - 1 |
|||
for string in words |
|||
if ( len( gDictionary(i) ) == whichWord[0] ) |
|||
fn FindAnagramsInDictionary( string, dictionary ) |
|||
if ( fn IsAnagram( whichWord, gDictionary(i) ) == _true ) |
|||
next |
|||
print gDictionary(i), |
|||
dispatchend |
|||
end if |
|||
end if |
|||
next |
|||
print |
|||
end fn |
end fn |
||
fn DoIt |
|||
fn LoadDictionaryToArray |
|||
HandleEvents |
|||
fn FindAnagrams( "bade" ) |
|||
fn FindAnagrams( "abet" ) |
|||
fn FindAnagrams( "beast" ) |
|||
fn FindAnagrams( "tuba" ) |
|||
fn FindAnagrams( "mace" ) |
|||
fn FindAnagrams( "scare" ) |
|||
fn FindAnagrams( "marine" ) |
|||
fn FindAnagrams( "antler" ) |
|||
fn FindAnagrams( "spare" ) |
|||
fn FindAnagrams( "leading" ) |
|||
fn FindAnagrams( "alerted" ) |
|||
fn FindAnagrams( "allergy" ) |
|||
fn FindAnagrams( "research") |
|||
fn FindAnagrams( "hustle" ) |
|||
fn FindAnagrams( "oriental") |
|||
def tab 3 |
|||
print |
|||
fn FindAnagrams( "creationism" ) |
|||
fn FindAnagrams( "resistance" ) |
|||
fn FindAnagrams( "mountaineer" ) |
|||
</syntaxhighlight> |
</syntaxhighlight> |
||
Output: |
Output: |