| Bytes | Lang | Time | Link |
|---|---|---|---|
| 016 | Pip | 211217T170435Z | DLosc |
| 044 | Perl 6 | 190422T090811Z | Jo King |
| 049 | Retina | 190422T075105Z | lolad |
| 104 | C gcc | 170623T064532Z | Bodo Thi |
| 041 | Cubix | 170622T231810Z | MickyT |
| 037 | MATL | 170606T160845Z | J Doe |
| 030 | str | 170605T002010Z | Conor O& |
| 014 | 05AB1E | 170604T001244Z | sporkl |
| 087 | Matlab | 170604T174907Z | J Doe |
| 137 | Java OpenJDK 8 | 170603T165908Z | marcelov |
| 054 | Haskell | 170603T155800Z | BlackCap |
| 036 | QBIC | 170604T082414Z | steenber |
| 071 | Python 3 | 170604T045744Z | Esolangi |
| 083 | JavaScript ES6 | 170603T161356Z | Stephen |
| 067 | PHP>=7.1 | 170603T163527Z | Jör |
| 079 | JavaScript ES6 | 170603T163443Z | Rick Hit |
Pip, 16 bytes
TaOba<0?RbbX:ABa
Explanation
TaOba<0?RbbX:ABa
a and b are the two command-line arguments
Ta Loop until a is truthy:
Ob Output b without a newline
If a=0, this loops infinitely; otherwise, it doesn't loop at all
a<0? If a is negative:
Rb b reversed
Else (if a is positive):
b b
X: Repeat that string this many times:
ABa Absolute value of a
Perl 6, 44 bytes
{[$^s.flip,$s,$s Zxx-$^n,Inf,$n][$n.sign+1]}
Anonymous code block that takes a number and a string and returns a (possibly infinite) list
Retina, 49 bytes
/¶-/&V`^.+
/¶0/&//+>G0`
~`(.+)¶-*(\d+)
.-$2+>K`$1
Input format: takes in the string, followed by a newline, followed by the number.
Explanation:
/¶-/&V`^.+
The /¶-/& runs this line only if the number is negative. V is the reverse stage, and it reverses ^.+, which matches the string (. matches every character apart from newlines).
/¶0/&//+>G0`
The /¶0/& runs this line only if the number is 0. //+> starts an infinite loop, which prints the working string after each iteration. G0 takes the string and discards the number; it does this infinitely, printing every time.
~`...
This marks code that will generate a string; the program evaluates the string as Retina code after.
(.+)¶-*(\d+)
.-$2+>K`$1
(.+)¶-*(\d+) matches the whole string and puts the string in capturing group 1 and the number in capturing group 2. .-$2+>K` $1 generates the Retina code to be run: . turns implicit output off (otherwise the string would be printed n+1 times), -$2+ sets a repeat loop that repeats for {capturing group 2} times. The minus at the beginning turns the number into a negative number, as this disables the convergence functionality in the loop, which would stop it after the 1st iteration. > sets this loop to print after each iteration. The rest of the code is just to print the string.
C (gcc), 115 112 109 107 104 bytes
f(n,s,l,p,d)char*s;{d=n<0?-1:1;do for(l=1,p=0;p>=0;p+=l)s[p]?d==l&&putchar(s[p]):l--;while(!n||(n-=d));}
Who said, we need
strlen?
C (gcc), 115 bytes (134 with #include<string.h> in front)
#include<string.h>
f(n,s)char*s;{int l=strlen(s),d=n<0?0:2,m=d--,p;do for(p=m?0:l-1;p!=(m?l:-1);p+=d)putchar(s[p]);while(!n||(n-=d));}
Without
#include<string.h>we get an implicit prototype forstrlenthat returnsint, butstrlenissize_t(at least nowadays, not perfectly sure about k&r or c89, but I believe, it returnedintin the old days).The missing
#include <stdio.h>isn't a problem, because due to integer promotion, the default prototype will beint putchar(int)which is exactly what we want.
Cubix, 41 Forty four 45 bytes
Takes input as <N> <String>
.uq.sB.p$IA;p?;ouu(..!q/o()uq?..@<w?q<<_)
Cubified:
. u q
. s B
. p $
I A ; p ? ; o u u ( . .
! q / o ( ) u q ? . . @
< w ? q < < _ ) . . . .
. . .
. . .
. . .
There is still an amount of no-ops in the code which I might be able to get a few more bytes out of, but wanted to get this up before I break it.
Basic procedure is
Iget counter from inputAtake the rest of input in as characters;p?remove the space, bring the number up and test itpsuqB$)if the counter is negative, reverse the stack. This involves handling the input number and EOI marker(-1). Increment the counter.;p;ouquif the counter is zero, remove the counter and EOI marker and start perpetual output loop.(if positive decrement the counter
<<q?/o()uthe output loop. This will output each character of the stack until the EOI marker (-1) is reached.... _ ... ?wq!on end EOI marker, go around the cube and reflect back to the?, change lane, drop the EOI marker to the bottom and test the counter.@if zero, halt?u(if positive u-turn and decrement, thie ends up hitting the beginning of the loop? ... <)if negative, go around the cube to the otherside, redirect to the beginning of the loop while passing over a increment./)<if negative increment and carry on to output loop
MATL, 37 bytes
jXJiXI0=?`1wtwDw]}I0>?I:"t]x}PI|:"t]x
Explanation:
j % input string
XJ % copy to clipboard J
i % input
XI % copy to clipboard I
0 % number literal
= % is equal? (element-wise, singleton expansion)
? % if
` % do...while
1 % number literal
w % swap elements in stack
t % duplicate elements
w % swap elements in stack
D % convert to string and display / string representation
w % swap elements in stack
] % end
} % else
I % paste from clipboard I
0 % number literal
> % is greater than? (element-wise, singleton expansion)
? % if
I % paste from clipboard I
: % range; vector of equally spaced values
" % for
t % duplicate elements
] % end
x % delete
} % else
P % flip the order of elements
I % paste from clipboard I
| % absolute value / norm / determinant
: % range; vector of equally spaced values
" % for
t % duplicate elements
] % end
x % delete
% (implicit) end
% (implicit) end
% (implicit) convert to string and display
str, 30 bytes
I#Lbd0<[_u_][d0='e'u#?]#?xo;db
Explanation
I#Lbd0<[_u_][d0='e'u#?]#?xo;db
...........................; preamble
I read number
#L read rest of STDIN
b buffer the STDIN
d duplicate number
0<[ ] #? if the number is less than zero
_ negate that number
u_ and reverse STDIN from buffer
[ ] otherwise
d0='e #? if its 0, push the empty string
'u otherwise, push the unbuffered STDIN untouched
x repeat STDIN by the TOS
o and output
;.. main program (only activates when input = 0)
d duplicate the implicitly unbuffered STDIN
b and rebuffer it
implicitly displayed
05AB1E, 17 16 14 bytes
0‹iR}¹Ä×¹_i[²?
Explanation:
0‹iR}¹Ä×¹_i[²?
0‹ Is the input negative?
iR} If so, reverse the second input.
¹Ä Get the absolute value of the first input.
× Repeat the string that many times.
¹_ Boolean NOT the first input. (Is the first input 0?)
i If so...
[ Do forever...
²? Print the second input without a newline.
Saved 2 bytes thanks to @EriktheOutgolfer
Matlab, 87 bytes
n=input('')
s=input('','s')
a=repmat(s,1,abs(n))
while~n s=[s s]
end
if n<0,flip(a)
end
My first attempt at code-golf! Any suggestions for golfing are welcome.
Java (OpenJDK 8), 137 bytes
void f(String[] a){for(long n=Long.valueOf(a[0]),i=0;n==0|i++<Math.abs(n);)System.out.print(n<0?new StringBuilder(a[1]).reverse():a[1]);}
Haskell, 57 54 bytes
f 0=cycle
f n|n<0=f(-n).reverse|n>0=concat.replicate n
Explanation:
f 0 -- If n=0 ..
=cycle -- infinitely repeat the input
f n|n<0 -- Otherwise, if n<0 ..
=f(-n) -- call f with the negative of n ..
.reverse -- and the reverse of the input
|n>0 -- Finally, if n>0 ..
concat -- concatenate the result of ..
.replicate n -- repeating the input n times
-3 bytes thanks to @nimi
QBIC, 36 bytes
Lot ging on here, and QBIC/QBasic just doesn't have the syntax to deal with such conditions elegantly.
~:<0|;=_fA}[abs(a)|Z=Z+A]~a|_X}{?A';
Explanation:
~:<0| IF cmd line arg 'a' is negative
;=_fA Make cmd line arg A$ into its reverse
} Close the IF (this eliminates the need for a | fuction terminator on _f)
[abs(a)| FOR b = 1 to (abs(a) (hammering out negatives)
Z=Z+A Add A$ to Z$ (on exit, Z$ is printed explicitly)
] NEXT
~a|_X IF a is non-zero, terminate the program
} END IF
{?A'; If we're here, just start a DO-loop and keep on printing the input.
Python 3, 71 bytes
def f(n,s,k=1):
if n<0:s=s[::-1];n=-n
while n|k:print(end=s);n-=1;k=0
The variable k guarantees the loop is always run at least once. This means that if n=0, then n will be negative on the next iteration of the loop, so the loop will continue to be run forever.
JavaScript (ES6), 98 94 91 83 bytes
n=>s=>{s=n<0?[...s].reverse().join``:s;while(!n)l(s);l(s.repeat(n<0?-n:n))}
-4, -5 bytes thanks to Arjun
-3 bytes thanks to Rick Hitchcock
Started out different than the Java answer, but quickly became very similar after golfing. Alert is infinite, but if you want it to look nice, switch to console.log. l=alert; and writing out alert are the same length, but if you switch to console.log it's shorter to redefine it.
PHP>=7.1, 67 Bytes
for([,$x,$y]=$argv,$z=$x<=>0;!$z||$x;$x-=$z)echo$x<0?strrev($y):$y;
Version with list(,$x,$y) instead of [,$x,$y]
Try it online!
JavaScript (ES6), 79 bytes
f=(n,s)=>n<0?f(-n,[...s].reverse().join``):(alert(!n?s:s.repeat(n)),!n&&f(n,s))
Snippet:
f=(n,s)=>n<0?f(-n,[...s].reverse().join``):(alert(!n?s:s.repeat(n)),!n&&f(n,s))
f(5, "hello world")
//f(0, "PPCG") //uncomment this at your peril!!!
f(-2, "truThY")
f(2000, "o")