38
\$\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\$
17
  • 25
    \$\begingroup\$ My god. Can we stop? \$\endgroup\$
    – S.S. Anne
    Commented Sep 22, 2019 at 16:30
  • 3
    \$\begingroup\$ @Night2 No, it complicates matters. \$\endgroup\$
    – Adám
    Commented Sep 22, 2019 at 16:30
  • 15
    \$\begingroup\$ @JL2210 No, I have a big one in the works. \$\endgroup\$
    – Adám
    Commented Sep 22, 2019 at 16:31
  • 7
    \$\begingroup\$ These challenges are getting impossible in procedural languages without serious comment abuse. \$\endgroup\$
    – S.S. Anne
    Commented Sep 22, 2019 at 16:32
  • 2
    \$\begingroup\$ @JL2210 Transforms. \$\endgroup\$
    – Adám
    Commented Sep 22, 2019 at 16:42

25 Answers 25

28
\$\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
  • 3
    \$\begingroup\$ That's really neat! \$\endgroup\$
    – Adám
    Commented 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 a SyntaxError. You need to move the quotes in both directions to fix this. \$\endgroup\$ Commented Sep 22, 2019 at 20:36
  • \$\begingroup\$ @EriktheOutgolfer Fixed. Thanks. \$\endgroup\$
    – Joel
    Commented Sep 22, 2019 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\$
0
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\$
2
  • \$\begingroup\$ You don't need the final #. \$\endgroup\$ Commented 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\$ Commented Sep 22, 2019 at 18:59
11
\$\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\$
2
  • \$\begingroup\$ 60 bytes \$\endgroup\$ Commented 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\$
    – Wheat Wizard
    Commented Sep 23, 2019 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\$

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

Japt, 2 bytes

ÕU

Try it | Transposed

ÕU     :Implicit input of string/array U
Õ      :Transpose U
 U     :Original U
       :Implicit output
Õ\nU     :Implicit input of string/array U
Õ        :Transpose U
 \n      :Reassign to U
   U     :Newly transposed U
         :Implicit output
\$\endgroup\$
0
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\$

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\$
4
  • \$\begingroup\$ Here is a 144 byte version while maintaining your structure. (Transposed). \$\endgroup\$
    – Wheat Wizard
    Commented Sep 25, 2019 at 3:44
  • \$\begingroup\$ Here is a 70 byte version that is still similar but with some structural changes. (Transposed). \$\endgroup\$
    – Wheat Wizard
    Commented 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\$ Commented 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\$ Commented Sep 25, 2019 at 7:50
2
\$\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\$
2
\$\begingroup\$

APL (Dyalog Unicode), 7 bytes

{⍵
⍉
⍵}

Try it online!

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

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

Wolfram Language (Mathematica), 13 bytes

#&(*
 *)
\

Try it online! / Transposed

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

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.

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

05AB1E, 4 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\$
2
  • \$\begingroup\$ That's 4 bytes, not 3... \$\endgroup\$
    – naffetS
    Commented 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\$ Commented Jun 26, 2022 at 11:36
1
\$\begingroup\$

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

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

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\$
0
\$\begingroup\$

Pyth, 10 bytes

CCQ "
Q

"

Try it online!

Transposed

CQ "
C
Q

"

Try it online!

Explanation

The program transposes the input twice, returning the original input. The transposed version only transposes once.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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