26
\$\begingroup\$

Write two programs, each taking a string s and a number n ≥ 0, such that:

  • The first program prints s n times, separated by newlines.
  • The second program prints each character of s n times, with repeated characters separated by newlines.
  • Every character at column x, line y in one program equals the character at column y, line x in the other program.

Both programs must contain at least two lines. You may take input in any reasonable order or format that works for both programs.

This is ; the less bytes, the better. Count bytes from your longer program (that is, the one with more lines than columns).


Here's an example in a hypothetical programming language, with s = "Hello, world!" and n = 3:

Program A

a(b*c
defg 
h.ij)

Output

Hello, world!
Hello, world!
Hello, world!

Program B

adh
(e.
bfi
*gj
c )

Output

HHH
eee
lll
lll
ooo
,,,

www
ooo
rrr
lll
ddd
!!!
\$\endgroup\$
8
  • 1
    \$\begingroup\$ Related, related, and related. \$\endgroup\$ May 30, 2020 at 17:15
  • \$\begingroup\$ Can one program take s first then n, and the other take n then s? \$\endgroup\$ May 31, 2020 at 0:57
  • \$\begingroup\$ @Khuldraesethna'Barya The input should be the same format for both programs. \$\endgroup\$
    – darrylyeo
    May 31, 2020 at 2:37
  • \$\begingroup\$ Can I write a function instead of a program? \$\endgroup\$ May 31, 2020 at 7:13
  • \$\begingroup\$ Do both programs need to be in the same language? \$\endgroup\$ May 31, 2020 at 18:47

10 Answers 10

16
\$\begingroup\$

C, 196 bytes

Regular version: 190 bytes

p;f(char *s,n) { 
; while( p++<  n)
puts(s);p; }   /*
f*******u*0**;* /
(///////t/)//}+*
c*)dfppnc(;p(w+/
hs{oo=+;h* u"hs}
a, {r0+)a /t"i)/
rn  (;< r *s l;*
/////// /  / e/ 
******* *  * (*/

Mirrored version: 196 bytes

p;pf(char/*
; u*/*s,n/*
fwt*/){  /*
(hs*/do{ /*
ci(*/for(/*
hls*/p=0;/*
ae)*/p++</*
r(;*/n;)   
  putchar/*
*p;+*/(*   
s+ 0); /*  
,+}*/puts/*
n< */(""   
)  ;}while(
   *++s);/*
{n/ */}/* /
 )*/

Readable regular version:

p; f(char* s, n) {
  ;
  while (p++ < n)
    puts(s);
  p;
}

Readable mirrored version:

p; pf(char* s, n) {
  do {
    for (p = 0; p++ < n; )
      putchar (*s + 0);
    puts("");
  } while (*++s);
}
\$\endgroup\$
2
  • \$\begingroup\$ Make sure you mirror your spaces, too! \$\endgroup\$
    – darrylyeo
    Jun 1, 2020 at 3:33
  • \$\begingroup\$ Oh, you got it, sorry! That's what happens when you do the reversing manually... \$\endgroup\$
    – yoann
    Jun 1, 2020 at 6:39
13
\$\begingroup\$

MATL, 11 bytes

Inputs are s, then n.

Normal version (10 bytes):

1!X"
XDD
"

Try it online!

Mirrored version (11 bytes):

1X"
!D
XD
"

Try it online!

Explanation

The normal version is parsed as

1    % Push 1
!    % Transpose: does nothing to the 1
X"   % Implicit inputs: n, s. Repeat s n times vertcally and 1 time horizontally 
XD   % Display the full stack contents. This prints the output
D    % Display. Triggers implicit input, which is not present, and so errors
"    % For each. This statement is not reached

The mirrored version is parsed as

1    % Push 1
X"   % Implicit inputs: n, s. Repeat s n times vertcally and 1 time horizontally 
!    % Transpose. This transforms the above into the desired output
D    % Display. This prints the output
XD   % Display all stack contents. The stack is empty, so this does nothing
"    % For each. Triggers implicit input, which is not present, and so errors
\$\endgroup\$
1
  • \$\begingroup\$ @Downvoter Any suggestion to improve my annswer? \$\endgroup\$
    – Luis Mendo
    Jul 21, 2020 at 9:46
9
\$\begingroup\$

perl, 102 bytes

$_=<>;$n=<>;print$_ x$n
__END__
=E
<N
>D
;_
$_
;
=
<
>
;
s
/
.
/
$
&
x
$
;
.
"
\
n
"
/
g
e
;
p
r
i
n
t

Try it online!

$_=<>;$;=<>;s/./$&x$;."\n"/ge;print
__END__
=E
<N
>D
;_
$_
n
=
<
>
;
p
r
i
n
t
$
_
 
x
$
n

Try it online!

The programs are fairly trivial, they just do what is required without any trickery. The only trickery is the __END__; this tells perl to ignore anything what is following. That way, hardly anything is shared between the two programs, only the first two bytes are: $_, where the _ is either the name of the variable, or the first character of the __END__ token.

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

Charcoal, 20 bytes

ENη¿⁰«
N
η
‖
↙
¿
⁰
«

Try it online! Explanation:

ENη

Convert the input number to an implicit range, map each entry to the input string, and print the result.

¿⁰

Execute the rest of the program only if 0 is true (which it isn't).

«

Wrap the rest of the program in a block, so it doesn't matter what its meaning is, since it never gets executed.

Reflected:

ENη‖↙¿⁰«
N
η
¿
⁰
«

Try it online! Explanation: Much like the other program, except that the ‖↙ reflects the output as desired.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ @Jonah You may be confused as I was. The challenge doesn't ask for a single program to be symmetric. So the double bar may appear at only one place in each program \$\endgroup\$
    – Luis Mendo
    May 31, 2020 at 0:01
8
\$\begingroup\$

05AB1E (legacy), 6 bytes

-1 byte thanks to @dingledooper.

иø
»
q

Try it online!

Explanation (Horizontal)

и   Sequence product.
 ø  Transpose this product.
»   Join the product by newlines.
q   Exit the program.

Explanation (transposed)

и   Sequence product.
 »  Join the product by newlines.
  q Exit the program.

A garbage dump the interpreter ignores:
ø
```
\$\endgroup\$
0
6
\$\begingroup\$

Python 2, 95 bytes

First program: 94 bytes

sfp,n=input();"\
,or";s=sfp+"\n"
print s*n;  """
= n
ict
n  
pic
un*
t p
(s;
):"
#""
"""
1;
#\

Try it online!

Second program: 95 bytes

s,p=input()#"1#
for c in s:"";\
print c*p;"""
,"n
n;t
=s 
i=s
ns*
pfn
up;
t+ 
(" 
)\"
;n"
"""
\

Try it online!

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

Perl 5 + -pF, 51 bytes

I feel I can condense this more so I'll likely tinker with it to try and make it more square...

$_=$_ x<>;'
; f
=xo
<$r
>;@
;.F
$$;
\/}
.|{
=| 
$$'

Try it online!

Explanation

Basically this just runs $_=$_ x <> which, since -p flag is used, will just print out the string triplicated. The rest of the string is avoid by being inside a single quote.

$;=<>;$\.=$
_ x$;.$/||$
=for@F;}{'
$
_
 
x
<
>
;
'

Try it online!

Explanation

This takes n from STDIN (<>) and stores in $;, next the magic variable $\ (which is automatically output as the final argument to any call to print) is appended to with $; copies of $_. $_ is set to each letter of the input during the for@F. We need to also close the implicit while (<STDIN>) loop (that is added via -p) with }{ so that the global $_ is empty and when print is called, only $\ is output.

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

Mathematica, 114 bytes

Mathematica, with it's long, unsplittable names means that no matter how short I make this, it'll still have obtuse names, the alternative of using <> and #&/@ would be shorter for either individual program, but dealing with them in the transposition makes the program longer overall.

StringRiffle[
t;Table[##],1*
rT;"\n"]&(*  )
ia
nb
gl
Re
i[
f#
f#
l]
e]
[
C,
h"
a\
rn
a"
c,
t"
e"
r]
s&
[(
1*
*)

Normal

StringRiffle[Characters[1*
t;Table[##]],"\n",""]&(*)
rT;
ia"
nb\
gln
Re"
i[]
f#&
f#(
l]*
e,
[1
 *)

Transposed

Base programs:

Normal:

StringRiffle[Table[##],"\n"]&

Transposed:

StringRiffle[Characters[Table[##]],"\n",""]&

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

Jelly, 10 bytes

Unfortunately, for this challenge, the execution of a Jelly program starts with the bottom line of code, so there's quite a bit of work required to get a solution under about 25 bytes!

W ñ
ẋY
Z
ñ

Try it online!

WẋZñ
 Y
ñ

Transposed

How?

Normal:

W ñ - Link 1: s, n
W   - wrap (s) in a list
  ñ - call the next Link (2) as a dyad - f(that, n)

ẋY - Link 2: wrapped s, n
ẋ  - repeat (s) (n) times
 Y - join with newlines

Z - Link 3 (unused)

ñ - Main Link: s, n
ñ - call the next Link (1) as a dyad - f(s, n)
  - implicit (smashing) print

Transposed:

WẋZñ - Link 1: s, n
W    - wrap (s) in a list
 ẋ   - repeat (that) (n) times
  Z  - transpose
   ñ - call the next Link (2) as a dyad - f(that, n)

 Y - Link 2: transposed, repeated [s]
 Y - join with newlines

ñ - Main Link: s, n
ñ - call the next Link (1) as a dyad - f(s, n)
  - implicit (smashing) print
\$\endgroup\$
2
\$\begingroup\$

Stax, 6 bytes

]
*M
m

Run and debug it

Mirrored:

]*m
 M

Run and debug it

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