Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Suppose I have a list that looks like the following (Town names and total distance to that town)

resupply = {
  {"Coleman", 0},
  {"Highwood House", 106},
  {"Canmore", 229},
  {"Exshaw", 245},
  {"Ghost Station", 288},
  {"MountainAire", 370},
  {"Nordegg", 552},
  {"Robb", 672},
  {"Hinton", 720}
  }

How would I process that list to give me something like

Coleman - Highwood House 106 km

Highwood House - Canmore 123 km

Canmore - Exshaw 16 km

and so on.

The output gives me the two towns being travelled between, and the distance between them.

share|improve this question
    
Nice! Appreciate the quick response... that helps me a ton. – Tom De Vries 20 hours ago
    
If you only knew the most basic functions in Mathematica, you could still do this using a Table ... Table[ fun[ resupply[[i]], resupply[[i+1]] ], {i, 1, Length[resupply]-1}]. Partition is of course better. My point is that it is usually possible to construct a reasonable solution using only a small, core part of the language. – Szabolcs 19 hours ago
1  
Or how about MapThread[fun, {Most[resupply], Rest[resupply]}]? fun[{name1_, dist1_}, {name2_, dist2_}] := name1 <> " - " <> name2 <> " " <> ToString[dist2 - dist1] There are countless ways – Szabolcs 19 hours ago
1  
from the examples you give, it seems that all towns are linearly connected to a single road or railroad. So you treat it as a 1-dimensional map. Is that the idea? – Wouter 14 hours ago
StringTemplate["`1` - `3` <*#4-#2*> km"] @@@  Flatten /@ Partition[resupply, 2, 1]
{
 "Coleman - Highwood House 106 km", 
 "Highwood House - Canmore 123 km", 
 "Canmore - Exshaw 16 km", 
 "Exshaw - Ghost Station 43 km", 
 "Ghost Station - MountainAire 82 km", 
 "MountainAire - Nordegg 182 km", 
 "Nordegg - Robb 120 km", "Robb - Hinton 48 km"
}
share|improve this answer
1  
saves one character: Partition[Flatten @ resupply, 4, 2] – Mr.Wizard 1 hour ago
{place, dist} = Transpose[resupply];
With[{n = Length@resupply}, 
 TableForm[
  Partition[Abs[#1 - #2] & @@@ (dist[[#]] & /@ Tuples[Range[n], 2]), 
   n], TableHeadings -> {place, place}]]

enter image description here

Outer could have been used instead of Tuples.

Or:

pos = Subsets[Range[Length@resupply], {2}];
pairs = #1 <> "-" <> #2 & @@@ (place[[#]] & /@ pos);
d = Abs[#1 - #2] & @@@ (dist[[#]] & /@ pos);
Grid[Transpose[{pairs, d}], Alignment -> Left]

enter image description here

share|improve this answer

The Terse Way

{-#, #2 "km"} & @@@ Differences[resupply] // TraditionalForm

$\left( \begin{array}{cc} \text{Coleman}-\text{Highwood House} & 106 \text{ km} \\ \text{Highwood House}-\text{Canmore} & 123 \text{ km} \\ \text{Canmore}-\text{Exshaw} & 16 \text{ km} \\ \text{Exshaw}-\text{Ghost Station} & 43 \text{ km} \\ \text{Ghost Station}-\text{MountainAire} & 82 \text{ km} \\ \text{MountainAire}-\text{Nordegg} & 182 \text{ km} \\ \text{Nordegg}-\text{Robb} & 120 \text{ km} \\ \text{Robb}-\text{Hinton} & 48 \text{ km} \\ \end{array} \right)$

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.