State name puzzle: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 7 users not shown)
Line 43:
"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"
</pre>
 
 
{{Template:Strings}}
<br><br>
 
Line 48 ⟶ 51:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V states = [‘Alabama’, ‘Alaska’, ‘Arizona’, ‘Arkansas’,
‘California’, ‘Colorado’, ‘Connecticut’, ‘Delaware’, ‘Florida’,
‘Georgia’, ‘Hawaii’, ‘Idaho’, ‘Illinois’, ‘Indiana’, ‘Iowa’, ‘Kansas’,
Line 65 ⟶ 68:
V i = L.index
L(s2) states[i+1..]
smap[sorted(s1‘’s2).join(‘’)].append(s1‘ + ’s2)
 
L(pairs) sorted(smap.values())
I pairs.len > 1
print(pairs.join(‘ = ’))</langsyntaxhighlight>
 
{{out}}
Line 75 ⟶ 78:
North Carolina + South Dakota = North Dakota + South Carolina
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
use sorter : script ¬
"Custom Iterative Ternary Merge Sort" -- <www.macscripter.net/t/timsort-and-nigsort/71383/3>
 
on stateNamePuzzle()
script o
property stateNames : {"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", ¬
"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"}
property workList : {}
-- Custom comparison handler for the sort.
on isGreater(a, b)
return (beginning of a > beginning of b)
end isGreater
end script
ignoring case
-- Remove duplicates.
repeat with i from 1 to (count o's stateNames)
set thisName to o's stateNames's item i
if ({thisName} is not in o's workList) then set end of o's workList to thisName
end repeat
set o's stateNames to o's workList
-- Build a list of lists containing unique pairs of names preceded by
-- text composed of their combined and sorted visible characters.
set o's workList to {}
set stateCount to (count o's stateNames)
repeat with i from 1 to (stateCount - 1)
set name1 to o's stateNames's item i
repeat with j from (i + 1) to stateCount
set name2 to o's stateNames's item j
set chrs to (name1 & name2)'s characters
tell sorter to sort(chrs, 1, -1, {})
set end of o's workList to {join(chrs, "")'s word 1, {name1, name2}}
end repeat
end repeat
-- Sort the lists on the character strings
set pairCount to (count o's workList)
tell sorter to sort(o's workList, 1, pairCount, {comparer:o})
-- Look for groups of equal character strings and match
-- associated name pairs not containing the same name(s).
set output to {}
set l to 1
repeat while (l < pairCount)
set chrs to beginning of o's workList's item l
set r to l
repeat while ((r < pairCount) and (beginning of o's workList's item (r + 1) = chrs))
set r to r + 1
end repeat
if (r > l) then
repeat with i from l to (r - 1)
set {name1, name2} to end of o's workList's item i
set text1 to join(result, " and ") & " --> "
repeat with j from (i + 1) to r
set pair2 to end of o's workList's item j
if (not (({name1} is in pair2) or ({name2} is in pair2))) then
set end of output to text1 & join(pair2, " and ")
end if
end repeat
end repeat
end if
set l to r + 1
end repeat
end ignoring
return join(output, linefeed)
end stateNamePuzzle
 
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
 
stateNamePuzzle()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"North Carolina and South Dakota --> North Dakota and South Carolina
New York and New Kory --> Wen Kory and York New
New York and New Kory --> Wen Kory and Kory New
New York and New Kory --> York New and Kory New
New York and Wen Kory --> New Kory and York New
New York and Wen Kory --> New Kory and Kory New
New York and Wen Kory --> York New and Kory New
New York and York New --> New Kory and Wen Kory
New York and York New --> New Kory and Kory New
New York and York New --> Wen Kory and Kory New
New York and Kory New --> New Kory and Wen Kory
New York and Kory New --> New Kory and York New
New York and Kory New --> Wen Kory and York New
New Kory and Wen Kory --> York New and Kory New
New Kory and York New --> Wen Kory and Kory New
New Kory and Kory New --> Wen Kory and York New"
</syntaxhighlight>
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( Alabama
Alaska
Arizona
Line 167 ⟶ 282:
& "State name puzzle"$!states
& "State name puzzle"$(!states !extrastates)
);</langsyntaxhighlight>
Output:
<pre>North Carolina + South Dakota = North Dakota + South Carolina
Line 191 ⟶ 306:
=={{header|C}}==
Sort by letter occurence and deal with dupes.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 296 ⟶ 411:
find_mix();
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
Ported from C solution.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <string>
Line 398 ⟶ 513:
start = next;
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(ns clojure-sandbox.statenames
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]
Line 499 ⟶ 614:
(println "Solutions with made up states added")
(evaluate-and-print (concat real-states made-up-states)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 523 ⟶ 638:
</pre>
=={{header|D}}==
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.string, std.exception;
 
auto states = ["Alabama", "Alaska", "Arizona", "Arkansas",
Line 550 ⟶ 665:
writefln("%-(%-(%s = %)\n%)",
smap.values.sort().filter!q{ a.length > 1 });
}</langsyntaxhighlight>
{{out}}
<pre>North Carolina + South Dakota = North Dakota + South Carolina</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// State name puzzle. Nigel Galloway: February 9th., 2023
let 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"; "New Kory"; "Wen Kory"; "York New"; "Kory New"; "New Kory"]|>List.distinct
let fN(i,g)(e,l)=let n=Array.zeroCreate<int>256
let fN i g=n[g]<-n[g]+i
let fG n g=(g:string).ToLower().ToCharArray()|>Array.iter(fun g->fN n (int g))
fG 1 i; fG 1 g; fG -1 e; fG -1 l; n|>Array.forall((=)0)
let n=states|>List.allPairs states|>List.filter(fun(n,g)->n<g)|>List.groupBy(fun(n,g)->n.Length+g.Length)
let g=n|>List.map(fun(_,n)->n|>List.allPairs n|>List.filter(fun((n,g),(n',g'))->(n,g)<(n',g') && n<>n' && n<>g' && g<>n' && g<>g' && fN(n,g)(n',g')))|>List.filter(List.isEmpty>>not)
g|>List.iter(List.iter(fun((i,g),(e,l))->printfn $"%s{i},%s{g}->%s{e},%s{l}"))
</syntaxhighlight>
{{out}}
<pre>
New Kory,New York->Wen Kory,York New
New Kory,Wen Kory->New York,York New
New Kory,York New->New York,Wen Kory
Kory New,New York->New Kory,Wen Kory
Kory New,New York->New Kory,York New
Kory New,New York->Wen Kory,York New
Kory New,New Kory->New York,Wen Kory
Kory New,New Kory->New York,York New
Kory New,New Kory->Wen Kory,York New
Kory New,Wen Kory->New York,York New
Kory New,Wen Kory->New Kory,New York
Kory New,Wen Kory->New Kory,York New
Kory New,York New->New York,Wen Kory
Kory New,York New->New Kory,New York
Kory New,York New->New Kory,Wen Kory
North Carolina,South Dakota->North Dakota,South Carolina
</pre>
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 631 ⟶ 777:
}
}
}</langsyntaxhighlight>
Output:
<pre>
Line 656 ⟶ 802:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}
 
import Data.Char (toLowerisLetter, isLettertoLower)
import Data.List (sort, sortBy, nub, groupBy)
import Data.Function (on)
import Data.List (groupBy, nub, sort, sortBy)
 
-------------------- STATE NAME PUZZLE -------------------
stateNames :: [String]
stateNames=
["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"]
fakeStateNames :: [String]
fakeStateNames =
["New Kory",
"Wen Kory",
"York New",
"Kory New",
"New Kory"]
 
pairspuzzle :: [aString] -> [(a(String,a String), (String, String))]
puzzle states =
concatMap
((filter isValid . pairs) . map snd)
( filter ((> 1) . length) $
groupBy ((==) `on` fst) $
sortBy
(compare `on` fst)
[ (pkey (a <> b), (a, b))
| (a, b) <- pairs (nub $ sort states)
]
)
where
pkey = sort . filter isLetter . map toLower
isValid ((a0, a1), (b0, b1)) =
(a0 /= b0)
&& (a0 /= b1)
&& (a1 /= b0)
&& (a1 /= b1)
 
pairs :: [a] -> [(a, a)]
pairs [] = []
pairs (y : ys) = map (y,) ys ++<> pairs ys
 
puzzle :: [String] -> [((String,String), (String, String))]
puzzle states =
concatMap (filter isValid.pairs) $
map (map snd) $
filter ((>1) . length ) $
groupBy ((==) `on` fst) $
sortBy (compare `on` fst) [(pkey (a++b), (a,b)) | (a,b) <- pairs (nub $ sort states)] where
pkey = sort . filter isLetter . map toLower
isValid ((a0, a1),(b0, b1)) = (a0 /= b0) && (a0 /= b1) && (a1 /= b0) && (a1 /= b1)
 
--------------------------- TEST -------------------------
main :: IO ()
main = do
putStrLn $ "Matching pairs generated from "
"Matching pairs generated from "
++ show (length stateNames) ++ " state names and "
++<> show (length fakeStateNamesstateNames) ++ " fake state names:"
<> " state names and "
mapM_ print $ puzzle $ stateNames ++ fakeStateNames</lang>
<> show (length fakeStateNames)
<> " fake state names:"
mapM_ print $ puzzle $ stateNames <> fakeStateNames
 
stateNames :: [String]
stateNames =
[ "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"
]
 
fakeStateNames :: [String]
fakeStateNames =
[ "New Kory",
"Wen Kory",
"York New",
"Kory New",
"New Kory"
]</syntaxhighlight>
{{out}}
<pre style="font-size:80%">Matching pairs generated from 50 state names and 5 fake state names:
Line 765 ⟶ 929:
=={{header|Icon}} and {{header|Unicon}}==
=== Equivalence Class Solution ===
<langsyntaxhighlight Iconlang="icon">link strings # for csort and deletec
 
procedure main(arglist)
Line 813 ⟶ 977:
}
write("... runtime ",(&time - st)/1000.,"\n",m," matches found.")
end</langsyntaxhighlight>
 
The following are common routines:<langsyntaxhighlight Iconlang="icon">procedure getStates() # return list of state names
return ["Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut",
Line 835 ⟶ 999:
procedure getStates2() # return list of state names + fictious states
return getStates() ||| ["New Kory", "Wen Kory", "York New", "Kory New", "New Kory"]
end</langsyntaxhighlight>
 
=== Godel Number Solution ===
<langsyntaxhighlight Iconlang="icon">link factors
 
procedure GNsolve(S)
Line 895 ⟶ 1,059:
every z *:= xlate[!s]
return z
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 944 ⟶ 1,108:
Implementation:
 
<langsyntaxhighlight lang="j">require'strings stats'
 
states=:<;._2]0 :0-.LF
Line 964 ⟶ 1,128:
pairUp=: (#~ matchUp)@({~ 2 comb #)@~.
matchUp=: (i.~ ~: i:~)@:(<@normalize@;"1)
normalize=: /:~@tolower@-.&' '</langsyntaxhighlight>
 
In action:
 
<langsyntaxhighlight lang="j"> pairUp states
┌──────────────┬──────────────┐
│North Carolina│South Dakota │
├──────────────┼──────────────┤
│North Dakota │South Carolina│
└──────────────┴──────────────┘</langsyntaxhighlight>
 
Note: this approach is sufficient to solve the original problem, but does not properly deal with the addition of fictitious states. So:
 
<langsyntaxhighlight lang="j">isolatePairs=: ~.@matchUp2@(#~ *./@matchUp"2)@({~ 2 comb #)
matchUp2=: /:~"2@:(/:~"1)@(#~ 4=#@~.@,"2)</langsyntaxhighlight>
 
In action:
 
<langsyntaxhighlight lang="j"> isolatePairs pairUp 'New Kory';'Wen Kory';'York New';'Kory New';'New Kory';states
┌──────────────┬──────────────┐
│Kory New │York New │
Line 1,077 ⟶ 1,241:
├──────────────┼──────────────┤
│North Dakota │South Carolina│
└──────────────┴──────────────┘</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
import java.util.stream.*;
 
Line 1,138 ⟶ 1,302:
});
}
}</langsyntaxhighlight>
 
Output:
Line 1,161 ⟶ 1,325:
=={{header|jq}}==
{{works with|jq|1.4}}
<langsyntaxhighlight lang="jq"># Input: a string
# Output: an array, being the exploded form of the normalized input
def normalize:
Line 1,200 ⟶ 1,364:
else .
end)
| .[];</langsyntaxhighlight>
 
'''The task:'''
<langsyntaxhighlight lang="jq">def States: [
"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
"Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho",
Line 1,222 ⟶ 1,386:
(States + ["New Kory", "Wen Kory", "York New", "Kory New", "New Kory"] | solve) ;
 
task</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -c -n -r -f State_name_puzzle.jq
Real state names:
[["North Carolina","South Dakota"],["North Dakota","South Carolina"]]
Line 1,244 ⟶ 1,408:
[["New Kory","Wen Kory"],["New York","York New"]]
[["New Kory","York New"],["New York","Wen Kory"]]
[["North Carolina","South Dakota"],["North Dakota","South Carolina"]]</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 1,251 ⟶ 1,415:
 
'''Module''':
<langsyntaxhighlight lang="julia">module StateNamePuzzle
 
const realnames = ["Alabama", "Alaska", "Arizona", "Arkansas", "California",
Line 1,305 ⟶ 1,469:
end
 
end # module StateNamePuzzle</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang="julia">println("Real states:")
foreach(println, StateNamePuzzle.solve(StateNamePuzzle.realnames))
 
println("\nReal + fictitious state:")
foreach(println, StateNamePuzzle.solve(vcat(StateNamePuzzle.realnames,
StateNamePuzzle.fictitious)))</langsyntaxhighlight>
 
{{out}}
Line 1,338 ⟶ 1,502:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.2.10
 
fun solve(states: List<String>) {
Line 1,408 ⟶ 1,572:
println("Real and fictitious states:")
solve(states + fictitious)
}</langsyntaxhighlight>
 
{{out}}
Line 1,436 ⟶ 1,600:
=={{header|LiveCode}}==
This is going to be O(N^2).
<langsyntaxhighlight lang="livecode">function pairwiseAnagrams X
if the optionkey is down then breakpoint
put the long seconds into T
Line 1,480 ⟶ 1,644:
replace comma with empty in X
return X
end itemsToChars</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">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];
Line 1,493 ⟶ 1,657:
"Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington",
"West Virginia", "Wisconsin", "Wyoming"}], {4}], 1],
letters[#, 2] === letters[#, -2] &]</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat, strutils, tables
 
 
Line 1,555 ⟶ 1,719:
echo "For real + fictitious states:"
for n, pairs in matchingPairs(States & Fictitious):
echo &"{n+1:2}: {pairs}"</langsyntaxhighlight>
 
{{out}}
Line 1,580 ⟶ 1,744:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
use warnings;
use strict;
Line 1,644 ⟶ 1,808:
 
say @states + @fictious, ' states:';
puzzle(@states, @fictious);</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant states = {"Alabama", "Alaska", "Arizona", "Arkansas",
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
"California", "Colorado", "Connecticut", "Delaware",
<span style="color: #008080;">constant</span> <span style="color: #000000;">states</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Alabama"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Alaska"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Arizona"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Arkansas"</span><span style="color: #0000FF;">,</span>
"Florida", "Georgia", "Hawaii", "Idaho", "Illinois",
<span style="color: #008000;">"California"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Colorado"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Connecticut"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Delaware"</span><span style="color: #0000FF;">,</span>
"Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
<span style="color: #008000;">"Florida"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Georgia"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Hawaii"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Idaho"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Illinois"</span><span style="color: #0000FF;">,</span>
"Maine", "Maryland", "Massachusetts", "Michigan",
<span style="color: #008000;">"Indiana"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Iowa"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Kansas"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Kentucky"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Louisiana"</span><span style="color: #0000FF;">,</span>
"Minnesota", "Mississippi", "Missouri", "Montana",
<span style="color: #008000;">"Maine"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Maryland"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Massachusetts"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Michigan"</span><span style="color: #0000FF;">,</span>
"Nebraska", "Nevada", "New Hampshire", "New Jersey",
<span style="color: #008000;">"Minnesota"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Mississippi"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Missouri"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Montana"</span><span style="color: #0000FF;">,</span>
"New Mexico", "New York", "North Carolina", "North Dakota",
<span style="color: #008000;">"Nebraska"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Nevada"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"New Hampshire"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"New Jersey"</span><span style="color: #0000FF;">,</span>
"Ohio", "Oklahoma", "Oregon", "Pennsylvania",
<span style="color: #008000;">"New Mexico"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"New York"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North Carolina"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"North Dakota"</span><span style="color: #0000FF;">,</span>
"Rhode Island", "South Carolina", "South Dakota",
<span style="color: #008000;">"Ohio"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Oklahoma"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Oregon"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Pennsylvania"</span><span style="color: #0000FF;">,</span>
"Tennessee", "Texas", "Utah", "Vermont", "Virginia",
<span style="color: #008000;">"Rhode Island"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South Carolina"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"South Dakota"</span><span style="color: #0000FF;">,</span>
"Washington", "West Virginia", "Wisconsin", "Wyoming"},
<span style="color: #008000;">"Tennessee"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Texas"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Utah"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Vermont"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Virginia"</span><span style="color: #0000FF;">,</span>
-- extras = {"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"}
<span style="color: #008000;">"Washington"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"West Virginia"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Wisconsin"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Wyoming"</span><span style="color: #0000FF;">},</span>
extras = {"Slender Dragon", "Abalamara"}
<span style="color: #000080;font-style:italic;">-- extras = {"New Kory", "Wen Kory", "York New", "Kory New", "New Kory"}</span>
 
<span style="color: #000000;">extras</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Slender Dragon"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Abalamara"</span><span style="color: #0000FF;">}</span>
function no_dup(sequence s)
s = sort(s)
<span style="color: #008080;">function</span> <span style="color: #000000;">no_dup</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
for i=length(s) to 2 by -1 do
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
if s[i]=s[i-1] then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
s[i] = s[$]
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
s = s[1..$-1]
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[$]</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
procedure play(sequence s)
s = no_dup(s)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">play</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
destroy_dict(1) -- empty dict
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">no_dup</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">))</span>
for i=1 to length(s)-1 do
<span style="color: #7060A8;">destroy_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- empty dict</span>
for j=i+1 to length(s) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
string key = trim(sort(lower(s[i]&s[j])))
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
object data = getd(key)
<span style="color: #004080;">string</span> <span style="color: #000000;">key</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])))</span>
if data=0 then
<span style="color: #004080;">object</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">)</span>
putd(key,{{i,j}})
<span style="color: #008080;">if</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
else
<span style="color: #7060A8;">putd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}})</span>
for k=1 to length(data) do
<span integer {m,n} style="color: data[k]#008080;">else</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if m!=i and m!=j and n!=i and n!=j then
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
?{s[i],s[j],"<==>",s[m],s[n]}
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">i</span> <span style="color: #008080;">and</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">j</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">i</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">j</span> <span style="color: #008080;">then</span>
end if
<span style="color: #0000FF;">?{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"&lt;==&gt;"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">],</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]}</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
putd(key,append(data,{i,j}))
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #7060A8;">putd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">}))</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
play(states)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
?"==="
<span style="color: #000000;">play</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">)</span>
play(states&extras)</lang>
<span style="color: #0000FF;">?</span><span style="color: #008000;">"==="</span>
<span style="color: #000000;">play</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">&</span><span style="color: #000000;">extras</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,707 ⟶ 1,874:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(setq *States
(group
(mapcar '((Name) (cons (clip (sort (chop (lowc Name)))) Name))
Line 1,747 ⟶ 1,914:
(car Y) ) )
(cdr X) ) )
*States ) ) )</langsyntaxhighlight>
Output:
<pre>-> ((("North Carolina" . "South Dakota") ("North Dakota" . "South Carolina")))</pre>
Line 1,754 ⟶ 1,921:
 
Works with SWI-Prolog. Use of Goedel numbers.
<langsyntaxhighlight Prologlang="prolog">state_name_puzzle :-
L = ["Alabama", "Alaska", "Arizona", "Arkansas",
"California", "Colorado", "Connecticut",
Line 1,824 ⟶ 1,991:
GC1 is GC + 26 ** Ind,
compute_Goedel(T, GC1, GF).
</syntaxhighlight>
</lang>
Output :
<pre> ?- time(state_name_puzzle).
Line 1,849 ⟶ 2,016:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">from collections import defaultdict
 
states = ["Alabama", "Alaska", "Arizona", "Arkansas",
Line 1,874 ⟶ 2,041:
for pairs in sorted(smap.itervalues()):
if len(pairs) > 1:
print " = ".join(pairs)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(define states
Line 1,908 ⟶ 2,075:
(cond [(hash-ref seen c (λ() (hash-set! seen c (list s1 s2)) #f))
=> (λ(states) (displayln (~v states (list s1 s2))))]))
</syntaxhighlight>
</lang>
Output:
<langsyntaxhighlight lang="racket">
'("north dakota" "south carolina") '("north carolina" "south dakota")
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2018.03}}
<syntaxhighlight lang="raku" perl6line>my @states = <
Alabama Alaska Arizona Arkansas California Colorado Connecticut Delaware
Florida Georgia Hawaii Idaho Illinois Indiana Iowa Kansas Kentucky
Line 1,952 ⟶ 2,119:
}
}
}</langsyntaxhighlight>
 
Output:
Line 1,979 ⟶ 2,146:
Code was added to the REXX program to remove dead-end words (state names) that can't possibly be part of
<br>a solution, in particular, words that contain a unique letter (among all the state names).
<langsyntaxhighlight lang="rexx">/*REXX program (state name puzzle) rearranges two state's names ──► two new states. */
!='Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia,',
'Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, ',
Line 2,054 ⟶ 2,221:
/*──────────────────────────────────────────────────────────────────────────────────────*/
list: say; do i=1 for z; say right(i, 9) ##.i; end; say; return
s: if arg(1)==1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre style="height:60ex">
Line 2,201 ⟶ 2,368:
=={{header|Ruby}}==
{{trans|Tcl}}
<langsyntaxhighlight lang="ruby">require 'set'
 
# 26 prime numbers
Line 2,245 ⟶ 2,412:
puts ""
puts "with fictional states"
print_answer(States + ["New Kory", "Wen Kory", "York New", "Kory New", "New Kory"])</langsyntaxhighlight>
 
outputs
Line 2,270 ⟶ 2,437:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object StateNamePuzzle extends App {
// Logic:
def disjointPairs(pairs: Seq[Set[String]]) =
Line 2,295 ⟶ 2,462:
 
println(anagramPairs(states).map(_.map(_ mkString " + ") mkString " = ") mkString "\n")
}</langsyntaxhighlight>
{{out}}
<pre>New Kory + Wen Kory = York New + Kory New
Line 2,315 ⟶ 2,482:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
# Gödel number generator
proc goedel s {
Line 2,381 ⟶ 2,548:
"New Kory" "Wen Kory" "York New" "Kory New" "New Kory"
}
printPairs "Real and False States" [groupStates [concat $realStates $falseStates]]</langsyntaxhighlight>
Output:
<pre>
Line 2,442 ⟶ 2,609:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./str" for Str
import "./sort" for Sort
import "./fmt" for Fmt
 
var solve = Fn.new { |states|
Line 2,516 ⟶ 2,683:
var fictitious = [ "New Kory", "Wen Kory", "York New", "Kory New", "New Kory" ]
System.print("Real and fictitious states:")
solve.call(states + fictitious)</langsyntaxhighlight>
 
{{out}}
Line 2,544 ⟶ 2,711:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">#<<< // here doc
states:=("Alabama, Alaska, Arizona, Arkansas,
California, Colorado, Connecticut, Delaware, Florida,
Line 2,569 ⟶ 2,736:
// pairs=Utils.Helpers.listUnique(pairs); // eliminate dups
if(pairs.len()>1)
println(pairs.concat(" = ")) }</langsyntaxhighlight>
{{out}}
<pre>
9,482

edits