City names game

16

1

If you like, write a program which sorts cities according to the rules of city name game.

  • Each name of the city should start from the last letter in the previous city name. E.g. Lviv -> v -> Viden -> n -> Neapolis -> s -> Sidney -> y -> Yokogama -> a -> Amsterdam -> m -> Madrid -> d -> Denwer

  • In the sorted list first letter of the first city and last letter of the last shouldn't match anything doesn't have to be the same letter.

  • You can assume city names have only letters.
  • Program output should have same capitalization as input

Example:

% ./script Neapolis Yokogama Sidney Amsterdam Madrid Lviv Viden Denwer
["Lviv", "Viden", "Neapolis", "Sidney", "Yokogama", "Amsterdam", "Madrid", "Denwer"]

defhlt

Posted 2012-07-16T15:41:03.903

Reputation: 1 717

2Can we assume that there will always be a valid solution? – Gareth – 2012-07-16T16:00:03.467

@Gareth yes, you can – defhlt – 2012-07-16T16:02:19.487

the second rule - "[...] shouldn't match anything" - is it a requirement or just a statement saying that it's OK to have mismatch between the first and last letter? (ex: is a list like ["Viden" ... "Lviv"] invalid?) – Cristian Lupascu – 2012-07-16T18:31:22.003

@w0lf by "shouldn't" I meant it doesn't have to, it is not compulsory. So your example is valid. – defhlt – 2012-07-16T18:39:51.537

Hint: If you want a nice solution, you can reduce this to the calculation of eulerian paths, where each letter is a vertex and each word is an edge. (For instance, Berlin is the edge BN) This is solvable in O(n), where n is the number of edges. – FUZxxl – 2012-07-17T22:56:46.287

Answers

11

Ruby, 58 55 44 characters

p$*.permutation.find{|i|i*?,!~/(.),(?!\1)/i}

Yet another ruby implementation. Uses also case insensitive regex (as Ventero's old solution) but the test is done differently.

Previous version:

p$*.permutation.find{|i|(i*?,).gsub(/(.),\1/i,"")!~/,/}

Howard

Posted 2012-07-16T15:41:03.903

Reputation: 23 109

Very nice! And I think you can get this down to 55 if you use !~ instead of negating the whole expression. – Cristian Lupascu – 2012-07-17T13:33:08.017

That is smart regexp – defhlt – 2012-07-17T13:37:47.703

@w0lf Of course! How could I not think of that? – Howard – 2012-07-17T14:16:22.517

5

Python (162 141 124)

Brute force for the win.

from itertools import*
print[j for j in permutations(raw_input().split())if all(x[-1]==y[0].lower()for x,y in zip(j,j[1:]))]

beary605

Posted 2012-07-16T15:41:03.903

Reputation: 3 904

1I think you can remove the &(j[0][0]!=j[-1][-1]) condition; see the question comments above. – Cristian Lupascu – 2012-07-16T18:45:33.957

1124 from itertools import*;print[j for j in permutations(raw_input().split())if all(x[-1]==y[0].lower()for x,y in zip(j,j[1:]))] – Ev_genus – 2012-07-16T21:36:50.187

I'm trying to wrap my head around what's going on in this function. What exactly are j, x, y? How are they defined? I'm sorry if these questions are lame, I'm new to Python and would like to work with it some more. – Rob – 2012-07-17T22:56:51.153

@MikeDtrick: j contains a permutation of the cities, which is generated with the permutations command. The big if on the end basically validates that for all values in j, the last letter of one value in j is the same as the first letter of the next value in j. Honestly, I don't know either what the zip does, zip works in mysterious ways. – beary605 – 2012-07-17T23:17:10.477

Okay, thank you for the explanation! +1 – Rob – 2012-07-18T01:46:47.617

Actually that was my +1 but I'm not greedy )). zip takes pair of lists and outputs list of pairs. j is permutation, j[1:] if permutation without first element. So zip(j,j[1:]) is list of pairs: current word x and next word y). – Ev_genus – 2012-07-19T01:01:48.007

I should use zip more, lol. – beary605 – 2012-07-19T01:05:37.847

5

Ruby 1.9, 63 54 characters

New solution is based on Howard's solution:

p$*.permutation.max_by{|i|(i*?,).scan(/(.),\1/i).size}

This uses the fact that there'll always be a valid solution.

Old solution, based on w0lf's solution:

p$*.permutation.find{|i|i.inject{|a,e|a&&e[0]=~/#{a[-1]}/i&&e}}

Ventero

Posted 2012-07-16T15:41:03.903

Reputation: 9 842

Nice idea with the max_by. And your new version inspired myself for an even newer (and shorter) one. – Howard – 2012-07-17T14:57:44.633

@Howard Thanks! Your new solution is really awesome, will be difficult to beat that. ;) – Ventero – 2012-07-17T15:57:32.213

4

Ruby 74 72 104 103 71 70

p$*.permutation.find{|i|i.inject{|a,e|a[-1].casecmp(e[0])==0?e:?,}>?,}

Demo: http://ideone.com/MDK5c (in the demo I've used gets().split() instead of $*; I don't know if Ideone can simulate command-line args).

Cristian Lupascu

Posted 2012-07-16T15:41:03.903

Reputation: 8 369

looks similar to my variant $*.permutation{|p|p p if p.inject(p[0][0]){|m,e|m.casecmp(e[0])==0?e[-1]:?_}>?_} but yours is 9 characters shorter! – defhlt – 2012-07-16T18:48:59.827

2p$*.permutation.find{|i|i.inject{|a,e|a&&e[0]=~/#{a[-1]}/i&&e}} is quite a bit shorter. A Ruby 1.8(!) solution which is even shorter: p$*.permutation.find{|i|i.inject{|a,e|a&&a[-1]-32==e[0]&&e}} – Ventero – 2012-07-17T00:03:06.127

@Ventero Using case-insensitive regex is a brilliant idea! Please post this as your own answer; I'm not worthy to use that. :) – Cristian Lupascu – 2012-07-17T06:19:49.403

@Ventero the -32 solution is also very ingenious, but it relies on the fact that names start with an uppercase letter and end with a lowercase, which may not always be the case. – Cristian Lupascu – 2012-07-17T06:22:32.490

@w0lf You're right, I thought I read in the specs that it would be the case, but obviously I'm mistaken. ;) – Ventero – 2012-07-17T12:01:25.427

3

Haskell, 94 74 bytes

g[a]=[[a]]
g c=[b:r|b<-c,r<-g[x|x<-c,x/=b],last b==[r!!0!!0..]!!32]
head.g

Recursively finds all solutions. -7 bytes if it's ok to output all solutions instead of the first. Thanks to @Lynn for getting rid of the pesky import, shaving 18 bytes off the score!

Try it online!

Angs

Posted 2012-07-16T15:41:03.903

Reputation: 4 825

You can get rid of the Data.Char import with last b==[r!!0!!0..]!!32. Also, you don't need parens in g[x|x<-c,x/=b] – Lynn – 2018-05-04T10:39:01.053

1@Lynn nice, I somehow thought fromEnum would be a must. Funny, I took those parentheses away already once, but I must have copied from the wrong tab… – Angs – 2018-05-04T10:51:51.837

3

Python, 113

Very similar to @beary605's answer, and even more brute-forced.

from random import*
l=raw_input().split()
while any(x[-1]!=y[0].lower()for x,y in zip(l,l[1:])):
 shuffle(l)
print l

Nolen Royalty

Posted 2012-07-16T15:41:03.903

Reputation: 330

1Woohoo, bogo-sort style! – beary605 – 2012-07-17T15:16:30.920

2

Jelly, 25 18 bytes (Improvements welcome!)

UżḢŒuE
ḲŒ!çƝẠ$ÐfḢK

Try it online!

UżḢŒuE        dyadic (2-arg) "check two adjacent city names" function:
Uż            pair (żip) the letters of the reversed left argument with the right argument,
  Ḣ           get the Ḣead of that pairing to yield just the last letter of left and the first letter of right,
   Œu         capitalize both letters,
     E       and check that they're equal!
ḲŒ!çƝẠ$ÐfḢK    i/o and check / fold function:
ḲŒ!            split the input on spaces and get all permutations of it,
   çƝẠ$        run the above function on every adjacent pair (çƝ), and return true if Ȧll pairs are true
       Ðf      filter the permutations to only get the correct ones,
         ḢK    take the first of those, and join by spaces!

Thanks to @Lynn for most of these improvements!

25-byte solution:

Uḣ1Œu=⁹ḣ1
çƝȦ
ḲŒ!©Ç€i1ị®K

Try it online!

Uḣ1Œu=⁹ḣ1      dyadic (2-arg) "check two adjacent city names" function:
Uḣ1Œu          reverse the left arg, get the ḣead, and capitalize it (AKA capitalize the last letter),
     =⁹ḣ1      and check if it's equal to the head (first letter) of the right argument.
çƝȦ            run the above function on every adjacent pair (çƝ), and return true if Ȧll pairs are true
ḲŒ!©Ç€i1ị®K     main i/o function:
ḲŒ!©           split the input on spaces and get all its permutations, ©opy that to the register
    Ç€         run the above link on €ach permutation,
      i1       find the index of the first "successful" permutation,
        ị®K    and ®ecall the permutation list to get the actual ordering at that ịndex, separating output by spaces

Harry

Posted 2012-07-16T15:41:03.903

Reputation: 1 189

2

Some improvements: Try it online! I wrote a little changelog in the "Input" field. (Oh, after Ðf I use X to pick a random solution instead of the first one, but works just as well.)

– Lynn – 2018-05-04T14:40:59.670

@Lynn Thank you so much! The zip part was very clever, and I think I can use that Ðf quick in a lot of my other programs to save some space! – Harry – 2018-05-04T15:52:30.967

2

Husk, 10 bytes

←fΛ~=o_←→P

Try it online!

Explanation

←fΛ~=(_←)→P  -- example input: ["Xbc","Abc","Cba"]
          P  -- all permutations: [["Xbc","Abc","Cba"],…,[Xbc","Cba","Abc"]]
 f           -- filter by the following (example with ["Xbc","Cba","Abc"])
  Λ          -- | all adjacent pairs ([("Xbc","Cba"),("Cba","Abc")])
   ~=        -- | | are they equal when..
     (_←)    -- | | .. taking the first character lower-cased
         →   -- | | .. taking the last character
             -- | : ['c'=='c','a'=='a'] -> 4
             -- : [["Xbc","Cba","Abc"]]
←            -- take the first element: ["Xbc","Cba","Abc"]

Alternatively, 10 bytes

We could also count the number of adjacent pairs which satisfy the predicate (#), sort on (Ö) that and take the last element () for the same number of bytes:

→Ö#~=o_←→P

Try it online!

ბიმო

Posted 2012-07-16T15:41:03.903

Reputation: 15 345

2

GolfScript, 78 characters

" ":s/.{1${1$=!},{:h.,{1$-1={1$0=^31&!{[1$1$]s*[\](\h\-c}*;}+/}{;.p}if}:c~;}/;

A first version in GolfScript. It also does a brute force approach. You can see the script running on the example input online.

Howard

Posted 2012-07-16T15:41:03.903

Reputation: 23 109

1

Mathematica 236 chars

Define the list of cities:

d = {"Neapolis", "Yokogama", "Sidney", "Amsterdam", "Madrid", "Lviv", "Viden", "Denver"}

Find the path that includes all cities:

c = Characters; f = Flatten;
w = Outer[List, d, d]~f~1;
p = Graph[Cases[w, {x_, y_} /;x != y \[And] (ToUpperCase@c[x][[-1]]== c[y][[1]]) :> (x->y)]];
v = f[Cases[{#[[1]], #[[2]], GraphDistance[p, #[[1]], #[[2]]]} & /@  w, {_, _, Length[d] - 1}]];
FindShortestPath[p, v[[1]], v[[2]]]

Output:

{"Lviv", "Viden", "Neapolis", "Sidney", "Yokogama", "Amsterdam","Madrid", "Denver"}

The above approach assumes that the cities can be arranged as a path graph.


The graph p is shown below:

graph

DavidC

Posted 2012-07-16T15:41:03.903

Reputation: 24 524

1

C, 225

#define S t=v[c];v[c]=v[i];v[i]=t
#define L(x)for(i=x;i<n;i++)
char*t;f;n=0;main(int c,char**v){int i;if(!n)n=c,c=1;if(c==n-1){f=1;L(2){for(t=v[i-1];t[1];t++);if(v[i][0]+32-*t)f=n;}L(f)puts(v[i]);}else L(c){S;main(c+1,v);S;}}

Run with country names as the command line arguments

Note:

  • brute force generation of permutations
  • for checking it assumes that country names start with an upper case and end in lower case.
  • assumes there is only one answer
  • In C, assumes that the **v array of main() is writable

baby-rabbit

Posted 2012-07-16T15:41:03.903

Reputation: 1 623

Not sure it is exactly valid, but if you do #define L(x)for(int i=x;i<n;i++) and don't declare i at the beginning of main you save 1 byte. – Tsathoggua – 2018-05-04T11:33:18.040

1

J, 69 65 60 59 54 characters

Somewhat off the pace.

{.l\:+/2=/\|:tolower;"2({.,{:)@>l=.(i.@!@#A.]);:1!:1[1

Example:

   {.l\:+/2=/\|:tolower;"2({.,{:)@>l=.(i.@!@#A.]);:1!:1[1
Neapolis Yokogama Sydney Amsterdam Madrid Lviv Viden Denwer
+----+-----+--------+------+--------+---------+------+------+
|Lviv|Viden|Neapolis|Sydney|Yokogama|Amsterdam|Madrid|Denwer|
+----+-----+--------+------+--------+---------+------+------+

Gareth

Posted 2012-07-16T15:41:03.903

Reputation: 11 678

1

C#, 398

And here is C# with Linq 5 cents

IEnumerable<string>CityNameGame(string[]input){var cities=new List<string>(input);string lastCity=null;while(cities.Any()){var city=lastCity??cities.First();lastCity=cities.First(name=>string.Equals(city.Substring(city.Length-1),name.Substring(0,1),StringComparison.CurrentCultureIgnoreCase));cities.RemoveAll(name=>name==city||name==lastCity);yield return string.Format("{0}→{1}",city,lastCity);}}

Akim

Posted 2012-07-16T15:41:03.903

Reputation: 111

0

C# (.NET Core), 297 bytes

using System;
using System.Linq;
var S="";int I=0,C=s.Count();for(;I<C;I++)S=Array.Find(s,x=>s[I].Substring(0,1).ToUpper()==x.Substring(x.Length-1).ToUpper())==null?s[I]:S;for(I=0;I<C;I++){Console.Write(S+" ");S=C>I?Array.Find(s,x=>S.Substring(S.Length-1).ToUpper()==x.Substring(0,1).ToUpper()):"";}

Try it online!

using System;
using System.Linq;

var S = "";
int I = 0, C = s.Count();
for (; I < C; I++)
    S = Array.Find(
        s, x =>
        s[I].Substring(0, 1).ToUpper() == x.Substring(x.Length - 1).ToUpper()
    ) == null ?
    s[I] :
    S;
for (I = 0; I < C; I++) {
    Console.Write(S + " ");
    S = C > I ? Array.Find(s, x => S.Substring(S.Length - 1).ToUpper() == x.Substring(0, 1).ToUpper()) : "";
}

Hille

Posted 2012-07-16T15:41:03.903

Reputation: 349

0

K, 96

{m@&{&/(~).'+(,_1_1#'x),,-1_-1#'x}@'$m:{$[x=1;y;,/.z.s[x-1;y]{x,/:{x@&~x in y}[y;x]}\:y]}[#x;x]}

.

k){m@&{&/(~).'+(,_1_1#'x),,-1_-1#'x}@'$m:{$[x=1;y;,/.z.s[x-1;y]{x,/:{x@&~x in y}[y;x]}\:y]}[#x;x]}`Neapolis`Yokogama`Sidney`Amsterdam`Madrid`Lviv`Viden`Denver
Lviv Viden Neapolis Sidney Yokogama Amsterdam Madrid Denver

tmartin

Posted 2012-07-16T15:41:03.903

Reputation: 3 917