28
3
EDIT I modified the wording of the rules to make some things which were implicit more explicit. I also added some emphasis to clear up some points of apparent confusion, and explicitly defined the option of making a standalone program instead of a function.
The goal here is to make a function that takes a text file (or string) and transposes it so that lines become columns and vice versa.
Example:
I am a text. Transpose me. Can you do it?
Results in:
ITC ra aan mn sy apo ou ts eed x o tm .ei .t ?
The rules:
- You are allowed to assume that the only whitespace characters used are
" "
and"\n"
and that there is no trailing whitespace on any line. - You may assume that the file is ASCII. Which endline marker you want to use is up to you.(CRLF or LF). It must work correctly on the example, but it should also work on any input that satisfies the above assumptions.
- You might have to insert spaces (as in the example) where there were none in order to keep the columns in line.
- Your result must not have trailing whitespace on any line.
- The final newline character(for the last line) is optional.
- It should be either a function or a complete program. If your function accepts a string, then it should return the results as a string. If it accepts a filename, then you return the name of the file where you saved the result. You are additionally allowed to write a complete program that accepts input from STDIN and outputs the correct result to STDOUT; if you do this, you must not output anything to STDERR.
- Shortest procedure wins, but I will upvote any answer I like.
Based on the rules, the output on the example is either 53 or 52 bytes long (for LF newlines) depending on whether the final newline is included or not.
Note: It is not a specific requirement, but if your function, when run twice successively is not identical to the original(the final newline might be differ, and any empty lines at the end of the file will be deleted), then you are probably breaking one of the rules.
Later duplicate: http://codegolf.stackexchange.com/q/85255
– msh210 – 2016-07-12T18:08:25.743I decided to remove the prohibition on language built-ins. – Tim Seguine – 2014-01-03T13:27:09.373
I edited to clarify the trailing whitespace condition. – Tim Seguine – 2014-01-03T14:16:44.047
Are you asking for a function? Is it acceptable to accept a string from STDIN and print the correct output to STDOUT? – Justin – 2014-01-03T22:56:18.353
@Quincunx Yes, I am accepting that as a "function".I will alter the rules to be explicit on that point. – Tim Seguine – 2014-01-04T12:30:12.087
Are blank lines allowed in the input, eg
"abc\n\ndef"
? – Lyndon White – 2018-07-25T10:53:12.570@LyndonWhite I don't remember anymore what I originally intended, but reading what I wrote, I would say yes. – Tim Seguine – 2018-07-25T12:33:41.333
Are trailing new lines allowed? – Asone Tuhid – 2018-07-30T13:20:53.040
@AsoneTuhid I think at this point it is best to say, that is unspecified. If you are following the "no trailing whitespace" rule I don't think it should make a difference. But if it does, then choose whichever you prefer. – Tim Seguine – 2018-07-30T13:38:44.247
The text transpose function cannot be an involution unless you allow for trailing ws. Example: "ac\ndef\n" ->TT-> "a\ncd\nef\n" ~ "a\ncd\nef\n" ->TT-> "acd\nef\n", where *=ws – Emanuel Landeholm – 2014-03-19T05:28:20.960
@EmanuelLandeholm it wasn't intended to be perfect. It was intended to be an involution up to trailing whitespace. "You might have to insert spaces (as in the example) where there were none in order to keep the columns in line." This never requires a whitespace character to be inserted at the end of a line. The correct transformation for your example according to the rules is: "ac\ndef\n"->"ad\ne\ncf\n"->"a*c\ndef\n" Note there is no trailing whitespace at any step and on this input it is an involution. – Tim Seguine – 2014-03-19T09:49:23.653