fix(challenges): More S problems

This commit is contained in:
Kris Koishigawa
2019-03-08 18:00:54 +09:00
committed by mrugesh
parent c752d57611
commit 442c9c291d
4 changed files with 29 additions and 27 deletions

View File

@ -7,16 +7,17 @@ challengeType: 5
## Description
<section id='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 <a href="https://en.wikipedia.org/wiki/soundex">the WP article</a>).
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 <a href="http://rosettacode.org/wiki/https://www.archives.gov/research/census/soundex.html">https://www.archives.gov/research/census/soundex.html</a>. So check for instance if <b>Ashcraft</b> is coded to <b>A-261</b>.
<ul><li>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.</li>
<li>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.</li></ul>
Write a function that takes a string as a parameter and returns the encoded string.
The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling (from <a href="https://en.wikipedia.org/wiki/soundex" target="_blank">the WP article</a>).
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 <a href="http://rosettacode.org/wiki/https://www.archives.gov/research/census/soundex.html" target="_blank">https://www.archives.gov/research/census/soundex.html</a>. So check for instance if <b>Ashcraft</b> is coded to <b>A-261</b>.
<ul>
<li>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.</li>
<li>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.</li>
</ul>
</section>
## Instructions
<section id='instructions'>
Write a function that takes a string as a parameter and returns the encoded string.
</section>
## Tests
@ -57,7 +58,7 @@ tests:
<div id='js-seed'>
```js
function soundex (s) {
function soundex(s) {
// Good luck!
}
```
@ -69,7 +70,7 @@ function soundex (s) {
<section id='solution'>
```js
function soundex (s) {
function soundex(s) {
var a = s.toLowerCase().split('')
var f = a.shift(),
r = '',

View File

@ -30,8 +30,8 @@ For example, given <b>5</b>, produce this array:
tests:
- text: <code>spiralArray</code> should be a function.
testString: assert(typeof spiralArray=='function','<code>spiralArray</code> should be a function.');
- text: <code>spiralArray(3)</code> should return a array.
testString: assert(Array.isArray(spiralArray(3)), '<code>spiralArray(3)</code> should return a array.');
- text: <code>spiralArray(3)</code> should return an array.
testString: assert(Array.isArray(spiralArray(3)), '<code>spiralArray(3)</code> should return an array.');
- text: <code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.
testString: assert.deepEqual(spiralArray(3), [[0, 1, 2], [7, 8, 3], [6, 5, 4]], '<code>spiralArray(3)</code> should return <code>[[0, 1, 2],[7, 8, 3],[6, 5, 4]]</code>.');
- text: <code>spiralArray(4)</code> should return <code>[[0, 1, 2, 3],[11, 12, 13, 4],[10, 15, 14, 5],[9, 8, 7, 6]]</code>.
@ -47,7 +47,7 @@ tests:
<div id='js-seed'>
```js
function spiralArray (n) {
function spiralArray(n) {
// Good luck!
}
```
@ -59,7 +59,7 @@ function spiralArray (n) {
<section id='solution'>
```js
function spiralArray (n) {
function spiralArray(n) {
var arr = Array(n),
x = 0, y = n,
total = n * n--,

View File

@ -9,9 +9,13 @@ challengeType: 5
Split a (character) string into comma (plus a blank) delimited strings based on a change of character (left to right).
Blanks should be treated as any other character (except they are problematic to display clearly). The same applies to commas.
For instance, the string:
<pre>
"gHHH5YY++///\"
</pre>
should be split as:
["g", "HHH", "5", "YY", "++", "///", "\" ]
<pre>
["g", "HHH", "5", "YY", "++", "///", "\" ];
</pre>
</section>
## Instructions
@ -26,8 +30,8 @@ should be split as:
tests:
- text: <code>split</code> should be a function.
testString: assert(typeof split == 'function', '<code>split</code> should be a function.');
- text: <code>split("hello")</code> should return a array.
testString: assert(Array.isArray(split("hello")), '<code>split("hello")</code> should return a array.');
- text: <code>split("hello")</code> should return an array.
testString: assert(Array.isArray(split("hello")), '<code>split("hello")</code> should return an array.');
- text: <code>split("hello")</code> should return <code>["h", "e", "ll", "o"]</code>.
testString: assert.deepEqual(split("hello"), ["h", "e", "ll", "o"], '<code>split("hello")</code> should return <code>["h", "e", "ll", "o"]</code>.');
- text: <code>split("commission")</code> should return <code>["c", "o", "mm", "i", "ss", "i", "o", "n"]</code>.
@ -47,7 +51,7 @@ tests:
<div id='js-seed'>
```js
function split (str) {
function split(str) {
// Good luck!
}
```
@ -59,7 +63,7 @@ function split (str) {
<section id='solution'>
```js
function split (str) {
function split(str) {
const concat = xs =>
xs.length > 0 ? (() => {
const unit = typeof xs[0] === 'string' ? '' : [];

View File

@ -6,19 +6,16 @@ challengeType: 5
## Description
<section id='description'>
<b>Background</b>
This task is inspired by <a href="http://drdobbs.com/windows/198701685">Mark Nelson's DDJ Column "Wordplay"</a> and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition [http://www.npr.org/templates/story/story.php?storyId=9264290] and originally attributed to David Edelheit.
This task is inspired by <a href="http://drdobbs.com/windows/198701685" target="_blank">Mark Nelson's DDJ Column "Wordplay"</a> and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition <a href="http://www.npr.org/templates/story/story.php?storyId=9264290"target="_blank">[1]</a> and originally attributed to David Edelheit.
The challenge was to take the names of two U.S. States, mix them all together, then rearrange the letters to form the names of two <i>different</i> U.S. States (so that all four state names differ from one another).
What states are these?
The problem was reissued on <a href="https://tapestry.tucson.az.us/twiki/bin/view/Main/StateNamesPuzzle">the Unicon Discussion Web</a> which includes several solutions with analysis. Several techniques may be helpful and you may wish to refer to <a href="https://en.wikipedia.org/wiki/Goedel_numbering">Gödel numbering</a>, <a href="https://en.wikipedia.org/wiki/Equivalence_relation">equivalence relations</a>, and <a href="https://en.wikipedia.org/wiki/Equivalence_classes">equivalence classes</a>. The basic merits of these were discussed in the Unicon Discussion Web.
The problem was reissued on <a href="https://tapestry.tucson.az.us/twiki/bin/view/Main/StateNamesPuzzle" target="_blank">the Unicon Discussion Web</a> which includes several solutions with analysis. Several techniques may be helpful and you may wish to refer to <a href="https://en.wikipedia.org/wiki/Goedel_numbering">Gödel numbering</a>, <a href="https://en.wikipedia.org/wiki/Equivalence_relation" target="_blank">equivalence relations</a>, and <a href="https://en.wikipedia.org/wiki/Equivalence_classes" target="_blank">equivalence classes</a>. The basic merits of these were discussed in the Unicon Discussion Web.
A second challenge in the form of a set of fictitious new states was also presented.
<b>Task:</b>
Write a function to solve the challenge for the given array of names of states. The function should return an array. Each element should be an object in this form: {"from":[],"to":[]}. The "from" array should contain the original names and the "to" array should contain the resultant names.
</section>
## Instructions
<section id='instructions'>
Write a function to solve the challenge for the given array of names of states. The function should return an array. Each element should be an object in this form: <code>{"from":[],"to":[]}</code>. The "from" array should contain the original names and the "to" array should contain the resultant names.
</section>
## Tests
@ -28,8 +25,8 @@ Write a function to solve the challenge for the given array of names of states.
tests:
- text: <code>solve</code> should be a function.
testString: assert(typeof solve == 'function', '<code>solve</code> should be a function.');
- text: <code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return a array.
testString: assert(Array.isArray(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])), '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return a array.');
- text: <code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return an array.
testString: assert(Array.isArray(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])), '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return an array.');
- text: '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return <code>[{ from: ["North Carolina ", "South Dakota"], to: ["North Dakota", "South Carolina"] }]</code>.'
testString: assert.deepEqual(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"]), [{ from:["North Carolina ", "South Dakota"], to:["North Dakota", "South Carolina"] }], '<code>solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])</code> should return <code>[{ from:["North Carolina ", "South Dakota"], to:["North Dakota", "South Carolina"] }]</code>.');
- text: '<code>solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"])</code> should return <code>[{ from: ["New Kory", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "New York"], to: ["Kory New", "Wen Kory"] }, { from: ["New Kory", "New York"], to: ["Kory New", "York New"] }, { from: ["New York", "Wen Kory"], to: ["New Kory", "York New"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "New Kory"] }, { from: ["New York", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New York", "York New"], to: ["New Kory", "Wen Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "New Kory"] }, { from: ["New York", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "Wen Kory"] }, { from: ["Kory New", "New York"], to: ["New Kory", "York New"] }, { from: ["Kory New", "New York"], to: ["Wen Kory", "York New"] }, { from: ["New Kory", "Wen Kory"], to: ["Kory New", "York New"] }, { from: ["New Kory", "York New"], to: ["Kory New", "Wen Kory"] }, { from: ["Kory New", "New Kory"], to: ["Wen Kory", "York New"] }]</code>.'
@ -43,7 +40,7 @@ tests:
<div id='js-seed'>
```js
function solve (input) {
function solve(input) {
// Good luck!
}
```
@ -55,7 +52,7 @@ function solve (input) {
<section id='solution'>
```js
function solve (input) {
function solve(input) {
var orig = {};
input.forEach(function(e) {
orig[e.replace(/\s/g, "").toLowerCase()] = e;