Rip-off of a rip-off of a rip-off of a rip-off. Go upvote those!
Your task, if you wish to accept it, is to write a program/function that outputs/returns its input/argument1. The tricky part is that if I transpose your source code2, the output/result must be transposed too.
You may choose which kind of 2D data your solution can accept. For example a list of lists, a matrix, a list of strings, etc. State which one(s) it handles. You may assume that the input will always be rectangular and will have a length of 1 or more along each dimension.
For the purposes of transposing, short lines in your source code will be considered as padded with trailing spaces until it is rectangular, however, these padded trailing spaces do not contribute to your code length.
Since this is code-golf, the target is to optimize the byte count of the original source code (not the transposed version).
Examples
Let's say your solution takes a numeric matrix and your source code is
AB
DEF
and its input/argument is [[1,2],[3,4],[5,6]]
. If I write
AD
BE
F
instead and run it, the output/result must be [[1,3,5],[2,4,6]]
.
Let's say your solution takes a linefeed-separated string and your source code is
ABC
and its input/argument is "96\n"
. If I write
A
B
C
instead and run it, the output/result must be "9\n6\n"
.
-
25\$\begingroup\$ My god. Can we stop? \$\endgroup\$S.S. Anne– S.S. Anne2019年09月22日 16:30:33 +00:00Commented Sep 22, 2019 at 16:30
-
3\$\begingroup\$ @Night2 No, it complicates matters. \$\endgroup\$Adám– Adám2019年09月22日 16:30:47 +00:00Commented Sep 22, 2019 at 16:30
-
15\$\begingroup\$ @JL2210 No, I have a big one in the works. \$\endgroup\$Adám– Adám2019年09月22日 16:31:08 +00:00Commented Sep 22, 2019 at 16:31
-
7\$\begingroup\$ These challenges are getting impossible in procedural languages without serious comment abuse. \$\endgroup\$S.S. Anne– S.S. Anne2019年09月22日 16:32:18 +00:00Commented Sep 22, 2019 at 16:32
-
2\$\begingroup\$ @JL2210 Transforms. \$\endgroup\$Adám– Adám2019年09月22日 16:42:04 +00:00Commented Sep 22, 2019 at 16:42
25 Answers 25
Python 3 + numpy, 45 bytes
lambda\
a:a
ma= """
b. "
dT" "
a "
\ """
Thanks to @EriktheOutgolfer pointing out a bug of the previous version
Transposed:
lambda\
a:a.T
ma= """
b "
d " "
a "
\ """
The functions take a numpy matrix as input and output a numpy matrix. The solution does not rely on comments like many other solutions do but instead exploits multi-line strings.
-
3\$\begingroup\$ That's really neat! \$\endgroup\$Adám– Adám2019年09月22日 19:43:20 +00:00Commented Sep 22, 2019 at 19:43
-
\$\begingroup\$ Hm, I'm pretty sure that the transposed version will be padded with spaces, so the last line will contain trailing spaces, therefore the
\
will throw aSyntaxError
. You need to move the quotes in both directions to fix this. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2019年09月22日 20:36:40 +00:00Commented Sep 22, 2019 at 20:36 -
\$\begingroup\$ @EriktheOutgolfer Fixed. Thanks. \$\endgroup\$Joel– Joel2019年09月22日 20:54:25 +00:00Commented Sep 22, 2019 at 20:54
Jelly, 2 bytes
Input is a list of lists. Thanks to Luis Mendo and Nick Kennedy for improving the answer.
ZZ
Try it online! Try it transposed!
The program transposes the input twice, returning the original input. The transposed version ignores the first line and only transposes once.
R, (削除) 5 (削除ここまで) 4 bytes
#t
I
An R function, either the identity function I
or the transpose function t
when transposed. Footer on TIO shows output of both.
Thanks to @RobinRyder for saving a byte!
-
\$\begingroup\$ You don't need the final
#
. \$\endgroup\$Robin Ryder– Robin Ryder2019年09月22日 18:58:08 +00:00Commented Sep 22, 2019 at 18:58 -
\$\begingroup\$ @RobinRyder thanks! For some reason I was padding to a rectangle even though I’d seen the statement that it was unnecessary. \$\endgroup\$Nick Kennedy– Nick Kennedy2019年09月22日 18:59:57 +00:00Commented Sep 22, 2019 at 18:59
C (gcc), (削除) 209 (削除ここまで) (削除) 205 (削除ここまで) (削除) 203 (削除ここまで) 201 bytes
Normal
f(n,L,r,c)char**L;{for(c=0;0?L+ c:c<n;c+=puts(""))for(r=0;0?r :L[c][r];r++)putchar(L[ c ][ r ]);}/*
\\\ \\ 1 [0][] \ \\\ \ \\ 1 <n \ \\\\\\ r+-c c+-r */
Transposed
f
(
n
,
L
,
r
,
c
)
c\
h\
a\
r
*
*
L
;
{
f\
o\
r
(
c
=
0
;
01
?
L[
+0
]
[
c]
:
c
<
n
;
c
+\
=
p\
u\
t\
s
(
"\
"
)
)
f\
o\
r
(
r
=
0
;
01
?
r<
n
:
L
[
c
]
[
r
]
;
r
+\
+
)
p\
u\
t\
c\
h\
a\
r
(
L
[r
+
c-
c
]
[c
+
r-
r
]
)
;
}
/*
*/
Haskell, 51 bytes
This version is valid, but will not halt when given []
as input.
f
--(:[|,<zabf=]f
--abx(y-i$]
-- ):x) pf;x[:x
y =y
Transposed, 75 bytes
f---
---y
(a
:b)=
[x:y
|(x
,y)
<-
zip
a$f
b];
f x
= [
] :
f x
Haskell, 51 bytes
This version is valid, but crashes with []
as input for the transposed version.
f
--d[(idi)z[.]d!0
-- m!)|,<i0.$ !]
-- a! (_-p
p=p
Haskell, (削除) 75 (削除ここまで) (削除) 67 (削除ここまで) 57 bytes
At least 7 bytes saved thanks to Ørjan Johansen
This version outputs []
when given []
as input.
f
--[[d(i<di)z[.$!0
--]]=!)$|,<i0.d!]
-- ;[! >(_-p ]
f=f
-
\$\begingroup\$ 60 bytes \$\endgroup\$Ørjan Johansen– Ørjan Johansen2019年09月23日 04:23:03 +00:00Commented Sep 23, 2019 at 4:23
-
\$\begingroup\$ @ØrjanJohansen Thanks! I had tried something like that earlier but I couldn't get it to work. There is also a pretty easy way to take 3 more bytes off of your 60. \$\endgroup\$2019年09月23日 04:30:13 +00:00Commented Sep 23, 2019 at 4:30
Python 3, 51 bytes
lambda\
a:a
mz= '''
bi' '
dp' '
a('
*
a
\)'''
Based on Joel's approach. The input is a tuple
of tuple
s, and the output is a zip
(converted to a tuple
over TIO for better visibility).
Transposed:
lambda \
a:zip(*a)
ma=''' '
b '
d ' '
a '
\ '''
Perl 6, (削除) 20 (削除ここまで) 11 bytes
#&
*[
#Z
]
Transposed
#*#
&[Z]
Both functions work on list of lists.
*[ ]
is a WhateverCode returning the zen slice of its argument, effectively the identity function.
&[Z]
is the zip operator.
JavaScript (Node.js), 62 bytes
//m0()m ]
//=](=a )
//>.a>p( )
//mm,m/aa
//[ap./=[
p =>p
Improved with a different layout
/////
/////
m=>m[
0].map
((a,p
)=>m.
map//=
(a=>
a[p
]))
Haskell, (削除) 185 (削除ここまで) 161 bytes
t i
=
i
af
n m
u a e i
l p s d
lu h=u
(
h
e
a
d
a
)
t
h
e
n
[
]
e
l
s
e
m
a
p
h
e
a
d
a
:
t
(
m
a
p
t
a
i
l
a
)
Transposed:
t a
=if null(head a)then[]else map head a:t(map tail a)
i u
map
h
es=
u
id
No comments, no string literals, just some additional function definitions.
Edit: -24 bytes thanks to @Ørjan Johansen.
-
2\$\begingroup\$ 161 bytes \$\endgroup\$Ørjan Johansen– Ørjan Johansen2019年09月24日 04:53:28 +00:00Commented Sep 24, 2019 at 4:53
PHP (7.4), (削除) 114 (削除ここまで) (削除) 86 (削除ここまで) 70 bytes
My first ever experience doing something like this in PHP, there must be a better way I cannot see! Input is an array of arrays like [[1,2],[3,4],[5,6]]
.
Normal:
fn($a)=>$a/*
nu /
(l
$l
a,
).
=.
>.
a$
ra
r)
a/
y*
_
m
a
p
(*/
Transposed (space padded):
fn($a)=>array_map(
null,...$a)/* *
( /
$
a
)
=
>
$
a
/
*/
Brain-Flak (BrainHack), (削除) 382 (削除ここまで) (削除) 375 (削除ここまで) 337 bytes
No comments!
( <( <>)<> ><>) ({}) {}{} {( )()<({}<{}<><>>{}<><>{}) ( <>) ({}<><( [ ]({}<{}( )<({}()<{}<><>>){} ><><{}<< ><> ( [ ]( <>)<>)>{}<>>>)){}>)> ( ){ {}[]} <>[]{
(({}({} ( )) <>( ))[( [ ])])({}[ ] [ ]( ) ( ) < ><>{} <>( ){{}()<( )( ({} { [ ]( ) } <>) ( (()) {{}()<{} >} ) ) >} )}[] {} ( ){} ( ){}({}<>)<>([])}<>
For the input the first two numbers are the dimensions of the matrix and the remainder is the contents of the matrix. Output is given in the same format.
Transposed
Brain-Flak (BrainHack), 465 bytes
(
(
({
}
<(
({
}
<
>
)(
<
>)
)
>
<
>
)<
>
((
{
})
))
[
(
{
}[
{
}]
)
]
)
{(
({
}
)[
(
)]
<
([
{
}]
<(
{
}
<
>)
<
>
>(
{
}
<
>)
<
><
{
}>
)<
>
({
}
<
>
)<
>
((
{
}
<
>)
<{
({
}
[(
)
]<
((
{
})
<(
{
}(
({
}
)
<{
(
{
}[
(
)]
<(
{
}
<
>)
<
>
>
)}
{
}<
>
>)
<
>
<(
{
}
<(
<(
)
>)
<
>
{
({
}
[(
)
]<
({
}
<
>
)
<
>>
)}
>
{
})
<
>
>)
>
)>
)}
{
}
>
)
>)
}
([
]
)
{{
}
{
}(
[
])
}{
}
<
>(
[
])
{{
}
(
{
}
<
>
)
<
>
(
[
]
)
}
<
>
Charcoal, 19 bytes
A¿0«
‖0¿
↗0
¿0
0¿
«
Try it online! Takes input as an array of strings. Explanation: A
implicitly prints the explicit input, while ¿0
is a conditional, with 0
as the condition, which is therefore always false. «
then starts a block of meaningless Charcoal code which never gets executed. (It might be possible to remove some of those bytes but in that case I'm not sure whether Charcoal would parse the program correctly.) Transposed, 17 bytes:
A‖↗¿0«
¿000¿
0¿
«
Try it online! Explanation: Much the same as the previous program, except for the addition of the transpose command ‖↗
.
I have an alternative solution where both the original and transposed program are 18 bytes:
A⊞υ"y
‖υ+y
↗+
⊞+
υ
Try it online! Explanation: A
as above; ⊞υ
pushes a value to the predefined empty list (which doesn't affect the output); "y
begins an arbitrary string (ends at end of program or matching "
). Transposed:
A‖↗⊞υ
⊞υ++
υ+
"y
y
Try it online! Explanation: A‖↗
as above; ⊞υ
as above; the minor difference here is that I'm pushing concatenations of smaller strings, since I don't want to repeat the "
.
Haskell, (削除) 153 (削除ここまで) 144 bytes
(thanks, Sriotchilism O'Zaic)
f
[
]=
[
];
f(
x:
l)
=(
:)
x l
-- : z $
-- f i f
-- [ p
-- ] W
-- i
-- t
-- h
-
\$\begingroup\$ Here is a 144 byte version while maintaining your structure. (Transposed). \$\endgroup\$2019年09月25日 03:44:58 +00:00Commented Sep 25, 2019 at 3:44
-
\$\begingroup\$ Here is a 70 byte version that is still similar but with some structural changes. (Transposed). \$\endgroup\$2019年09月25日 04:08:30 +00:00Commented Sep 25, 2019 at 4:08
-
\$\begingroup\$ Thanks. The second version lacks the central idea though (re-using the
foldr
recursion for both versions), don't like that. \$\endgroup\$ceased to turn counterclockwis– ceased to turn counterclockwis2019年09月25日 07:01:46 +00:00Commented Sep 25, 2019 at 7:01 -
\$\begingroup\$ You know, this challenge and your username kinda contradict each other, since your code did turn counterclockwise in that transposed version. ;p \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2019年09月25日 07:50:10 +00:00Commented Sep 25, 2019 at 7:50
Ruby, 35 bytes
#-sz##
->hip{
hip }
#{f
#ht*
#..h
Hip to be square! (Almost)
Accepts ruby matrices as input (arrays of arrays)
APL (Dyalog Unicode), 7 bytes
{⍵
⍉
⍵}
Transposed:
{⍉⍵
⍵ }
Probably a rather boring answer, but anyway here it goes.
The inline function definition {...}
can span multiple lines. In this case, each line is sequentially executed, but any line without an assignment ←
immediately returns its computed value. So the first function returns ⍵
while the second returns ⍉⍵
.
Even more boring answer would be the comment abuse:
APL (Dyalog Unicode), 4 bytes
⍝⍉
⊢
No TIO is necessary, I guess...
Clean, 64 bytes
//i$
//mp
$ p=p
//ot
//rr
//ta
// n
//Ss
//tp
//do
//Ls
//ie
//b
//$//////////
// //////////
import StdLib
$p=transpose
p
Runic Enchantments, 88 bytes
vrlril1-{)?\:',=4*?~r0[
i! '${U [0r/?*7̸0l{$
$ka6 ̹\!,ドル'/;? = ̹
' ̸
$
Try it online!
Try it transposed!
Input is space separated for each value and comma separated for each row (newlines are optional) and supports both strings and (non-zero) numbers. This makes parsing the input easier as input is automatically broken by spaces and newlines. So for example, the input:
1 2 3 , 4 5 6 , 7 8 9
Would be represented in nested array form as [[1,2,3],[4,5,6],[7,8,9]]
. Minimal support for jagged arrays (only the last one can be short), but as inputs are expected to be rectangular, this satisfies that requirement.
Output is represented in the same format (transposed version outputs with newlines, but its zero bytes different to use a space instead). Normal version has a trailing space, transposed version has a trailing comma and newline (due to the difficulty of determining when there is no more data to print).
Normal version does have modifier characters in weird places (eg. 7̸0
), but this is due to needing them in the right place when the source is transposed and the normal execution only utilizes the leftmost column of instructions.
Explanation
Explanation of the transposed source will be in a non-transposed form. Arrows will represent IP directionality at the entrance and exit of various blocks.
→rlril1-{)?\:',≠4*?~r0[ Initial position. Read and parse input.
↓ Input loop exit and direction
The input is read, and when a ,
character is found, a new substack is pushed. This allows each stack to hold each row separately in memory. Enters next section from the top.
↓
.. '${̹L [0r/?*7≠0l{̹$ When no more input:
↑ ↓
[0
(executed leftwards) sets up a an empty stack to act as a boundary between the first row and the last row and then rotates to the first stack ({̹
) and starts printing elements and rotating stacks. The i!
is not executed and {̹L [0r
is only executed once. When a zero-size stack is found, the loop exits.
↑ ↓
$ka6 \!,ドル'/;? Row separation formatting
When the empty-stack is found, a ,
and newline are printed, and value loop is re-entered. Execution order (mirrored for readability) where .
is an un-executed command: \',$!.6ak$?....../
. Changing ak
to '<space>
would print spaces instead of newlines.
Execution halts when the program attempts to write from an empty stack; this is why only last-row jagginess works correctly and why there is a trailing ,
in the output: the comma has already been printed before the program knows that there's no more data.
05AB1E, 4 bytes
øø
q
øq
ø
Explanation:
Unlike some of the other languages, newlines are simply ignored in 05AB1E, so I don't think a 2-byter is possible (although I'd love to be proven wrong).
ø # Transpose the (implicit) input
ø # Transpose it back
q # Stop the program (and output the top of the stack implicitly as result)
ø # Transpose the (implicit) input
q # Stop the program (and output the top of the stack implicitly as result)
ø # No-op, since the program has already stopped
-
\$\begingroup\$ That's 4 bytes, not 3... \$\endgroup\$naffetS– naffetS2022年06月25日 16:49:38 +00:00Commented Jun 25, 2022 at 16:49
-
\$\begingroup\$ @Steffan Fixed. I think I might have misread the rule about trailing spaces not counting, as all whitespaces not counting instead. But it's been a while, so not sure why I put it as 3 bytes anymore; could also just have been a typo. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2022年06月26日 11:36:14 +00:00Commented Jun 26, 2022 at 11:36
Vyxal, 2 bytes
#∩
Try it online or try it online transposed.
Receives a list of lists (a list of strings will work too).
-2 bytes thanks to lyxal
-
\$\begingroup\$ Why not just comment then tranpose? \$\endgroup\$2022年06月26日 12:31:22 +00:00Commented Jun 26, 2022 at 12:31
Python 3, (削除) 118 (削除ここまで) 95 bytes
Input and result are lists of tuples.
f=lambda i:i;"""
= "
l "
a
m
b
d
a
i
:
[
*
z
i
p
(
*
i
)
]
;
"
"
"""
Cjam, 13 bytes
qo
~
z
`
Transposed version:
q~z`
o
Input format
The input format is the standard CJam array format: [[1 2] [3 4]]
No crash version, 12 bytes
The normal version crashes after printing the array. A version which doesn't crash would be:
qo{
~
z
` };
or transposed:
q~z`
o
{ };
There are probably a few extra bytes that can be saved, i accidentaly worked on the transposed version first, which led to a few extra linebreaks and it's been a long time since i last used CJam. Any improvements welcome.
Zsh, 75 bytes
<<<${(F)@}
fa<
o[<
r+$
+{
si(
;]F
i+)
==a
&$}
&s
r[
ei
p+
e1
a]
t
$
#
s
TIO: Normal Transposed
The garbage below the main print is harmless, it prints an error when encountering a newline after fa<
and exits. 86 bytes to remove that error.
The transposed version is here. After printing it errors on seeing <
at the end of a line and exits.
for s;i=&&repeat $#s
a[++i]+=$s[i+1]
<<<${(F)a}
<
<
$
{
(
F
)
@
}
Pyth, 10 bytes
CCQ "
Q
"
Transposed
CQ "
C
Q
"
Explanation
The program transposes the input twice, returning the original input. The transposed version only transposes once.