0
$\begingroup$

I'm pretty new to mathematica and was wondering if you guys could help me out.

Example: We have the following coordinates:

  • {1, 1}, {2, 1}, {3, 1}, {5, 1}, {1, 2}, {2, 2}, {3, 2}, where {x,y}

And I need an algorithm which returns (or prints) "01 Xx Yy" if the y-coordinate is the same as next in the list and if the x-coordinate is in steps of 1. If the conditions isn't true it returns "00 Xx Yy" and then "01 Xx Yy".

So the output would be:

  • 01 X1 Y1
  • 01 X2 Y1
  • 01 X3 Y1
  • 00 X5 Y1
  • 01 X5 Y1
  • 00 X1 Y2
  • 01 X1 Y2
  • 01 X2 Y2
  • 01 X3 Y2

Thanks. Sorry for bad english.

$\endgroup$
7
  • 1
    $\begingroup$ As it stands, the question is not asking for help with Mathematica, but rather with the development of an algorithm. It is also not clear, what the expected output is if the first element of the list of coordinates is, say, {2, 2}. $\endgroup$
    – LLlAMnYP
    Nov 3, 2015 at 12:41
  • $\begingroup$ You should be able to adapt toString[coord_, validQ_] := If[validQ, "01", "00"] <> " X" <> ToString[coord[[1]]] <> " Y" <> ToString[coord[[2]]] <> "\n" and prints[list_] := toString[list[[1]], True] <> StringJoin[ If[#[[2, 2]] == #[[1, 2]] && #[[1, 1]] + 1 == #[[2, 1]], "", toString[#[[2]], False]] <> toString[#[[2]], True] & /@ Partition[list, 2, 1]] to handle the case when the first line is not 01 X1 Y1. $\endgroup$ Nov 3, 2015 at 12:44
  • $\begingroup$ The first element of the list of coordinates should be "01 Xx Yy". I'm trying to learn mathematica because it's the program I'm going to use in the future. I can make the algorithm, but not in mathematica, that's the problem. $\endgroup$
    – Rob1234
    Nov 3, 2015 at 12:44
  • $\begingroup$ Ok, by the looks of your expected output, to get a 01, the next y coordinate should be the same as the current one, while the previous x coordinate should be 1 less than the current one, is that right? $\endgroup$
    – LLlAMnYP
    Nov 3, 2015 at 12:50
  • $\begingroup$ it should not be 1 less, it depends on the x- and y-coordinates, but everytime the y-coordinate changes or the x-coordinates jumps in steps over 1, it changes to 00 Xx Yy and then 01 Xx Yy $\endgroup$
    – Rob1234
    Nov 3, 2015 at 12:56

1 Answer 1

2
$\begingroup$

To the extent that I understood the OP, here's a code.

First, the input list:

list = {{1, 1}, {2, 1}, {3, 1}, {5, 1}, {1, 2}, {2, 2}, {3, 2}};

Now pad the list to the left and right with elements satisfying the conditions:

listpad = {First@# - {1, 0}}~Join~#~Join~{Last@# + {1, 0}} &@list
(* {{0, 1}, {1, 1}, {2, 1}, {3, 1}, {5, 1}, {1, 2}, {2, 2}, {3, 2}, {4, 2}} *)

Each element needs to be compared to the one before and after, so let's partition this appropriately.

listpart = Partition[listpad, 3, 1]
(*
{{{0, 1}, {1, 1}, {2, 1}},
 {{1, 1}, {2, 1}, {3, 1}},
 {{2, 1}, {3, 1}, {5, 1}},
 {{3, 1}, {5, 1}, {1, 2}},
 {{5, 1}, {1, 2}, {2, 2}},
 {{1, 2}, {2, 2}, {3, 2}},
 {{2, 2}, {3, 2}, {4, 2}}}
*)

The 2nd coordinate from each sublist is the one under scrutiny, its first element should be 1 greater than in the 1st coordinate of each sublist, its second element should be equal to that in the 3rd coordinate of each sublist.

Expressed in mathematica, this condition is

Last[#3 - #2] == 0 && First[#2 - #1] == 1

Let's roll this into a function which does stuff depending on the satisfaction of this condition (EDIT: I've replaced Which with If as there's only two outcomes):

f = 
  If[Last[#3 - #2] == 0 && First[#2 - #1] == 1,
    "01 X" <> ToString@First@#2 <> " Y" <> ToString@Last@#2,
  Unevaluated@
    Sequence[
      "00 X" <> ToString@First@#2 <> " Y" <> ToString@Last@#2, 
      "01 X" <> ToString@First@#2 <> " Y" <> ToString@Last@#2]
  ] &

Now

f @@@ listpart // TableForm

returns

01 X1 Y1
01 X2 Y1
01 X3 Y1
00 X5 Y1
01 X5 Y1
00 X1 Y2
01 X1 Y2
01 X2 Y2
01 X3 Y2

The result of f @@@ somelist is usually a list of as many elements, as in somelist, so I use Unevaluated@Sequence[...] here, which allows the result of applying f to an element of somelist to be a sequence of two elements splatted into the output, therefore the output becomes longer than the input. Unevaluated is necessary, otherwise while defining the function, the Sequence would splat itself into the arguments of the If statement and not into the end result.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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