Alternade words: Difference between revisions

m
m (→‎{{header|AppleScript}}: New sort handler URL, tidy-up, optimisation.)
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 4 users not shown)
Line 1,176:
truant tun rat
twirly til wry
</pre>
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,StdCtrls,SysUtils}}
 
Delphi-style problem solving, using standard Delphi
controls like the TStringList, which greatly simplifies
the solution.
 
<syntaxhighlight lang="Delphi">
unit Alternade;
 
interface
 
uses Classes,StdCtrls,SysUtils;
 
procedure FindAlternadeWords(Memo: TMemo);
 
implementation
 
var Dict: TStringList; {List holds dictionary}
 
function GetAlts(S: string; var Alt1,Alt2: string): boolean;
{Grab Alternades from string and test if they are valid }
var I: integer;
begin
Alt1:='';
Alt2:='';
{Copy every other letter into different string}
for I:=1 to Length(S) do
if (I mod 2)=0 then Alt2:=Alt2+S[I]
else Alt1:=Alt1+S[I];
{Check if the two strings are in the dictionary}
Result:=(Dict.IndexOf(Alt1)>=0) and (Dict.IndexOf(Alt2)>=0);
end;
 
procedure FindAlternadeWords(Memo: TMemo);
{test all words in the dictionary}
{And diplays the Alternade words in the Memo}
var I,Cnt: integer;
var Alt1,Alt2: string;
begin
Cnt:=0;
for I:=0 to Dict.Count-1 do
if (Length(Dict[I])>=6) and GetAlts(Dict[I], Alt1, Alt2) then
begin
Inc(Cnt);
Memo.Lines.Add(IntToStr(Cnt)+': '+Dict[I]+' '+Alt1+' '+Alt2);
end
end;
 
 
initialization
{Create/load dictionary}
Dict:=TStringList.Create;
Dict.LoadFromFile('unixdict.txt');
Dict.Sorted:=True;
finalization
Dict.Free;
end.
 
</syntaxhighlight>
 
{{out}}
<pre>
1: accost acs cot
2: accuse acs cue
3: afield ail fed
4: agleam ala gem
5: alcott act lot
6: allele all lee
7: allied ale lid
8: alpert apr let
9: ambient abet min
10: annette ante net
11: apport apr pot
12: ariadne aide ran
13: assist ass sit
14: battle btl ate
15: blaine ban lie
16: brenda bed rna
17: calliope clip aloe
18: choose cos hoe
19: choosy cos hoy
20: claire car lie
21: collude clue old
22: effete eft fee
23: fabric fbi arc
24: fealty fat ely
25: fluent fun let
26: forwent fret own
27: friend fin red
28: george gog ere
29: inroad ira nod
30: israel ire sal
31: jaunty jut any
32: joanne jan one
33: lounge lug one
34: oriole oil roe
35: oswald owl sad
36: parrot pro art
37: peoria poi era
38: pierre per ire
39: poodle pol ode
40: pounce puc one
41: racial rca ail
42: realty rat ely
43: sordid sri odd
44: spatial sail pta
45: sprain sri pan
46: strain sri tan
47: strait sri tat
48: sturdy sud try
49: sweaty set way
50: tattle ttl ate
51: theorem term hoe
52: though tog huh
53: throaty tray hot
54: triode tid roe
55: triune tin rue
56: troupe top rue
57: truant tun rat
58: twirly til wry
 
</pre>
 
Line 1,247 ⟶ 1,373:
twirly -> til * wry
</pre>
 
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: formatting io.encodings.ascii io.files kernel literals
Line 1,419 ⟶ 1,547:
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
local fn Words as CFArrayRef
CFURLRef url = fn URLWithString( @"https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt" )
Line 1,779 ⟶ 1,910:
 
=={{header|Java}}==
The 'userdict.txt' file is great, except it contains entries like 'mrs' and '1st'.<br />
<syntaxhighlight lang="java">import java.io.*;
Testing a word for at least one vowel, and 'a-z' is necessary for this task.
import java.util.*;
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
 
public class AlternadeWords {
public static void main(Stringchar[] argsvowels = "aeiouy".toCharArray() {;
static String try {alphabet;
 
Set<String> dictionary = new TreeSet<>();
static {
try (BufferedReader reader = new BufferedReader(new FileReader("unixdict.txt"))) {
alphabet = String line"";
for (int index = 0; index < 26; while ((line = reader.readLine()) != nullindex++)
alphabet += (char) (index + dictionary.add(line'a');
}
 
StringBuilder word1 = new StringBuilder();
boolean alphabetic(String string) {
StringBuilder word2 = new StringBuilder();
for (Stringchar wordcharacter : dictionarystring.toCharArray()) {
if int length = word(!alphabet.lengthcontains(String.valueOf(character)));
ifreturn (length < 6)false;
}
return true;
}
 
boolean containsVowel(String string) {
for (char vowel : vowels) {
if (string.contains(String.valueOf(vowel)))
return true;
}
return false;
}
 
void alternateWords() throws IOException {
Set<String> dictionary = new TreeSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader("unixdict.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
if (!alphabetic(line) || !containsVowel(line))
continue;
word1dictionary.setLengthadd(0line);
word2.setLength(0);
for (int i = 0; i < length; i += 2) {
word1.append(word.charAt(i));
if (i + 1 < length)
word2.append(word.charAt(i + 1));
}
String w1 = word1.toString();
String w2 = word2.toString();
if (dictionary.contains(w1) && dictionary.contains(w2))
System.out.printf("%-10s%-6s%s\n", word, w1, w2);
}
} catch (Exception e) {
StringBuilder wordA = new e.printStackTraceStringBuilder();
StringBuilder wordB = new StringBuilder();
for (String word : dictionary) {
int length = word.length();
if (length < 6)
continue;
wordA.setLength(0);
wordB.setLength(0);
for (int index = 0; index < length; index += 2) {
wordA.append(word.charAt(index));
if (index + 1 < length)
wordB.append(word.charAt(index + 1));
}
if (dictionary.contains(wordA.toString()))
if (dictionary.contains(wordB.toString()))
System.out.printf("%-15s%5s %s%n", word, wordA, wordB);
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
 
<pre>
{{out}}
accost acs cot
<pre style="height: 45ex">
accostaccuse acs cot acs cue
accuseafield acs cue ail fed
afieldagleam ail fed ala gem
agleamalcott ala gem act lot
alcottallele act lot all lee
alleleallied all lee ale lid
alliedalpert ale lid apr let
alpertambient apr let abet min
annette ante net
ambient abet min
apport apr pot
annette ante net
apportariadne apr pot aide ran
assist ass sit
ariadne aide ran
assistblaine ass sit ban lie
battlebrenda btl ate bed rna
blainecalliope ban lie clip aloe
brendachoose bed rna cos hoe
choosy cos hoy
calliope clip aloe
chooseclaire cos hoe car lie
choosycollude cos hoy clue old
claireeffete car lie eft fee
fabric fbi arc
collude clue old
effetefealty eft fee fat ely
fabricfluent fbi arc fun let
fealtyforwent fat ely fret own
fluentfriend fun let fin red
george gog ere
forwent fret own
friendinroad fin red ira nod
georgeisrael gog ere ire sal
inroadjaunty ira nod jut any
israeljoanne ire sal jan one
jauntylounge jut any lug one
joanneoriole jan one oil roe
loungeoswald lug one owl sad
orioleparrot oil roe pro art
oswaldpeoria owl sad poi era
parrotpierre pro art per ire
peoriapoodle poi era pol ode
pierrepounce per ire puc one
poodleracial pol ode rca ail
pouncerealty puc one rat ely
racialsordid rca ail sri odd
realtyspatial rat ely sail pta
sordidsprain sri odd sri pan
strain sri tan
spatial sail pta
sprainstrait sri pan sri tat
strainsturdy sri tan sud try
straitsweaty sri tat set way
sturdytheorem sud try term hoe
sweatythough set way tog huh
tattlethroaty ttl ate tray hot
triode tid roe
theorem term hoe
thoughtriune tog huh tin rue
troupe top rue
throaty tray hot
triodetruant tid roe tun rat
triunetwirly tin rue til wry
troupe top rue
truant tun rat
twirly til wry
</pre>
 
Line 2,121 ⟶ 2,278:
truant->{tun,rat}
twirly->{til,wry}</pre>
 
=={{header|MiniScript}}==
This implementation is for use with the [http://miniscript.org/MiniMicro Mini Micro] version of MiniScript. The command-line version does not include a HTTP library. Modify the declaration of wordList object to use the file class to read a local copy of the word list file.
<syntaxhighlight lang="miniscript">
alternateLetters = function(word, ix)
if ix != 0 then ix = 1
altWord = ""
for i in range(ix, word.len - 1, 2)
altWord += word[i]
end for
return altWord
end function
 
wordList = http.get("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt").split(char(10))
ctr = 0
for word in wordList
if word.len > 5 then
word1 = alternateLetters(word, 0)
word2 = alternateLetters(word, 1)
if wordList.indexOf(word1) != null and wordList.indexOf(word2) != null then
ctr += 1
print [ctr, word, char(18), word1, word2].join(" ")
end if
end if
end for
</syntaxhighlight>
{{out}}
<pre>
1 accost : acs cot
2 accuse : acs cue
3 afield : ail fed
4 agleam : ala gem
5 alcott : act lot
6 allele : all lee
7 allied : ale lid
8 alpert : apr let
9 ambient : abet min
10 annette : ante net
11 apport : apr pot
12 ariadne : aide ran
13 assist : ass sit
14 battle : btl ate
15 blaine : ban lie
16 brenda : bed rna
17 calliope : clip aloe
18 choose : cos hoe
19 choosy : cos hoy
20 claire : car lie
21 collude : clue old
22 effete : eft fee
23 fabric : fbi arc
24 fealty : fat ely
25 fluent : fun let
26 forwent : fret own
27 friend : fin red
28 george : gog ere
29 inroad : ira nod
30 israel : ire sal
31 jaunty : jut any
32 joanne : jan one
33 lounge : lug one
34 oriole : oil roe
35 oswald : owl sad
36 parrot : pro art
37 peoria : poi era
38 pierre : per ire
39 poodle : pol ode
40 pounce : puc one
41 racial : rca ail
42 realty : rat ely
43 sordid : sri odd
44 spatial : sail pta
45 sprain : sri pan
46 strain : sri tan
47 strait : sri tat
48 sturdy : sud try
49 sweaty : set way
50 tattle : ttl ate
51 theorem : term hoe
52 though : tog huh
53 throaty : tray hot
54 triode : tid roe
55 triune : tin rue
56 troupe : top rue
57 truant : tun rat
58 twirly : til wry
</pre>
 
=={{header|Nim}}==
Line 3,787 ⟶ 4,031:
{{libheader|Wren-set}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./set" for Set
import "./fmt" for Fmt
 
var wordList = "unixdict.txt" // local copy
9,485

edits