Changeable words: Difference between revisions

no edit summary
No edit summary
 
(15 intermediate revisions by 11 users not shown)
Line 18:
* [[Levenshtein distance]]
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
{{trans|Go}}
 
<langsyntaxhighlight lang="11l">V words = File(‘unixdict.txt’).read().split("\n").filter(word -> word.len > 11)
 
F hamming_dist(word1, word2)
Line 46 ⟶ 45:
count += 2
 
print("\nFound "count‘ changeable words.’)</langsyntaxhighlight>
 
{{out}}
Line 81 ⟶ 80:
Found 52 changeable words.
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Strings.Fixed;
with Ada.Containers.Indefinite_Vectors;
Line 134 ⟶ 132:
end loop;
end loop;
end Changeable;</langsyntaxhighlight>
{{out}}
<pre>
Line 140 ⟶ 138:
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeewoman <-> committeewomen
committeeperson <-> committeepeople
complementary <-> complimentary
confirmation <-> conformation
congresswoman <-> congresswomen
congressperson <-> congresspeople
councilpersoncouncilwoman <-> councilpeoplecouncilwomen
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
Line 164 ⟶ 162:
upperclassman <-> upperclassmen
</pre>
=={{header|ALGOL 68}}==
Brute force - tries each possible changed word.
<syntaxhighlight lang="algol68">
# find words where changing one letter results in another word #
# only words of 12 or more characters are to be considered #
IF FILE input file;
STRING file name = "unixdict.txt";
open( input file, file name, stand in channel ) /= 0
THEN
# failed to open the file #
print( ( "Unable to open """ + file name + """", newline ) )
ELSE
# file opened OK #
BOOL at eof := FALSE;
# set the EOF handler for the file - notes eof has been reached and #
# returns TRUE so processing can continue #
on logical file end( input file, ( REF FILE f )BOOL: at eof := TRUE );
 
# table of possible words - there are around 1000 12+ character words #
[ 1 : 2 000 ]STRING words; # in unixdict.txt #
 
# in-place quick sort an array of strings #
PROC s quicksort = ( REF[]STRING a, INT lb, ub )VOID:
IF ub > lb
THEN
# more than one element, so must sort #
INT left := lb;
INT right := ub;
# choosing the middle element of the array as the pivot #
STRING pivot := a[ left + ( ( right + 1 ) - left ) OVER 2 ];
WHILE
WHILE IF left <= ub THEN a[ left ] < pivot ELSE FALSE FI
DO
left +:= 1
OD;
WHILE IF right >= lb THEN a[ right ] > pivot ELSE FALSE FI
DO
right -:= 1
OD;
left <= right
DO
STRING t := a[ left ];
a[ left ] := a[ right ];
a[ right ] := t;
left +:= 1;
right -:= 1
OD;
s quicksort( a, lb, right );
s quicksort( a, left, ub )
FI # s quicksort # ;
 
# returns the length of s #
OP LENGTH = ( STRING s )INT: 1 + ( UPB s - LWB s );
 
# returns TRUE if words[ low : high ] contains s, FALSE otherwise #
PROC is word = ( STRING s, INT low, high )BOOL:
IF high < low THEN FALSE
ELSE INT mid = ( low + high ) OVER 2;
IF words[ mid ] > s THEN is word( s, low, mid - 1 )
ELIF words[ mid ] = s THEN TRUE
ELSE is word( s, mid + 1, high )
FI
FI # is word # ;
 
INT w count := 0; # store the 12 character words #
WHILE
STRING word;
get( input file, ( word, newline ) );
NOT at eof
DO
IF LENGTH word > 11 THEN
words[ w count +:= 1 ] := word
FI
OD;
close( input file );
s quicksort( words, 1, w count ); # sort the words #
FOR i TO w count DO # find the changeable words #
STRING word = words[ i ];
STRING c word := word;
FOR i FROM LWB word TO UPB word DO
FOR c FROM ABS word[ i ] + 1 TO ABS "z" DO
c word[ i ] := REPR c;
IF is word( c word, 1, w count ) THEN
FOR p FROM LENGTH word TO 15 DO print( ( " " ) ) OD;
print( ( word, " <-> ", c word, newline ) )
FI
OD;
c word[ i ] := word[ i ]
OD
OD
FI
</syntaxhighlight>
{{out}}
<pre>
aristotelean <-> aristotelian
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeewoman <-> committeewomen
complementary <-> complimentary
confirmation <-> conformation
congresswoman <-> congresswomen
councilwoman <-> councilwomen
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
frontiersman <-> frontiersmen
handicraftsman <-> handicraftsmen
incommutable <-> incomputable
installation <-> instillation
kaleidescope <-> kaleidoscope
neuroanatomy <-> neuroanotomy
newspaperman <-> newspapermen
nonagenarian <-> nonogenarian
onomatopoeia <-> onomatopoeic
philanthrope <-> philanthropy
prescription <-> proscription
schizophrenia <-> schizophrenic
shakespearean <-> shakespearian
spectroscope <-> spectroscopy
underclassman <-> underclassmen
upperclassman <-> upperclassmen
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">wordset: map read.lines relative "unixdict.txt" => strip
 
wordset: select wordset 'word -> 12 =< size word
results: new []
loop wordset 'a [
loop select wordset 'word [equal? size a size word] 'b [
if a <> b [
if 1 = levenshtein a b [
'results ++ @[sort @[a b]]
]
]
]
]
 
loop unique results 'result ->
print join.with:" - " result</syntaxhighlight>
 
{{out}}
 
<pre>aristotelean - aristotelian
claustrophobia - claustrophobic
committeeman - committeemen
committeewoman - committeewomen
complementary - complimentary
confirmation - conformation
congresswoman - congresswomen
councilwoman - councilwomen
craftsperson - draftsperson
eavesdropped - eavesdropper
frontiersman - frontiersmen
handicraftsman - handicraftsmen
incommutable - incomputable
installation - instillation
kaleidescope - kaleidoscope
neuroanatomy - neuroanotomy
newspaperman - newspapermen
nonagenarian - nonogenarian
onomatopoeia - onomatopoeic
philanthrope - philanthropy
prescription - proscription
schizophrenia - schizophrenic
shakespearean - shakespearian
spectroscope - spectroscopy
underclassman - underclassmen
upperclassman - upperclassmen</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">FileRead, db, % A_Desktop "\unixdict.txt"
oWord := [], Found := []
for i, word in StrSplit(db, "`n", "`r")
if (StrLen(word) > 11)
oWord[word] := true
 
for word1 in oWord
for word2 in oWord
if Changeable(word1, word2) && !Found[word2]
found[word1] := true, result .= word1 . "`t<-> " word2 "`n"
 
MsgBox, 262144, , % result
return
 
Changeable(s1, s2) {
if (StrLen(s1) <> StrLen(s2))
return 0
for i, v in StrSplit(s1)
if (v = SubStr(s2, i, 1))
num++
return (StrLen(s1) - num = 1)
}</syntaxhighlight>
{{out}}
<pre>aristotelean <-> aristotelian
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeewoman <-> committeewomen
complementary <-> complimentary
confirmation <-> conformation
congresswoman <-> congresswomen
councilwoman <-> councilwomen
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
frontiersman <-> frontiersmen
handicraftsman <-> handicraftsmen
incommutable <-> incomputable
installation <-> instillation
kaleidescope <-> kaleidoscope
neuroanatomy <-> neuroanotomy
newspaperman <-> newspapermen
nonagenarian <-> nonogenarian
onomatopoeia <-> onomatopoeic
philanthrope <-> philanthropy
prescription <-> proscription
schizophrenia <-> schizophrenic
shakespearean <-> shakespearian
spectroscope <-> spectroscopy
underclassman <-> underclassmen
upperclassman <-> upperclassmen</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f CHANGEABLE_WORDS.AWK unixdict.txt
#
Line 213 ⟶ 430:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 222 ⟶ 439:
committeeman committeemen
committeemen committeeman
committeewoman committeewomen
committeeperson committeepeople
committeewomen committeewoman
committeepeople committeeperson
complementary complimentary
complimentary complementary
confirmation conformation
conformation confirmation
congresswoman congresswomen
congressperson congresspeople
congresswomen congresswoman
congresspeople congressperson
councilwoman councilwomen
councilperson councilpeople
councilwomen councilwoman
councilpeople councilperson
craftsperson draftsperson
draftsperson craftsperson
Line 271 ⟶ 488:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 356 ⟶ 573:
free(dictionary);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 367 ⟶ 584:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 414 ⟶ 631:
52: upperclassmen -> upperclassman
</pre>
 
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iomanip>
Line 464 ⟶ 680:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 475 ⟶ 691:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 521 ⟶ 737:
51: upperclassman -> upperclassmen
52: upperclassmen -> upperclassman
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
var Dict: TStringList; {List holds dictionary}
 
 
procedure DoChangeWords(Memo: TMemo);
{Do change word problem}
var I,J,K,Inx,Cnt: integer;
var S: string;
 
procedure Display(OldStr,NewStr: string);
{Display both the original word and the modified word}
begin
Inc(Cnt);
Memo.Lines.Add(IntToStr(Cnt)+': '+OldStr+' '+NewStr);
end;
 
begin
Cnt:=0;
{Check every word in the dictionary}
for I:=0 to Dict.Count-1 do
begin
{Only check words at least 12 chars long}
if Length(Dict[I])>=12 then
begin
{Test a specific position in the word}
for J:=1 to Length(Dict[I]) do
begin
{try all combinations of alpha chars in the position}
for K:=byte('a') to byte('z') do
begin
{skip if the char is already in the position}
if Dict[I][J]=char(K) then continue;
{Get a new copy of the word to modify}
S:=Dict[I];
{Modify the position}
S[J]:=char(K);
{Is it in the dictionary? }
Inx:=Dict.IndexOf(S);
{Display it if it is}
if Inx>=0 then Display(Dict[I],S);
end;
end;
end;
end;
end;
 
 
initialization
{Create/load dictionary}
Dict:=TStringList.Create;
Dict.LoadFromFile('unixdict.txt');
Dict.Sorted:=True;
finalization
Dict.Free;
end.
</syntaxhighlight>
{{out}}
<pre>
 
1: aristotelean aristotelian
2: aristotelian aristotelean
3: claustrophobia claustrophobic
4: claustrophobic claustrophobia
5: committeeman committeemen
6: committeemen committeeman
7: committeewoman committeewomen
8: committeewomen committeewoman
9: complementary complimentary
10: complimentary complementary
11: confirmation conformation
12: conformation confirmation
13: congresswoman congresswomen
14: congresswomen congresswoman
15: councilwoman councilwomen
16: councilwomen councilwoman
17: craftsperson draftsperson
18: draftsperson craftsperson
19: eavesdropped eavesdropper
20: eavesdropper eavesdropped
21: frontiersman frontiersmen
22: frontiersmen frontiersman
23: handicraftsman handicraftsmen
24: handicraftsmen handicraftsman
25: incommutable incomputable
26: incomputable incommutable
27: installation instillation
28: instillation installation
29: kaleidescope kaleidoscope
30: kaleidoscope kaleidescope
31: neuroanatomy neuroanotomy
32: neuroanotomy neuroanatomy
33: newspaperman newspapermen
34: newspapermen newspaperman
35: nonagenarian nonogenarian
36: nonogenarian nonagenarian
37: onomatopoeia onomatopoeic
38: onomatopoeic onomatopoeia
39: philanthrope philanthropy
40: philanthropy philanthrope
41: prescription proscription
42: proscription prescription
43: schizophrenia schizophrenic
44: schizophrenic schizophrenia
45: shakespearean shakespearian
46: shakespearian shakespearean
47: spectroscope spectroscopy
48: spectroscopy spectroscope
49: underclassman underclassmen
50: underclassmen underclassman
51: upperclassman upperclassmen
52: upperclassmen upperclassman
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Changeable words: Nigel Galloway. June 14th., 2021
let fN g=Seq.fold2(fun z n g->z+if n=g then 0 else 1) 0 g
Line 530 ⟶ 865:
seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter(fun n->n.Length>11)
|>List.ofSeq|>List.groupBy(fun n->n.Length)|>Seq.collect(fun(_,n)->fG [] n)|>Seq.iter(fun(n,g)->printfn "%s <=> %s" n g)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 562 ⟶ 897:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: assocs combinators.short-circuit formatting
io.encodings.ascii io.files kernel math math.combinatorics
math.distances sequences ;
Line 572 ⟶ 907:
[ length 11 > ] filter
2 [ first2 changeable? ] filter-combinations
[ "%s <-> %s\n" printf ] assoc-each</langsyntaxhighlight>
{{out}}
<pre>
Line 578 ⟶ 913:
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeewoman <-> committeewomen
committeeperson <-> committeepeople
complementary <-> complimentary
confirmation <-> conformation
congresswoman <-> congresswomen
congressperson <-> congresspeople
councilwoman <-> councilwomen
councilperson <-> councilpeople
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
Line 602 ⟶ 937:
upperclassman <-> upperclassmen
</pre>
 
=={{header|FreeBASIC}}==
Brute force method, reuses some code from [[Odd_words#FreeBASIC]].
<langsyntaxhighlight lang="freebasic">
#define NULL 0
 
Line 661 ⟶ 995:
next i
curr = curr->nxt
wend</langsyntaxhighlight>
{{out}}
<pre>
Line 670 ⟶ 1,004:
committeeman ---> committeemen
committeemen ---> committeeman
committeepersoncommitteewoman ---> committeepeoplecommitteewomen
committeepeoplecommitteewomen ---> committeepersoncommitteewoman
complementary ---> complimentary
complimentary ---> complementary
confirmation ---> conformation
conformation ---> confirmation
congresspersoncongresswoman ---> congresspeoplecongresswomen
congresspeoplecongresswomen ---> congresspersoncongresswoman
councilpersoncouncilwoman ---> councilpeoplecouncilwomen
councilpeoplecouncilwomen ---> councilpersoncouncilwoman
craftsperson ---> draftsperson
draftsperson ---> craftsperson
Line 716 ⟶ 1,050:
upperclassman ---> upperclassmen
upperclassmen ---> upperclassman</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
include "NSLog.incl"
 
local fn Words as CFArrayRef
CFURLRef url = fn URLWithString( @"http://wiki.puzzlers.org/pub/wordlists/unixdict.txt" )
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
CFArrayRef array = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
CFMutableArrayRef words = fn MutableArrayWithCapacity(0)
for string in array
if ( len(string) > 11 )
MutableArrayAddObject( words, string )
end if
next
end fn = words
 
void local fn DoIt
dispatchglobal
CFArrayRef words = fn Words
CFStringRef wd1, wd2
long length, i, j
unichar chr
for wd1 in words
length = len(wd1)
for i = 0 to length - 1
chr = fn StringCharacterAtIndex( wd1, i )
for j = 97 to 122
if ( j == chr ) then continue
wd2 = fn StringWithFormat( @"%@%c%@", left(wd1,i), j, right(wd1,length-i-1) )
if ( fn ArrayContainsObject( words, wd2 ) )
NSLog(@"%@\t%@",wd1,wd2)
end if
next
next
next
dispatchend
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
 
{{out}}
<pre style="height:30ex">
aristotelean aristotelian
aristotelian aristotelean
claustrophobia claustrophobic
claustrophobic claustrophobia
committeeman committeemen
committeemen committeeman
committeewoman committeewomen
committeewomen committeewoman
complementary complimentary
complimentary complementary
confirmation conformation
conformation confirmation
congresswoman congresswomen
congresswomen congresswoman
councilwoman councilwomen
councilwomen councilwoman
craftsperson draftsperson
draftsperson craftsperson
eavesdropped eavesdropper
eavesdropper eavesdropped
frontiersman frontiersmen
frontiersmen frontiersman
handicraftsman handicraftsmen
handicraftsmen handicraftsman
incommutable incomputable
incomputable incommutable
installation instillation
instillation installation
kaleidescope kaleidoscope
kaleidoscope kaleidescope
neuroanatomy neuroanotomy
neuroanotomy neuroanatomy
newspaperman newspapermen
newspapermen newspaperman
nonagenarian nonogenarian
nonogenarian nonagenarian
onomatopoeia onomatopoeic
onomatopoeic onomatopoeia
philanthrope philanthropy
philanthropy philanthrope
prescription proscription
proscription prescription
schizophrenia schizophrenic
schizophrenic schizophrenia
shakespearean shakespearian
shakespearian shakespearean
spectroscope spectroscopy
spectroscopy spectroscope
underclassman underclassmen
underclassmen underclassman
upperclassman upperclassmen
upperclassmen upperclassman
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 771 ⟶ 1,209:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 782 ⟶ 1,220:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 829 ⟶ 1,267:
52: upperclassmen -> upperclassman
</pre>
=={{header|J}}==
 
<syntaxhighlight lang="j"> ;:inv ({~ ~.@($ /:~"1@#: I.@,)@(1=+/ .~: ::0:&>/~))(#~ 11<#@>) cutLF fread'unixdict.txt'
aristotelean aristotelian
claustrophobia claustrophobic
committeeman committeemen
committeewoman committeewomen
complementary complimentary
confirmation conformation
congresswoman congresswomen
councilwoman councilwomen
craftsperson draftsperson
eavesdropped eavesdropper
frontiersman frontiersmen
handicraftsman handicraftsmen
incommutable incomputable
installation instillation
kaleidescope kaleidoscope
neuroanatomy neuroanotomy
newspaperman newspapermen
nonagenarian nonogenarian
onomatopoeia onomatopoeic
philanthrope philanthropy
prescription proscription
schizophrenia schizophrenic
shakespearean shakespearian
spectroscope spectroscopy
underclassman underclassmen
upperclassman upperclassmen</syntaxhighlight>
=={{header|Java}}==
{{trans|Go}}
<langsyntaxhighlight lang="java">import java.io.*;
import java.util.*;
 
Line 875 ⟶ 1,340:
return count;
}
}</langsyntaxhighlight>
 
{{out}}
Line 886 ⟶ 1,351:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 933 ⟶ 1,398:
52: upperclassmen -> upperclassman
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq"># Emit a stream of words "greater than" the input word that differ by just one character.
def changeable_to($dict):
. as $w
Line 952 ⟶ 1,416:
| . as $w
| [changeable_to($dict)] | unique[]
| "\($w) <=> \(.)"</langsyntaxhighlight>
Invocation: jq -n -rR -f changeable-words.jq unixdict.txt
{{out}}
Line 983 ⟶ 1,447:
upperclassman <=> upperclassmen
</pre>
 
=={{header|Julia}}==
See [[Alternade_words#Julia]] for the foreachword function.
<langsyntaxhighlight lang="julia">const alts = Set{String}()
function ischangeable(w, d)
alternatives = [w[1:p[1]-1] * p[2] * w[p[1]+1:end] for p in Iterators.product(1:length(w), 'a':'z')]
Line 1,002 ⟶ 1,465:
 
foreachword("unixdict.txt", ischangeable, minlen = 12, colwidth=40, numcols=2)
</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
 
aristotelean <=> aristotelian claustrophobia <=> claustrophobic
committeeman <=> committeemen committeepersoncommitteewoman <=> committeepeoplecommitteewomen
complementary <=> complimentary confirmation <=> conformation
congresspersoncongresswoman <=> congresspeoplecongresswomen councilpersoncouncilwoman <=> councilpeoplecouncilwomen
craftsperson <=> draftsperson eavesdropped <=> eavesdropper
frontiersman <=> frontiersmen handicraftsman <=> handicraftsmen
Line 1,020 ⟶ 1,483:
underclassman <=> underclassmen upperclassman <=> upperclassmen
</pre>
 
=={{header|Nim}}==
Using the standard module <code>std/editdistance</code> to get the distance between two words.
<langsyntaxhighlight Nimlang="nim">import std/editdistance, sugar
 
# Build list of words with length >= 12.
Line 1,041 ⟶ 1,503:
inc count, 2
 
echo "\nFound ", count, " changeable words."</langsyntaxhighlight>
 
{{out}}
Line 1,074 ⟶ 1,536:
 
Found 52 changeable words.</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
<syntaxhighlight lang="pascal">
{$mode ObjFPC}{$H+}
uses
Classes, SysUtils;
 
const
FNAME = 'unixdict.txt';
 
function OneCharDifference(s1, s2: string): boolean;
var
i, diffCount: integer;
begin
diffCount := 0;
if Length(s1) <> Length(s2) then
Exit(false);
for i := 1 to Length(s1) do
begin
if s1[i] <> s2[i] then
Inc(diffCount);
if diffCount > 1 then
Exit(false);
end;
Result := diffCount = 1;
end;
 
procedure PurgeList(var list: TStringList);
{ Remove every word that doesn't have at least 12 characters }
var
i: Integer;
begin
for i := Pred(list.Count) downto 0 do
if Length(list[i]) < 12 then
list.Delete(i);
end;
 
var
list: TStringList;
i, j: Integer;
 
begin
list := TStringList.Create;
try
list.LoadFromFile(FNAME);
PurgeList(list);
for i := 0 to list.Count - 2 do
for j := i + 1 to list.Count - 1 do
if OneCharDifference(list[i], list[j]) then
writeln(list[i]:15, ' <-> ', list[j]);
finally
list.Free;
end;
end.
</syntaxhighlight>
{{out}}
<pre>
aristotelean <-> aristotelian
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeewoman <-> committeewomen
complementary <-> complimentary
confirmation <-> conformation
congresswoman <-> congresswomen
councilwoman <-> councilwomen
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
frontiersman <-> frontiersmen
handicraftsman <-> handicraftsmen
incommutable <-> incomputable
installation <-> instillation
kaleidescope <-> kaleidoscope
neuroanatomy <-> neuroanotomy
newspaperman <-> newspapermen
nonagenarian <-> nonogenarian
onomatopoeia <-> onomatopoeic
philanthrope <-> philanthropy
prescription <-> proscription
schizophrenia <-> schizophrenic
shakespearean <-> shakespearian
spectroscope <-> spectroscopy
underclassman <-> underclassmen
upperclassman <-> upperclassmen
</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict;
Line 1,092 ⟶ 1,639:
}
push @{ $words[length] }, $_;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,098 ⟶ 1,645:
claustrophobia <-> claustrophobic
committeeman <-> committeemen
committeepersoncommitteewoman <-> committeepeoplecommitteewomen
complementary <-> complimentary
confirmation <-> conformation
congresspersoncongresswoman <-> congresspeoplecongresswomen
councilpersoncouncilwoman <-> councilpeoplecouncilwomen
craftsperson <-> draftsperson
eavesdropped <-> eavesdropper
Line 1,121 ⟶ 1,668:
underclassman <-> underclassmen
upperclassman <-> upperclassmen
</pre> Alternatively:
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Changeable_words
Line 1,132 ⟶ 1,677:
my $count = 0;
printf "%3d: %15s <-> %s\n", ++$count, $1, $4
while /^ ((\N*)\N(\N*)) \n(?=.*^ (\2\N\3) \n)/gmsx;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,162 ⟶ 1,707:
26: upperclassman <-> upperclassmen
</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">changeable</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
Line 1,181 ⟶ 1,725:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d changeable words found:\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,193 ⟶ 1,737:
upperclassman <=> upperclassmen
</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{trans|Go}}
<langsyntaxhighlight lang="prolog">:- dynamic dictionary_word/1.
 
main:-
Line 1,247 ⟶ 1,790:
Count1 is Count + 1,
Count1 < 2,
hamming_distance(Chars1, Chars2, Dist, Count1).</langsyntaxhighlight>
 
{{out}}
Line 1,258 ⟶ 1,801:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 1,305 ⟶ 1,848:
52: upperclassmen -> upperclassman
</pre>
 
=={{header|Python}}==
 
<langsyntaxhighlight lang="python">from collections import defaultdict, Counter
 
 
Line 1,346 ⟶ 1,888:
print(f"\n{len(cwords)} words that are from changing a char from other words:")
for v in sorted(cwords):
print(f" {v[0]:12} From {', '.join(v[1:])}")</langsyntaxhighlight>
 
{{out}}
Line 1,361 ⟶ 1,903:
claustrophobia From claustrophobic
committeeman From committeemen
committeepersoncommitteewoman From committeepeoplecommitteewomen
complementary From complimentary
confirmation From conformation
congresspersoncongresswoman From congresspeoplecongresswomen
councilpersoncouncilwoman From councilpeoplecouncilwomen
craftsperson From draftsperson
eavesdropped From eavesdropper
Line 1,384 ⟶ 1,926:
underclassman From underclassmen
upperclassman From upperclassmen</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ over size
over size != iff
[ 2drop false ]
done
0 unrot
witheach
[ over i^ peek
!= rot + swap
over 2 = if
conclude ]
drop 1 = ] is 1diff ( $ $ --> b )
 
$ "rosetta/unixdict.txt"
sharefile drop nest$
[] swap witheach
[ dup size 11 > iff
[ nested join ]
else drop ]
[ behead over witheach
[ 2dup
1diff iff
[ over echo$
sp echo$ cr ]
else drop ]
drop
dup size 1 = until ]
drop</syntaxhighlight>
 
{{out}}
 
<pre>aristotelean aristotelian
claustrophobia claustrophobic
committeeman committeemen
committeewoman committeewomen
complementary complimentary
confirmation conformation
congresswoman congresswomen
councilwoman councilwomen
craftsperson draftsperson
eavesdropped eavesdropper
frontiersman frontiersmen
handicraftsman handicraftsmen
incommutable incomputable
installation instillation
kaleidescope kaleidoscope
neuroanatomy neuroanotomy
newspaperman newspapermen
nonagenarian nonogenarian
onomatopoeia onomatopoeic
philanthrope philanthropy
prescription proscription
schizophrenia schizophrenic
shakespearean shakespearian
spectroscope spectroscopy
underclassman underclassmen
upperclassman upperclassmen</pre>
 
=={{header|Raku}}==
Line 1,389 ⟶ 1,990:
 
Get the best of both worlds by doing an initial filter with Sorensen, then get exact results with Levenshtein.
<syntaxhighlight lang="raku" perl6line>use Text::Levenshtein;
use Text::Sorensen :sorensen;
 
Line 1,407 ⟶ 2,008:
%skip{$_}++ for @sorensens[@levenshtein];
": {$this.fmt('%14s')} <-> ", @sorensens[@levenshtein].join: ', ';
}</langsyntaxhighlight>
{{out}}
<pre> 1: aristotelean <-> aristotelian
2: claustrophobia <-> claustrophobic
3: committeeman <-> committeemen
4: committeepersoncommitteewoman <-> committeepeoplecommitteewomen
5: complimentary <-> complementary
6: confirmation <-> conformation
7: congresspersoncongresswoman <-> congresspeoplecongresswomen
8: councilpersoncouncilwoman <-> councilpeoplecouncilwomen
9: draftsperson <-> craftsperson
10: eavesdropped <-> eavesdropper
Line 1,435 ⟶ 2,036:
25: underclassman <-> underclassmen
26: upperclassman <-> upperclassmen</pre>
 
=={{header|REXX}}==
This REXX version doesn't care what order the words in the dictionary are in, &nbsp; nor does it care what
Line 1,441 ⟶ 2,041:
 
It also allows the minimum length to be specified on the command line (CL) as well as the dictionary file identifier.
<langsyntaxhighlight lang="rexx">/*REXX program finds changeable words (within an identified dict.), changing any letter.*/
parse arg minL iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 12 /*Not specified? Then use the default.*/
Line 1,477 ⟶ 2,077:
#= # - 1 /*adjust word count because of DO loop.*/
say copies('─', 30) # "words ("n 'usable words) in the dictionary file: ' iFID
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,487 ⟶ 2,087:
committeeman committeemen
committeemen committeeman
committeepersoncommitteewoman committeepeoplecommitteewomen
committeepeoplecommitteewomen committeepersoncommitteewoman
complementary complimentary
complimentary complementary
confirmation conformation
conformation confirmation
congresspersoncongresswoman congresspeoplecongresswomen
congresspeoplecongresswomen congresspersoncongresswoman
councilpersoncouncilwoman councilpeoplecouncilwomen
councilpeoplecouncilwomen councilpersoncouncilwoman
craftsperson draftsperson
draftsperson craftsperson
Line 1,535 ⟶ 2,135:
────────────────────────────── 52 changeable words found with a minimum length of 12
</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
cStr = read("unixdict.txt")
wordList = str2list(cStr)
Line 1,573 ⟶ 2,172:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,584 ⟶ 2,183:
5. committeeman >> committeemen
6. committeemen >> committeeman
7. committeewoman >> committeewomen
7. committeeperson >> committeepeople
8. committeewomen >> committeewoman
8. committeepeople >> committeeperson
9. complementary >> complimentary
10. complimentary >> complementary
11. confirmation >> conformation
12. conformation >> confirmation
13. congresspersoncongresswoman >> congresspeoplecongresswomen
14. congresspeoplecongresswomen >> congresspersoncongresswoman
15. councilpersoncouncilwoman >> councilpeoplecouncilwomen
16. councilpeoplecouncilwomen >> councilpersoncouncilwoman
17. craftsperson >> draftsperson
18. draftsperson >> craftsperson
Line 1,632 ⟶ 2,231:
done...
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">words = File.open("unixdict.txt").readlines.map(&:chomp).select{|w| w.size > 11 }
 
size_groups = words.group_by(&:size).sort.map(&:last)
Line 1,643 ⟶ 2,241:
puts "Found #{res.size} changeable word pairs:"
res.each{|w1, w2|puts "#{w1} - #{w2}" }
</syntaxhighlight>
</lang>
{{out}}
<pre>Found 26 changeable word pairs:
Line 1,675 ⟶ 2,273:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">var file = File("unixdict.txt")
 
if (!file.exists) {
Line 1,703 ⟶ 2,301:
}
bucket{len} << word
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,733 ⟶ 2,331:
26: upperclassman <-> upperclassmen
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
Using the Hamming Distance between two equal length strings which needs to be 1 here:
<langsyntaxhighlight ecmascriptlang="wren">import "io" for File
import "./fmt" for Fmt
 
var hammingDist = Fn.new { |s1, s2|
Line 1,768 ⟶ 2,365:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,779 ⟶ 2,376:
5: committeeman -> committeemen
6: committeemen -> committeeman
7: committeepersoncommitteewoman -> committeepeoplecommitteewomen
8: committeepeoplecommitteewomen -> committeepersoncommitteewoman
9: complementary -> complimentary
10: complimentary -> complementary
11: confirmation -> conformation
12: conformation -> confirmation
13: congresspersoncongresswoman -> congresspeoplecongresswomen
14: congresspeoplecongresswomen -> congresspersoncongresswoman
15: councilpersoncouncilwoman -> councilpeoplecouncilwomen
16: councilpeoplecouncilwomen -> councilpersoncouncilwoman
17: craftsperson -> draftsperson
18: draftsperson -> craftsperson
Line 1,828 ⟶ 2,425:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \use zero-terminated strings
int Dict(26000); \pointers to words (enough for unixdict.txt)
int DictSize; \actual number of pointers in Dict
Line 1,901 ⟶ 2,498:
DI:= DI+1;
until DI >= DictSize;
]</langsyntaxhighlight>
 
{{out}}
Line 1,911 ⟶ 2,508:
5 committeeman committeemen
6 committeemen committeeman
7 committeepersoncommitteewoman committeepeoplecommitteewomen
8 committeepeoplecommitteewomen committeepersoncommitteewoman
9 complementary complimentary
10 complimentary complementary
11 confirmation conformation
12 conformation confirmation
13 congresspersoncongresswoman congresspeoplecongresswomen
14 congresspeoplecongresswomen congresspersoncongresswoman
15 councilpersoncouncilwoman councilpeoplecouncilwomen
16 councilpeoplecouncilwomen councilpersoncouncilwoman
17 craftsperson draftsperson
18 draftsperson craftsperson
45

edits