I transpose the source code, you transpose the input!

33

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

Adám

Posted 2019-09-22T12:08:53.417

Reputation: 37 779

Can we count our bytes by the transposed version? In my case the transposed version has less newlines, so it is smaller. – Night2 – 2019-09-22T16:05:47.243

23My god. Can we stop? – S.S. Anne – 2019-09-22T16:30:33.833

3@Night2 No, it complicates matters. – Adám – 2019-09-22T16:30:47.837

14@JL2210 No, I have a big one in the works. – Adám – 2019-09-22T16:31:08.283

7These challenges are getting impossible in procedural languages without serious comment abuse. – S.S. Anne – 2019-09-22T16:32:18.233

1What, negate the source code, negate the output? – S.S. Anne – 2019-09-22T16:40:44.850

2

@JL2210 Transforms.

– Adám – 2019-09-22T16:42:04.797

The optimization target should be clearly specified in this case (c.f. this related problem) since the original code and transposed code can have different lengths due to different numbers of newlines.

– Joel – 2019-09-22T18:22:45.313

@Joel How is it not clear? Your source code size is measure like in any other code-golf challenge. – Adám – 2019-09-22T18:24:04.343

OK. Then you could add sth. like "the target is to optimize the byte count of the original source code. The byte count of the transposed version does not matter". – Joel – 2019-09-22T18:35:48.970

@Joel You wish… – Adám – 2019-09-22T18:37:29.870

Was able to do a couple of the other rip-offs in Turing Machine Language, but I think this challenge may have bested that language's capabilities (or perhaps more accurately my capabilities with it). – ouflak – 2019-09-23T10:42:03.067

@ouflak Isn't it like — per definition — possible to solve this challenge in Turing Machine Language‽ – Adám – 2019-09-23T10:43:21.357

@Adám, Hence my qualification in parethesis. I could put together a limited case I suppose.... – ouflak – 2019-09-23T10:45:00.803

@Adám Similarly, this challenge would be very difficult in Runic, due to its language features/restrictions. Just managing the stack while reading the input would be troublesome. Possible? Yes. Fun, not really. – Draco18s no longer trusts SE – 2019-09-23T13:48:43.210

@nimi You can assume a non-empty array. – Adám – 2019-09-23T20:55:25.867

@SriotchilismO'Zaic Well, some languages cannot distinguish between various 0×n arrays… Anyway, clarified OP to minimum 1×1 – Adám – 2019-09-23T20:56:26.070

Answers

27

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.

Joel

Posted 2019-09-22T12:08:53.417

Reputation: 1 691

3That's really neat! – Adám – 2019-09-22T19:43:20.057

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. – Erik the Outgolfer – 2019-09-22T20:36:40.337

@EriktheOutgolfer Fixed. Thanks. – Joel – 2019-09-22T20:54:25.650

15

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.

ovs

Posted 2019-09-22T12:08:53.417

Reputation: 21 408

13

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!

Nick Kennedy

Posted 2019-09-22T12:08:53.417

Reputation: 11 829

You don't need the final #. – Robin Ryder – 2019-09-22T18:58:08.233

@RobinRyder thanks! For some reason I was padding to a rectangle even though I’d seen the statement that it was unnecessary. – Nick Kennedy – 2019-09-22T18:59:57.620

11

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!

gastropner

Posted 2019-09-22T12:08:53.417

Reputation: 3 264

8

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

Post Rock Garf Hunter

Posted 2019-09-22T12:08:53.417

Reputation: 55 382

60 bytes – Ørjan Johansen – 2019-09-23T04:23:03.657

@Ø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. – Post Rock Garf Hunter – 2019-09-23T04:30:13.237

7

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!

Erik the Outgolfer

Posted 2019-09-22T12:08:53.417

Reputation: 38 134

5

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.

nwellnhof

Posted 2019-09-22T12:08:53.417

Reputation: 10 037

5

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.

nimi

Posted 2019-09-22T12:08:53.417

Reputation: 34 639

2161 bytes – Ørjan Johansen – 2019-09-24T04:53:28.017

4

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!

Night2

Posted 2019-09-22T12:08:53.417

Reputation: 5 484

4

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

Shaggy

Posted 2019-09-22T12:08:53.417

Reputation: 24 623

4

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!

Post Rock Garf Hunter

Posted 2019-09-22T12:08:53.417

Reputation: 55 382

3

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 .

Neil

Posted 2019-09-22T12:08:53.417

Reputation: 95 035

3

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

AZTECCO

Posted 2019-09-22T12:08:53.417

Reputation: 2 441

2

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)

G B

Posted 2019-09-22T12:08:53.417

Reputation: 11 099

2

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!

ceased to turn counterclockwis

Posted 2019-09-22T12:08:53.417

Reputation: 5 200

Here is a 144 byte version while maintaining your structure. (Transposed). – Post Rock Garf Hunter – 2019-09-25T03:44:58.823

Here is a 70 byte version that is still similar but with some structural changes. (Transposed). – Post Rock Garf Hunter – 2019-09-25T04:08:30.357

Thanks. The second version lacks the central idea though (re-using the foldr recursion for both versions), don't like that. – ceased to turn counterclockwis – 2019-09-25T07:01:46.437

You know, this challenge and your username kinda contradict each other, since your code did turn counterclockwise in that transposed version. ;p – Kevin Cruijssen – 2019-09-25T07:50:10.553

2

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

Bubbler

Posted 2019-09-22T12:08:53.417

Reputation: 16 616

1

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

Kevin Cruijssen

Posted 2019-09-22T12:08:53.417

Reputation: 67 575

1

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!

Οurous

Posted 2019-09-22T12:08:53.417

Reputation: 7 916

0

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!

Nishioka

Posted 2019-09-22T12:08:53.417

Reputation: 181

0

attinat

Posted 2019-09-22T12:08:53.417

Reputation: 3 495

0

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.

Roman Gräf

Posted 2019-09-22T12:08:53.417

Reputation: 2 915

0

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

GammaFunction

Posted 2019-09-22T12:08:53.417

Reputation: 2 838

0

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.

Draco18s no longer trusts SE

Posted 2019-09-22T12:08:53.417

Reputation: 3 053