Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
15
Task/Soundex/D/soundex-1.d
Normal file
15
Task/Soundex/D/soundex-1.d
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import std.stdio: writeln;
|
||||
import std.string: soundex;
|
||||
|
||||
void main() {
|
||||
assert(soundex("soundex") == "S532");
|
||||
assert(soundex("example") == "E251");
|
||||
assert(soundex("ciondecks") == "C532");
|
||||
assert(soundex("ekzampul") == "E251");
|
||||
assert(soundex("Robert") == "R163");
|
||||
assert(soundex("Rupert") == "R163");
|
||||
assert(soundex("Rubin") == "R150");
|
||||
assert(soundex("Ashcraft") == "A261");
|
||||
assert(soundex("Ashcroft") == "A261");
|
||||
assert(soundex("Tymczak") == "T522");
|
||||
}
|
||||
61
Task/Soundex/D/soundex-2.d
Normal file
61
Task/Soundex/D/soundex-2.d
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
import std.array, std.string, std.ascii, std.algorithm, std.range;
|
||||
|
||||
/**
|
||||
Soundex is a phonetic algorithm for indexing names by
|
||||
sound, as pronounced in English. See:
|
||||
http://en.wikipedia.org/wiki/Soundex
|
||||
*/
|
||||
string soundex(in string name) pure /*nothrow*/
|
||||
out(result) {
|
||||
assert(result.length == 4);
|
||||
assert(result[0] == '0' || result[0].isUpper);
|
||||
|
||||
if (name.empty)
|
||||
assert(result == "0000");
|
||||
immutable charCount = name.filter!isAlpha.walkLength;
|
||||
assert((charCount == 0) == (result == "0000"));
|
||||
} body {
|
||||
// Adapted from public domain Python code by Gregory Jorgensen:
|
||||
// http://code.activestate.com/recipes/52213/
|
||||
// digits holds the soundex values for the alphabet.
|
||||
static immutable digits = "01230120022455012623010202";
|
||||
string firstChar, result;
|
||||
|
||||
// Translate alpha chars in name to soundex digits.
|
||||
foreach (immutable dchar c; name.toUpper) { // Not nothrow.
|
||||
if (c.isUpper) {
|
||||
if (firstChar.empty)
|
||||
firstChar ~= c; // Remember first letter.
|
||||
immutable char d = digits[c - 'A'];
|
||||
// Duplicate consecutive soundex digits are skipped.
|
||||
if (!result.length || d != result.back)
|
||||
result ~= d;
|
||||
}
|
||||
}
|
||||
|
||||
// Return 0000 if the name is empty.
|
||||
if (!firstChar.length)
|
||||
return "0000";
|
||||
|
||||
// Replace first digit with first alpha character.
|
||||
assert(!result.empty);
|
||||
result = firstChar ~ result[1 .. $];
|
||||
|
||||
// Remove all 0s from the soundex code.
|
||||
result = result.replace("0", "");
|
||||
|
||||
// Return soundex code padded to 4 zeros.
|
||||
return (result ~ "0000")[0 .. 4];
|
||||
} unittest { // Tests of soundex().
|
||||
auto tests = [["", "0000"], ["12346", "0000"],
|
||||
["he", "H000"], ["soundex", "S532"],
|
||||
["example", "E251"], ["ciondecks", "C532"],
|
||||
["ekzampul", "E251"], ["résumé", "R250"],
|
||||
["Robert", "R163"], ["Rupert", "R163"],
|
||||
["Rubin", "R150"], ["Ashcraft", "A226"],
|
||||
["Ashcroft", "A226"]];
|
||||
foreach (const pair; tests)
|
||||
assert(pair[0].soundex == pair[1]);
|
||||
}
|
||||
|
||||
void main() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue