Changeable words: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added a usable word count to the report title.) |
(Added Java solution) |
||
Line 432: | Line 432: | ||
} |
} |
||
} |
} |
||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Changeable words in unixdict.txt: |
|||
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|Java}}== |
|||
{{trans|Go}} |
|||
<lang java>import java.io.*; |
|||
import java.util.*; |
|||
public class ChangeableWords { |
|||
public static void main(String[] args) { |
|||
try { |
|||
final String fileName = "unixdict.txt"; |
|||
List<String> dictionary = new ArrayList<>(); |
|||
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { |
|||
String line; |
|||
while ((line = reader.readLine()) != null) { |
|||
if (line.length() > 11) |
|||
dictionary.add(line); |
|||
} |
|||
} |
|||
System.out.println(String.format("Changeable words in %s:", fileName)); |
|||
int n = 1; |
|||
for (String word1 : dictionary) { |
|||
for (String word2 : dictionary) { |
|||
if (word1 != word2 && hammingDistance(word1, word2) == 1) |
|||
System.out.println(String.format("%2d: %-14s -> %s", n++, word1, word2)); |
|||
} |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
} |
|||
private static int hammingDistance(String str1, String str2) { |
|||
int len1 = str1.length(); |
|||
int len2 = str2.length(); |
|||
if (len1 != len2) |
|||
return 0; |
|||
int count = 0; |
|||
for (int i = 0; i < len1; ++i) { |
|||
if (str1.charAt(i) != str2.charAt(i)) |
|||
++count; |
|||
// don't care about counts > 2 in this case |
|||
if (count == 2) |
|||
break; |
|||
} |
|||
return count; |
|||
} |
} |
||
}</lang> |
}</lang> |