# Creating algorithm with several conditions

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

• 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}. Nov 3, 2015 at 12:41
• You should be able to adapt toString[coord_, validQ_] := If[validQ, "01", "00"] <> " X" <> ToString[coord[]] <> " Y" <> ToString[coord[]] <> "\n" and prints[list_] := toString[list[], True] <> StringJoin[ If[#[[2, 2]] == #[[1, 2]] && #[[1, 1]] + 1 == #[[2, 1]], "", toString[#[], False]] <> toString[#[], True] & /@ Partition[list, 2, 1]] to handle the case when the first line is not 01 X1 Y1. Nov 3, 2015 at 12:44
• 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. Nov 3, 2015 at 12:44
• 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? Nov 3, 2015 at 12:50
• 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 Nov 3, 2015 at 12:56

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.