Write two programs, each taking a string s and a number n ≥ 0, such that:
- The first program prints
sntimes, separated by newlines. - The second program prints each character of
sntimes, with repeated characters separated by newlines. - Every character at column
x, lineyin one program equals the character at columny, linexin 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 code-golf; 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
!!!
10 Answers 10
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);
}
-
\$\begingroup\$ Make sure you mirror your spaces, too! \$\endgroup\$darrylyeo– darrylyeo2020年06月01日 03:33:17 +00:00Commented Jun 1, 2020 at 3:33
-
\$\begingroup\$ Oh, you got it, sorry! That's what happens when you do the reversing manually... \$\endgroup\$yoann– yoann2020年06月01日 06:39:02 +00:00Commented Jun 1, 2020 at 6:39
MATL, 11 bytes
Inputs are s, then n.
Normal version (10 bytes):
1!X"
XDD
"
Mirrored version (11 bytes):
1X"
!D
XD
"
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
-
\$\begingroup\$ @Downvoter Any suggestion to improve my annswer? \$\endgroup\$Luis Mendo– Luis Mendo2020年07月21日 09:46:12 +00:00Commented Jul 21, 2020 at 9:46
perl, 102 bytes
$_=<>;$n=<>;print$_ x$n
__END__
=E
<N
>D
;_
$_
;
=
<
>
;
s
/
.
/
$
&
x
$
;
.
"
\
n
"
/
g
e
;
p
r
i
n
t
$_=<>;$;=<>;s/./$&x$;."\n"/ge;print
__END__
=E
<N
>D
;_
$_
n
=
<
>
;
p
r
i
n
t
$
_
x
$
n
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.
Charcoal, 20 bytes
ENη¿0«
N
η
‖
↙
¿
0
«
Try it online! Explanation:
ENη
Convert the input number to an implicit range, map each entry to the input string, and print the result.
¿0
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η‖↙¿0«
N
η
¿
0
«
Try it online! Explanation: Much like the other program, except that the ‖↙ reflects the output as desired.
-
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– Luis Mendo2020年05月31日 00:01:07 +00:00Commented May 31, 2020 at 0:01
05AB1E (legacy), 6 bytes
-1 byte thanks to @dingledooper.
иø
»
q
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:
ø
```
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;
#\
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"
"""
\
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
$$;
\/}
.|{
=|
$$'
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
<
>
;
'
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.
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",""]&
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
ñ
WẋZñ
Y
ñ
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
sfirst thenn, and the other takenthens? \$\endgroup\$