Alternade words: Difference between revisions
m
→{{header|Wren}}: Minor tidy
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 />
Testing a word for at least one vowel, and 'a-z' is necessary for this task.
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
static String
static {
alphabet =
for (int index = 0; index < 26;
alphabet += (char) (index +
boolean alphabetic(String string) {
if
}
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;
}
}
StringBuilder wordA = new
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>
<pre>
accost acs cot
annette ante net
apport apr pot
assist ass sit
choosy cos hoy
fabric fbi arc
george gog ere
strain sri tan
triode tid roe
troupe top rue
</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="
import "./set" for Set
import "./fmt" for Fmt
var wordList = "unixdict.txt" // local copy
|