31
\$\begingroup\$

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/argument¹. The tricky part is that if I transpose your source code², the output/result must be transposed too.

  1. 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.

  2. 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 , 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".

\$\endgroup\$
  • 23
    \$\begingroup\$ My god. Can we stop? \$\endgroup\$ – JL2210 Sep 22 at 16:30
  • 3
    \$\begingroup\$ @Night2 No, it complicates matters. \$\endgroup\$ – Adám Sep 22 at 16:30
  • 13
    \$\begingroup\$ @JL2210 No, I have a big one in the works. \$\endgroup\$ – Adám Sep 22 at 16:31
  • 7
    \$\begingroup\$ These challenges are getting impossible in procedural languages without serious comment abuse. \$\endgroup\$ – JL2210 Sep 22 at 16:32
  • 2
    \$\begingroup\$ @JL2210 Transforms. \$\endgroup\$ – Adám Sep 22 at 16:42

21 Answers 21

26
\$\begingroup\$

Python 3 + numpy, 45 bytes

lambda\
a:a
ma= """
b.    "
dT"   "
a "
\ """

Try it online!

Thanks to @EriktheOutgolfer pointing out a bug of the previous version

Transposed:

lambda\
a:a.T  
ma= """
b     "
d "   "
a "    
\ """  

Try it online!

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ That's really neat! \$\endgroup\$ – Adám Sep 22 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 a SyntaxError. You need to move the quotes in both directions to fix this. \$\endgroup\$ – Erik the Outgolfer Sep 22 at 20:36
  • \$\begingroup\$ @EriktheOutgolfer Fixed. Thanks. \$\endgroup\$ – Joel Sep 22 at 20:54
15
\$\begingroup\$

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.

\$\endgroup\$
13
\$\begingroup\$

R, 5 4 bytes

#t
I

Try it online!

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!

\$\endgroup\$
  • \$\begingroup\$ You don't need the final #. \$\endgroup\$ – Robin Ryder Sep 22 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 Sep 22 at 18:59
10
\$\begingroup\$

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    */

Try it online!

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
] 
) 
; 
} 
/*
*/

Try it online!

\$\endgroup\$
8
\$\begingroup\$

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

Try it online!

Transposed, 75 bytes

f---
 ---y
 (a
 :b)=
 [x:y
 |(x
 ,y)
 <-
 zip
 a$f
 b];
 f x
 = [
 ] :
 f x

Try it online!

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

Try it online! Transposed

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

Try it online!

Transposed

\$\endgroup\$
  • \$\begingroup\$ 60 bytes \$\endgroup\$ – Ørjan Johansen Sep 23 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\$ – Sriotchilism O'Zaic Sep 23 at 4:30
7
\$\begingroup\$

Python 3, 51 bytes

lambda\
a:a
mz= '''
bi'   '
dp'   '
a('
 *
 a
\)'''

Try it online!

Based on Joel's approach. The input is a tuple of tuples, and the output is a zip (converted to a tuple over TIO for better visibility).

Transposed:

lambda  \
a:zip(*a)
ma='''  '
b       '
d '     '
a '      
\ '''    

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Perl 6, 20 11 bytes

#&
*[
#Z
 ]

Try it online!

Transposed

#*# 
&[Z]

Try it online!

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.

\$\endgroup\$
5
\$\begingroup\$

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
 )

Try it online!

Transposed:

t  a
 =if null(head a)then[]else map head a:t(map tail a)
i       u
     map
        h
      es=
        u
      id

Try it online!

No comments, no string literals, just some additional function definitions.

Edit: -24 bytes thanks to @Ørjan Johansen.

\$\endgroup\$
4
\$\begingroup\$

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
(*/

Try it online!

Transposed (space padded):

fn($a)=>array_map(
null,...$a)/*    *
(                /
$                 
a                 
)                 
=                 
>                 
$                 
a                 
/                 
*/                

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Charcoal, 19 bytes

A¿⁰«
‖⁰¿
↗⁰
¿⁰
⁰¿
«

Try it online! Takes input as an array of strings. Explanation: implicitly prints the explicit input, while ¿⁰ 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‖↗¿⁰«
¿⁰⁰⁰¿
⁰¿
«

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: 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 .

\$\endgroup\$
3
\$\begingroup\$

Brain-Flak (BrainHack), 382 375 337 bytes

No comments!

  ( <( <>)<> ><>) ({})  {}{}   {( )()<({}<{}<><>>{}<><>{}) ( <>) ({}<><( [ ]({}<{}( )<({}()<{}<><>>){} ><><{}<< ><> ( [ ]( <>)<>)>{}<>>>)){}>)> ( ){ {}[]} <>[]{
(({}({}  ( ))   <>( ))[( [ ])])({}[ ] [ ](   )  (   ) < ><>{}  <>(   ){{}()<( )( ({} {  [ ](   )   } <>)  (  (())  {{}()<{}    >}  )  ) >}    )}[] {} ( ){} ( ){}({}<>)<>([])}<>

Try it online!

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

 (
 (
({
 }
<(
({
 }
<
>
)(
<
>)
 )
>
<
>
)<
 >
((
{
})
))
 [
 (
{
}[
{
}]
 )
 ]
 )
{(
({
 }
)[
(
)]
<
([
{
}]
<( 
{
}
<
>)
<
>
>(
{
}
<
>)
<
><
{
}>
)<
 >
({
 }
<
>
)<
 >
((
{
}
<
>)
<{
({
 }
[(
 )
]<
((
{
})
<(
{
}(
({
 }
)
<{
(
{
}[
(
)]
<(
{
}
<
>)
<
>
> 
)}
{
}<
 >
>)
<
>
<(
{
}
<(
<(
 )
>)
<
>
 {
({
 }
[(
 )
]<
({
 }
<
>
)
<
>>
)}
>
{
})
<
>
>)
>
)>
)}
{
}
>
)
>)
 }
([
 ]
)
{{
 }
{
}(
[
])
}{
 }
<
>(
[
])
{{
 }
 (
 {
 }
 <
 >
 )
 <
 >
 (
 [
 ]
 )
 }
 <
 >

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Japt, 2 bytes

ÕU

Try it | Transposed

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 62 bytes

//m0()m  ]
//=](=a  )
//>.a>p( )
//mm,m/aa
//[ap./=[
   p  =>p

Try it online!

Improved with a different layout

///// 
///// 
m=>m[ 
0].map 
((a,p 
)=>m. 
map//= 
  (a=> 
   a[p 
])) 
\$\endgroup\$
2
\$\begingroup\$

Haskell, 153 144 bytes

(thanks, Sriotchilism O'Zaic)

f
 [
 ]=
   [
   ];
    f(
     x:
      l)
       =(
        :)
         x l
-- :   z $
-- f   i f
-- [   p
-- ]   W
--     i
--     t
--     h

Try it online!

Try it transposed!

\$\endgroup\$
  • \$\begingroup\$ Here is a 144 byte version while maintaining your structure. (Transposed). \$\endgroup\$ – Sriotchilism O'Zaic Sep 25 at 3:44
  • \$\begingroup\$ Here is a 70 byte version that is still similar but with some structural changes. (Transposed). \$\endgroup\$ – Sriotchilism O'Zaic Sep 25 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 Sep 25 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 Sep 25 at 7:50
1
\$\begingroup\$

05AB1E, 3 bytes

øø
q

Try it online.

øq
ø

Try it transposed.

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
\$\endgroup\$
1
\$\begingroup\$

Ruby, 35 bytes

#-sz##
->hip{
 hip  }
#{f
#ht*
#..h

Try it online!

Hip to be square! (Almost)

Accepts ruby matrices as input (arrays of arrays)

\$\endgroup\$
1
\$\begingroup\$

Clean, 64 bytes

//i$
//mp
$ p=p
//ot
//rr
//ta
// n
//Ss
//tp
//do
//Ls
//ie
//b

Try it online!

//$//////////
// //////////
import StdLib
$p=transpose
  p

Try it transposed!

\$\endgroup\$
0
\$\begingroup\$

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
)
]
;
"
"
"""

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Wolfram Language (Mathematica), 13 bytes

#&(*
 *)
\

Try it online! / Transposed

\$\endgroup\$
0
\$\begingroup\$

Cjam, 13 bytes

qo
~
z
`

Try it online!

Transposed version:

q~z`
o

Try it online!

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
` };

Try it online!

or transposed:

q~z`
o
{  };

Try it online!

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.

\$\endgroup\$
0
\$\begingroup\$

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
)
@
}

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.