2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc8024
title: State name puzzle
challengeType: 5
2020-05-21 17:31:25 +02:00
isHidden: false
2019-08-05 09:17:33 -07:00
forumTopicId: 302323
2018-12-30 19:05:56 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='description'>
2019-07-18 17:32:12 +02:00
2019-05-23 13:57:59 +09:00
This task is inspired by Mark Nelson's DDJ Column "Wordplay" and one of the weekly puzzle challenges from Will Shortz on NPR Weekend Edition <a href="https://www.npr.org/templates/story/story.php?storyId=9264290" target="_blank">[1]</a> and originally attributed to David Edelheit.
2018-12-30 19:05:56 +05:30
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?
2020-03-30 11:23:18 -05:00
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" target="_blank">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.
2018-12-30 19:05:56 +05:30
A second challenge in the form of a set of fictitious new states was also presented.
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
</section>
## Instructions
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='instructions'>
2019-07-18 17:32:12 +02:00
2019-03-08 18:00:54 +09:00
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.
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
</section>
## Tests
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='tests'>
2020-03-30 11:23:18 -05:00
```yml
2018-12-30 19:05:56 +05:30
tests:
- text: <code>solve</code> should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof solve == 'function');
2019-03-08 18:00:54 +09:00
- 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.
2020-03-30 11:23:18 -05:00
testString: assert(Array.isArray(solve(["New Mexico", "New York", "North Carolina ", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota"])));
2018-12-30 19:05:56 +05:30
- 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>.'
2020-03-30 11:23:18 -05:00
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"] }]);
2018-12-30 19:05:56 +05:30
- 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>.'
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(solve(["New York", "New Kory", "Wen Kory", "York New", "Kory New", "New Kory"]), [{ 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"] }]);
2018-12-30 19:05:56 +05:30
```
</section>
## Challenge Seed
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='challengeSeed'>
2019-07-18 17:32:12 +02:00
2018-12-30 19:05:56 +05:30
<div id='js-seed'>
```js
2019-03-08 18:00:54 +09:00
function solve(input) {
2018-12-30 19:05:56 +05:30
// Good luck!
}
```
</div>
</section>
## Solution
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
<section id='solution'>
```js
2019-03-08 18:00:54 +09:00
function solve(input) {
2018-12-30 19:05:56 +05:30
var orig = {};
input.forEach(function(e) {
2020-03-30 11:23:18 -05:00
orig[e.replace(/\s/g, '').toLowerCase()] = e;
});
2018-12-30 19:05:56 +05:30
2020-03-30 11:23:18 -05:00
input = Object.keys(orig);
2018-12-30 19:05:56 +05:30
var map = {};
for (var i = 0; i < input.length - 1; i++) {
var pair0 = input[i];
for (var j = i + 1; j < input.length; j++) {
var pair = [pair0, input[j]];
var s = pair0 + pair[1];
2020-03-30 11:23:18 -05:00
var key = s.split('').sort();
2018-12-30 19:05:56 +05:30
var val = map[key] ? map[key] : [];
val.push(pair);
map[key] = val;
}
}
var result = [];
2020-03-30 11:23:18 -05:00
Object.keys(map).forEach(key => {
2018-12-30 19:05:56 +05:30
for (var i = 0; i < map[key].length - 1; i++) {
var a = map[key][i];
for (var j = i + 1; j < map[key].length; j++) {
var b = map[key][j];
2020-03-30 11:23:18 -05:00
if (new Set([a[0], b[0], a[1], b[1]]).size < 4) continue;
var from = [orig[a[0]], orig[a[1]]].sort();
var to = [orig[b[0]], orig[b[1]]].sort();
2018-12-30 19:05:56 +05:30
result.push({
from,
to
2020-03-30 11:23:18 -05:00
});
2018-12-30 19:05:56 +05:30
}
}
});
return result;
}
```
2019-07-18 17:32:12 +02:00
</section>