Changeable words: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: remove flag)
(Added C++ solution)
Line 10: Line 10:
{{Template:Strings}}
{{Template:Strings}}
<br><br>
<br><br>

=={{header|C++}}==
{{trans|Go}}
<lang cpp>#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

int hamming_distance(const std::string& str1, const std::string& str2) {
size_t len1 = str1.size();
size_t len2 = str2.size();
if (len1 != len2)
return 0;
int count = 0;
for (size_t i = 0; i < len1; ++i) {
if (str1[i] != str2[i])
++count;
// don't care about counts > 2 in this case
if (count == 2)
break;
}
return count;
}

int main(int argc, char** argv) {
const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
std::ifstream in(filename);
if (!in) {
std::cerr << "Cannot open file '" << filename << "'.\n";
return EXIT_FAILURE;
}
std::string line;
std::vector<std::string> dictionary;
while (getline(in, line)) {
if (line.size() > 11)
dictionary.push_back(line);
}
std::cout << "Changeable words in " << filename << ":\n";
int n = 1;
for (const std::string& word1 : dictionary) {
for (const std::string& word2 : dictionary) {
if (hamming_distance(word1, word2) == 1)
std::cout << std::setw(2) << std::right << n++
<< ": " << std::setw(14) << std::left << word1
<< " -> " << word2 << '\n';
}
}
return EXIT_SUCCESS;
}</lang>

{{out}}
<pre>
Changeable words in ../data/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|FreeBASIC}}==
=={{header|FreeBASIC}}==