How to resolve the algorithm State name puzzle step by step in the Wren programming language
How to resolve the algorithm State name puzzle step by step in the Wren programming language
Table of Contents
Problem Statement
Background 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 [1] 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 different U.S. States (so that all four state names differ from one another). What states are these?
The problem was reissued on the Unicon Discussion Web which includes several solutions with analysis. Several techniques may be helpful and you may wish to refer to Gödel numbering, equivalence relations, and equivalence classes. 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.
Write a program to solve the challenge using both the original list of states and the fictitious list.
Comma separated list of state names used in the original puzzle: Comma separated list of additional fictitious state names to be added to the original (Includes a duplicate):
Let's start with the solution:
Step by Step solution about How to resolve the algorithm State name puzzle step by step in the Wren programming language
Source code in the wren programming language
import "/str" for Str
import "/sort" for Sort
import "/fmt" for Fmt
var solve = Fn.new { |states|
var dict = {}
for (state in states) {
var key = Str.lower(state).replace(" ", "")
if (!dict[key]) dict[key] = state
}
var keys = dict.keys.toList
Sort.quick(keys)
var solutions = []
var duplicates = []
for (i in 0...keys.count) {
for (j in i+1...keys.count) {
var len = keys[i].count + keys[j].count
var chars = (keys[i] + keys[j]).toList
Sort.quick(chars)
var combined = chars.join()
for (k in 0...keys.count) {
for (l in k+1...keys.count) {
if (k != i && k != j && l != i && l != j) {
var len2 = keys[k].count + keys[l].count
if (len2 == len) {
var chars2 = (keys[k] + keys[l]).toList
Sort.quick(chars2)
var combined2 = chars2.join()
if (combined == combined2) {
var f1 = "%(dict[keys[i]]) + %(dict[keys[j]])"
var f2 = "%(dict[keys[k]]) + %(dict[keys[l]])"
var f3 = "%(f1) = %(f2)"
if (!duplicates.contains(f3)) {
solutions.add(f3)
var f4 = "%(f2) = %(f1)"
duplicates.add(f4)
}
}
}
}
}
}
}
}
Sort.quick(solutions)
var i = 0
for (sol in solutions) {
Fmt.print("$2d $s", i + 1, sol)
i = i + 1
}
}
var states = [
"Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut",
"Delaware",
"Florida", "Georgia", "Hawaii",
"Idaho", "Illinois", "Indiana", "Iowa",
"Kansas", "Kentucky", "Louisiana",
"Maine", "Maryland", "Massachusetts", "Michigan",
"Minnesota", "Mississippi", "Missouri", "Montana",
"Nebraska", "Nevada", "New Hampshire", "New Jersey",
"New Mexico", "New York", "North Carolina", "North Dakota",
"Ohio", "Oklahoma", "Oregon",
"Pennsylvania", "Rhode Island",
"South Carolina", "South Dakota", "Tennessee", "Texas",
"Utah", "Vermont", "Virginia",
"Washington", "West Virginia", "Wisconsin", "Wyoming"
]
System.print("Real states only:")
solve.call(states)
System.print()
var fictitious = [ "New Kory", "Wen Kory", "York New", "Kory New", "New Kory" ]
System.print("Real and fictitious states:")
solve.call(states + fictitious)
You may also check:How to resolve the algorithm Modified random distribution step by step in the Nim programming language
You may also check:How to resolve the algorithm Mertens function step by step in the BASIC programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by number of users step by step in the REXX programming language
You may also check:How to resolve the algorithm Terminal control/Dimensions step by step in the C programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the C# programming language