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.
{2, 2}
. $\endgroup$toString[coord_, validQ_] := If[validQ, "01", "00"] <> " X" <> ToString[coord[[1]]] <> " Y" <> ToString[coord[[2]]] <> "\n"
andprints[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 not01 X1 Y1
. $\endgroup$y
coordinate should be the same as the current one, while the previousx
coordinate should be 1 less than the current one, is that right? $\endgroup$