How to resolve the algorithm State name puzzle step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm State name puzzle step by step in the Mathematica/Wolfram Language 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 Mathematica/Wolfram Language programming language

This Wolfram program generates and selects permutations of 4-letter subsets of U.S. state names, prioritizing those with the same first and last pair of letters. Let's break down the code step by step:

letters[words_,n_] := Sort[Flatten[Characters /@ Take[words,n]]]

This function takes two inputs: a list of words (or strings) and an integer n. It extracts the first n characters from each word in the list, flattens them into a single list, and sorts the resulting list. The purpose of this function is to compare the first and last characters of different permutations.

groupSameQ[g1_, g2_] := Sort /@ Partition[g1, 2] === Sort /@ Partition[g2, 2]

This function takes two input lists (or groups) and compares them. It partitions both groups into pairs of elements, sorts each pair, and then compares the sorted pairs from both groups. If the sorted pairs are equal, the function returns True, indicating that the groups have the same first and last pair of elements.

permutations[{a_, b_, c_, d_}] = Union[Permutations[{a, b, c, d}], SameTest -> groupSameQ]

This function takes a list of four elements and generates permutations of those elements. It uses the Union function to remove duplicate permutations and applies the SameTest option to use the groupSameQ function for comparison. This means that permutations with the same first and last pair of elements are considered duplicates and are discarded.

Select[Flatten[permutations /@ 
  Subsets[Union[ToLowerCase/@{"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"}], {4}], 1], 
letters[#, 2] === letters[#, -2] &]

In this part of the code, the program generates all possible 4-element subsets of the 50 U.S. state names in lowercase. It then creates permutations of each subset using the permutations function. The Flatten function is used to transform the list of lists into a single list. The Select function is then used to filter the list of permutations based on the condition that the first and last two letters of each permutation are the same.

Source code in the wolfram programming language

letters[words_,n_] := Sort[Flatten[Characters /@ Take[words,n]]];
groupSameQ[g1_, g2_] := Sort /@ Partition[g1, 2] === Sort /@ Partition[g2, 2];
permutations[{a_, b_, c_, d_}] = Union[Permutations[{a, b, c, d}], SameTest -> groupSameQ];
Select[Flatten[permutations /@ 
   Subsets[Union[ToLowerCase/@{"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"}], {4}], 1], 
 letters[#, 2] === letters[#, -2] &]


  

You may also check:How to resolve the algorithm Chat server step by step in the Erlang programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the zkl programming language
You may also check:How to resolve the algorithm Zsigmondy numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the PL/I programming language