* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
3.2 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5a23c84252665b21eecc8017 | Soundex | 5 | 302320 | soundex |
--description--
Soundex is an algorithm for creating indices for words based on their pronunciation. The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling (from the WP article). There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the official Rules. So check for instance if Ashcraft is coded to A-261.
- If a vowel (A, E, I, O, U) separates two consonants that have the same soundex code, the consonant to the right of the vowel is coded. Tymczak is coded as T-522 (T, 5 for the M, 2 for the C, Z ignored (see "Side-by-Side" rule above), 2 for the K). Since the vowel "A" separates the Z and K, the K is coded.
- If "H" or "W" separate two consonants that have the same soundex code, the consonant to the right of the vowel is not coded. Example: Ashcraft is coded A-261 (A, 2 for the S, C ignored, 6 for the R, 1 for the F). It is not coded A-226.
--instructions--
Write a function that takes a string as a parameter and returns the encoded string.
--hints--
soundex
should be a function.
assert(typeof soundex == 'function');
soundex("Soundex")
should return a string.
assert(typeof soundex('Soundex') == 'string');
soundex("Soundex")
should return "S532"
.
assert.equal(soundex('Soundex'), 'S532');
soundex("Example")
should return "E251"
.
assert.equal(soundex('Example'), 'E251');
soundex("Sownteks")
should return "S532"
.
assert.equal(soundex('Sownteks'), 'S532');
soundex("Ekzampul")
should return "E251"
.
assert.equal(soundex('Ekzampul'), 'E251');
soundex("Euler")
should return "E460"
.
assert.equal(soundex('Euler'), 'E460');
soundex("Gauss")
should return "G200"
.
assert.equal(soundex('Gauss'), 'G200');
soundex("Hilbert")
should return "H416"
.
assert.equal(soundex('Hilbert'), 'H416');
soundex("Knuth")
should return "K530"
.
assert.equal(soundex('Knuth'), 'K530');
soundex("Lloyd")
should return "L300"
.
assert.equal(soundex('Lloyd'), 'L300');
soundex("Lukasiewicz")
should return "L222"
.
assert.equal(soundex('Lukasiewicz'), 'L222');
--seed--
--seed-contents--
function soundex(s) {
}
--solutions--
function soundex(s) {
var a = s.toLowerCase().split('');
var f = a.shift(),
r = '',
codes = {
a: '',
e: '',
i: '',
o: '',
u: '',
b: 1,
f: 1,
p: 1,
v: 1,
c: 2,
g: 2,
j: 2,
k: 2,
q: 2,
s: 2,
x: 2,
z: 2,
d: 3,
t: 3,
l: 4,
m: 5,
n: 5,
r: 6
};
r =
f +
a
.map(function(v, i, a) {
return codes[v];
})
.filter(function(v, i, a) {
return i === 0 ? v !== codes[f] : v !== a[i - 1];
})
.join('');
return (r + '000').slice(0, 4).toUpperCase();
}