Given a grid layout of Basic Latin letters as follows:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
Z
where A is at location (0,0), B is at location (0, 1), C is at (0, 2), N is at (2,3), and Z is at (5,0), write a function in Swift that outputs the "instructions" for typing out a given word. By "instructions" we mean that a cursor starts at location (0,0) and can only be moved like so:
R
- moves the cursor rightL
- moves the cursor leftU
- moves the cursor upD
- moves the cursor down
There is 5th instruction:
- E - to accept the character under the cursor.
So one way to give instructions for the word "BAT" is:
RELEDDDRRRRE
Starting at A, we go Right to get B then emit an E to accept the B. Then Left then Enter to get the A. Then three D's and four R's and finally an E to accept the T. You do not have to move back to the beginning.
You may not put the cursor over any row and column that does not have a letter. So if the cursor is on Z, you cannot move Right.
The Swift function should accept a string (like "BAT") and return the instruction string ("RELEDDDRRRRE"). Make sure to indicate if the input string is illegal in the proper idiomatic way.