I came across this question on SO and thought it'd make a nice golf challenge. So here it is:
Write a program that reads a sequence of character strings, one per line, and outputs a list of all positions where each string has the same character.
Input and output:
The input consists of one or more lines of printable non-whitespace ASCII characters, each followed by a newline. You may assume that all input lines have the same length. The newline is not to be considered part of the input (i.e. you should not output it as a matching character).
Example input (shamelessly stolen from the SO question):
abcdefg avcddeg acbdeeg
After reading the input, your program should print the positions of each matching column and the characters they contain. (Your program may, but need not, stop reading further input if it can determine early that there are no matching columns.) Any reasonable output format is permitted; in particular, you may use either 0-based or 1-based indexing for the positions.
Example output for the above input (using 0-based indexing):
0: a 3: d 6: g
This is code golf, so shortest answer wins. In the event of a tie, fractional tie-breaker chars may be awarded for additional features:
- −½ chars for correctly handling input lines of unequal length. (Output should not contain positions past the end of the shortest input line.)
- −¼ chars for correctly handling input consisting of arbitrary UTF-8 encoded Unicode characters.
For inspiration, you may find some ungolfed solutions at the SO question (see above).
Simply concatenating the positions and characters, as in
0a3d6g, does not count as "reasonable output". You should provide some kind of separator (such as a space) between each element of the output so that it can be parsed unambiguously.
The input will be provided on the standard input stream (
stdin), or using whatever text file input mechanism is most natural to your language of choice. (If your chosen language doesn't have a natural mechanism for file input, do whatever seems closest in spirit.)
The input ends when there is no more data to be read (i.e. when an end-of-file condition occurs). If you wish, you may require that the input be terminated by a blank line (which you then should not count as part of the input, obviously). If you do so, please mention it in your answer so that others can provide correct input for testing.
Every input line, including the last one, ends with a newline character. Your answer must not report this newline as a matching column. (It's fine if your solution can also handle input where the last line doesn't end in a newline, but that's not required.)