46
\$\begingroup\$

In this challenge fake marquee text is text that is shown part by part, in a scrolling-like fashion.

Some examples:

testing 4

   t
  te
 tes
test
esti
stin
ting
ing
ng
g


hello 2

 h
he
el
ll
lo
o


foobarz 3

  f
 fo
foo
oob
oba
bar
arz
rz
z


Something a bit longer 10

         S
        So
       Som
      Some
     Somet
    Someth
   Somethi
  Somethin
 Something
Something 
omething a
mething a 
ething a b
thing a bi
hing a bit
ing a bit 
ng a bit l
g a bit lo
 a bit lon
a bit long
 bit longe
bit longer
it longer
t longer
 longer
longer
onger
nger
ger
er
r


small 15

              s
             sm
            sma
           smal
          small
         small
        small
       small
      small
     small
    small
   small
  small
 small
small
mall
all
ll
l


aaa 3

  a
 aa
aaa
aa
a


brace yourself 6

     b
    br
   bra
  brac
 brace
brace 
race y
ace yo
ce you
e your
 yours
yourse
oursel
urself
rself
self
elf
lf
f

You have to write a program or a function that takes in two input and prints the output as described above. You may or may not output trailing spaces in your output. This is code-golf so shortest code in bytes wins.

If your program is standalone (i.e. when run actually prints the lines) (Input can be hard-coded but easy to change) and sleeps a bit between each line of output you get a -10 bonus.

\$\endgroup\$
  • \$\begingroup\$ What do you mean by "includes Output" ? \$\endgroup\$ – Optimizer May 13 '15 at 13:13
  • 1
    \$\begingroup\$ Also, do we have to print the first and the last lines which are simply spaces ? \$\endgroup\$ – Optimizer May 13 '15 at 13:17
  • 2
    \$\begingroup\$ Inspired by this? \$\endgroup\$ – Spikatrix May 13 '15 at 14:18
  • 1
    \$\begingroup\$ A single empty line at the beginning seems to be allowed. How about another empty line at the end? \$\endgroup\$ – nimi May 13 '15 at 17:57
  • 2
    \$\begingroup\$ Since you're talking about "only visible output", can we assume that the input will only be printable ASCII? (character codes 0x20 to 0x7E) \$\endgroup\$ – Martin Ender May 13 '15 at 23:24

37 Answers 37

24
\$\begingroup\$

CJam, 12 11 bytes

1 byte saved by Dennis.

,Sf*\f+$zN*

I'm making use of "Input can be hard-coded but easy to change": this expects the input to be already on the stack, so you can prepend "testing" 4 to the above, for instance.

Test it here.

Explanation

Notice that the transpose of the desired output is much simpler:

   testing
  testing
 testing
testing

So we just need to create n lines, prepending i spaces for i from n-1 down to 0. That's what the code does:

,            e# Turn n into a range [0 1 .. n-1]
 Sf*         e# Turn each i into a string of i spaces.
    \f+      e# Swap this array with the input string and append it to each of the
             e# strings of spaces.
       $     e# Sort the array to have the string with n-1 spaces first.
        z    e# Transpose the grid.
         N*  e# Join the lines with newline characters.

19 - 10 = 9?

I find the "sleeps a bit between each line" bonus a bit vague and dodgy, but here is a 19 byte version that simply stalls after each line by computing all permutations of the array [0 1 .. 7]. In the online interpreter this simply leads to the final result being shown a bit later, but if you use the Java interpreter this will actually print each line after "sleeping a bit":

,Sf*\f+$z{oNo8e!;}/
\$\endgroup\$
  • \$\begingroup\$ Nice use of z. Assuming the input is printable ASCII, you can replace W% with $. \$\endgroup\$ – Dennis May 13 '15 at 23:23
  • \$\begingroup\$ @Dennis Oh, I like that. I've requested clarification on this from the OP. (That said, I use z all the time for ascii grid-based challenges.) \$\endgroup\$ – Martin Ender May 13 '15 at 23:25
15
\$\begingroup\$

C, 69 bytes

printf magic!

f(s,n,i)char*s;{for(i=n;*s;i?i--:s++)printf("%*s%.*s\n",i,"",n-i,s);}

Expanded version with some explanation:

f(s,n,i)char*s;{       /* s is the string, n is the width of the grid. */
  for(i=n;             /* i is the number of preceding spaces. */
      *s;              /* Stop once we reach the end of the string. */
      i?i--:s++)       /* Decrease the number of spaces, and when there's 
                          none left start truncating the string itself. */
  printf("%*s          /* The first argument is the minimum width to print the 
                          string (left padded with spaces) and the second 
                          argument is the string to print. We use the empty 
                          string just to print the i spaces. */
    %.*s              /* The third argument is the maximum number of 
                         characters from the string (which is the fourth 
                         argument) to print. */
    \n",i,"",n-i,s);
}

And here's an example:

$ ./marquee stackoverflow 12

           s
          st
         sta
        stac
       stack
      stacko
     stackov
    stackove
   stackover
  stackoverf
 stackoverfl
stackoverflo
tackoverflow
ackoverflow
ckoverflow
koverflow
overflow
verflow
erflow
rflow
flow
low
ow
w
\$\endgroup\$
14
\$\begingroup\$

Pyth, 13 bytes

jb.:++K*dQzKQ

Try it online: Pyth Compiler/Executor

Explanation

                 implicit: z = input string, Q = input number
      K*dQ       K = " " * Q
    ++K   zK     K + z + K
  .:        Q    all substrings of length Q
jb               join by newlines and print
\$\endgroup\$
6
\$\begingroup\$

Python 65 63

s=lambda t,s:'\n'.join((' '*s+t)[i:s+i]for i in range(len(t)+s))

This was actually used to write the examples. Baseline solution.

>>> print(s("foobarz", 3))

  f
 fo
foo
oob
oba
bar
arz
rz
z
\$\endgroup\$
  • 2
    \$\begingroup\$ i haven't tested it but you should be able to remove the square brackets inside join \$\endgroup\$ – undergroundmonorail May 13 '15 at 13:57
  • \$\begingroup\$ @undergroundmonorail right \$\endgroup\$ – Caridorc May 13 '15 at 15:04
6
\$\begingroup\$

Javascript (ES7 Draft), 61 bytes

f=(s,l)=>[x.substr(i,l)for(i in x=' '.repeat(l)+s)].join(`
`)
<input id="str" value="Some String" />
<input id="num" value="5" />
<button onclick="out.innerHTML=f(str.value, +num.value)">Run</button>
<br /><pre id="out"></pre>

Javascript (ES6) Hardcoded Inputs, 47 bytes

Assuming hard-coded inputs in variables s (string) and l (length), it can be reduced to 47 bytes printing with an alert for each line:

for(i in x=' '.repeat(l)+s)alert(x.substr(i,l))
\$\endgroup\$
5
\$\begingroup\$

K, 19 bytes

{x#'![1]\(x#" "),y}

Tack x spaces (x#" ") onto the beginning of the string y. Then use the "fixed-point scan" form of the operator \ to create the set of rotated strings. A fixed-point in K stops iterating if the result of applying the function returns a repeated result or if the initial input is revisited. Since ![1] will rotate a string one step at a time, ![1]\ is a nice idiom for cyclic permutations. Then we just trim the results with x#'.

A sample run:

  {x#'![1]\(x#" "),y}[4;"some text"]
("    "
 "   s"
 "  so"
 " som"
 "some"
 "ome "
 "me t"
 "e te"
 " tex"
 "text"
 "ext "
 "xt  "
 "t   ")
\$\endgroup\$
5
\$\begingroup\$

J (22)

This ended up longer than I anticipated, but I guess it is not too bad.

[{."0 1[:]\.(' '#~[),]

Fun fact: non of the [ and ] are actually matched, or have anything to do with each other.

\$\endgroup\$
  • \$\begingroup\$ After 3 small changes: [{."1]]\.@,~' '#~[ (18 bytes). \$\endgroup\$ – randomra May 14 '15 at 13:53
5
\$\begingroup\$

Julia, 75 bytes

(s,n)->(n-=1;print(join([(" "^n*s*" "^n)[i:n+i]for i=1:length(s)+n],"\n")))

This creates an unnamed function that accepts a string and integer as input and prints the output. To call it, give it a name, e.g. f=(s,n)->(...).

Ungolfed + explanation:

function f(s, n)
    # Decrement n by 1
    n -= 1

    # Construct the lines as an array using comprehension by repeatedly
    # extracting subsets of the input string padded with spaces
    lines = [(" "^n * s * " "^n)[i:n+i] for i = 1:length(s)+n]

    # Print the array elements separated by a newline
    print(join(lines, "\n"))
end

Examples:

julia> f("banana", 3)
  b
 ba
ban
ana
nan
ana
na 
a

julia> f("Julia", 6)
     J
    Ju
   Jul
  Juli
 Julia
Julia 
ulia  
lia   
ia    
a     

Note that this solution is 66 bytes if s and n are assumed to already exist in the program.

\$\endgroup\$
5
\$\begingroup\$

QBasic, 56 - 10 = 46

This is golfed QBasic--the autoformatter will expand ? into PRINT and add some spaces. Tested with QB64, though there shouldn't be anything in here that won't work with DOS QBasic.

s=SPACE$(n)+s
FOR i=1TO LEN(s)
?MID$(s,i,n)
SLEEP 1
NEXT

QBasic generally isn't good with string operations, but there very conveniently is a function that returns a given number of spaces!

Taking some liberties with "input may be hard-coded," this code expects the variable s to be DIM'd AS STRING, in order to avoid the $ type suffix, as well as the string being assigned to s and the number to n.

Example preamble:

DIM s AS STRING
s="string"
n=4

Output:


   s
  st
 str
stri
trin
ring
ing
ng
g

The top blank row may be eliminated by starting the FOR loop at 2 instead of 1.

Bonus: Adding CLS right before NEXT for a paltry four bytes turns this into... a real marquee!

Marquee

I PRINT CHR$(3) QBasic. :^D

\$\endgroup\$
5
\$\begingroup\$

Ruby, 68, 55 bytes

a=" "*$*[1].to_i+$*[0]+" ";a.size.times{|b|puts a[b...b+$*[1].to_i]}

After an update from @blutorange:

a=" "*(z=$*[1].to_i)+$*[0];a.size.times{|b|puts a[b,z]}

Output:

         S
        So
       Som
      Some
     Somet
    Someth
   Somethi
  Somethin
 Something
Something 
omething a
mething a 
ething a b
thing a bi
hing a bit
ing a bit 
ng a bit l
g a bit lo
 a bit lon
a bit long
 bit longe
bit longer
it longer 
t longer 
 longer 
longer 
onger 
nger 
ger 
er 
r 

ruby marquee.rb "Something a bit longer" 10

First submission so asking for criticism.

\$\endgroup\$
  • 1
    \$\begingroup\$ Looks well done to me, using some ruby shortcuts. The final space doesn't seem to be neccessary though ("only the visible output matters"), and you can save some bytes by using a temporary variable: a=" "*(z=$*[1].to_i)+$*[0];a.size.times{|b|puts a[b,z]} \$\endgroup\$ – blutorange May 14 '15 at 12:17
  • \$\begingroup\$ @blutorange Thanks for the feedback! \$\endgroup\$ – DickieBoy May 14 '15 at 12:23
  • \$\begingroup\$ You're welcome. Feel free to edit the answer if you'd like ;) \$\endgroup\$ – blutorange May 14 '15 at 13:03
4
\$\begingroup\$

Haskell, 61 59 54 bytes

m n=unlines.scanr((take n.).(:))[].(replicate n ' '++)

Usage example:

*Main> putStr $ m 6 "stackoverflow"

     s
    st
   sta
  stac
 stack
stacko
tackov
ackove
ckover
koverf
overfl
verflo
erflow
rflow
flow
low
ow
w

*Main> 

Edit: an empty line at the beginning / end is allowed

\$\endgroup\$
4
\$\begingroup\$

Bash, 109 - 10 = 99 bytes

I see that in the time it took me to write my solution, I have been soundly beaten. Nevertheless, I spent too long writing it to not post it...

Besides which, it has some unique features, like the time between lines printing being user-adjustable based on how much is in the current directory! (Also somewhat inconsistent, depending on how your disk feels)

l=${#1};s=$1;for j in `seq 1 $2`;do s=" $s";done;for i in `seq 0 $((l+$2))`;do echo "${s:i:$2}";find 1>2;done

Example:

cd <some directory in which you own everything recursively>
Marquee.sh "Test Case" 4

   T
  Te
 Tes
Test
est 
st C
t Ca
 Cas
Case
ase
se
e

Ungolfed and commented:

l=${#1} #Length of the incoming string
s=$1 #Can't reassign to the parameter variables, so give it another name
for j in `seq 1 $2`; do
    s=" $s" # Put on the padding spaces
done

for i in `seq 0 $((l+$2))`; do
    #Cut the string and print it. I wish I could lose "padding" that easily!
    echo "${s:i:$2}" #Format is ${string:index:length}, so always the same
    # length, with the index moving into the string as the loop advances
    find 1>2  #Wait "a bit". From ~/, about 6 minutes per line on my junky 
    # computer with a huge home directory. Probably in the <1 sec range for
    # most people.
    #This actually has the same character count as sleep(X), but is much
    # more fun!
done

I've never really tried this before. Suggestions and comments welcome!

\$\endgroup\$
4
\$\begingroup\$

Pure Bash, 61 bytes

printf -vs %$2s"$1"
for((;++i<${#1}+$2;)){
echo "${s:i:$2}"
}

Output:

$ ./marquee.sh testing 4
   t
  te
 tes
test
esti
stin
ting
ing
ng
g
$ 
\$\endgroup\$
  • \$\begingroup\$ I guess I don't understand the distinction "pure" Bash, so maybe this suggestion isn't any good, but sleep(1) is 8 characters and gives you -10 (and is a GNU Core Util) \$\endgroup\$ – Sompom May 14 '15 at 12:57
3
\$\begingroup\$

Perl, 50

$c=$" x$^I;$_="$c$_$c";sleep say$1while(/(?<=(.{$^I}))/g)

57 characters +3 for -i, -n and -l. -10 characters for the sleep.

-i is used for the numeric input, which is stored in $^I. Basically, we add i spaces to the front and end of the input, and then search for every i characters and loop through them with while. say conveniently returns 1 which we can input to sleep.

echo "testing" | perl -i4 -nlE'$c=$" x$^I;$_="$c$_$c";sleep say$1while(/(?<=(.{$^I}))/g)'
\$\endgroup\$
  • \$\begingroup\$ I know this is old, but it's just jumped back onto the front page and I've had a go at shrinking this down somewhat: s/^|$/$"x$^I/eg;sleep say$1 while s/.(.{$^I})/$1/. You can also lose the -l flag, but I think you need to count 5 for -i -n (since -i isn't a default flag) if you run via: echo -n "testing" | perl -i4 -nE'...'. Should still be down to 44 though! \$\endgroup\$ – Dom Hastings Oct 19 '16 at 15:43
  • \$\begingroup\$ @DomHastings thanks Dom! Nice work, I'll edit my answer later :) \$\endgroup\$ – hmatt1 Oct 19 '16 at 15:54
3
\$\begingroup\$

POSIX shell, 94

[ $3 ]||set "`printf "%${2}s"`$1" $2 t
[ "$1" ]&&printf "%-.${2}s" "$1" "
"&&$0 "${1#?}" $2 t

I know it looks closer to perl, but this really is shell!

The first line adds the necessary leading spaces, only on the first time through the loop. It sets $3 to indicate that it has done so.

The second line (N.B. embedded newline) recurses until input is exhausted, printing the first n characters of the string, then invoking itself with the first character removed from $1.

Tested with Debian /bin/dash - sample outputs follow:

./marquee "testing" 4

   t
  te
 tes
test
esti
stin
ting
ing
ng
g

./marquee "Something a bit longer" 10

         S
        So
       Som
      Some
     Somet
    Someth
   Somethi
  Somethin
 Something
Something 
omething a
mething a 
ething a b
thing a bi
hing a bit
ing a bit 
ng a bit l
g a bit lo
 a bit lon
a bit long
 bit longe
bit longer
it longer
t longer
 longer
longer
onger
nger
ger
er
r

./marquee "small" 15

              s
             sm
            sma
           smal
          small
         small
        small
       small
      small
     small
    small
   small
  small
 small
small
mall
all
ll
l
\$\endgroup\$
  • \$\begingroup\$ I can add 9 chars to get the -10 bonus! [ "$1" ]&&printf "%-.${2}s" "$1" " "&&sleep 1&&$0 "${1#?}" $2 t \$\endgroup\$ – Toby Speight May 13 '15 at 21:09
3
\$\begingroup\$

Python 2, 51 bytes / 37 bytes

Without hardcoded input (51 bytes):

def f(s,n):
 s=" "*n+s
 while s:print s[:n];s=s[1:]

Call like f("testing", 4).

With hardcoded input (37 bytes):

s="testing";n=4

s=" "*n+s
while s:print s[:n];s=s[1:]

Both versions output an initial line of spaces.

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

Python 2, (54 bytes - 10 = 44) 64 62 60 46

(I assumed the line for hard-coded input doesn't add to byte count.)

I didn't see a program yet that actually sleeps between printing lines, so I made one that does, since it looks more like a marquee that way. This program is 2 more bytes in Python 3.

EDIT: The program now does a computation instead of sleeping. I used i in the calculation so that the program doesn't store it as a constant, but must compute it each time.

Try the Python 3 one here (the Python 2 repl is buggy)

i=0
while s[i-n:]:print((' '*n+s)[i:n+i]);i+=1;i**7**7
\$\endgroup\$
  • \$\begingroup\$ Maybe instead of time.sleep there's some long computation you can use? Also, it's a bit shorter to use a while loop: i=0\nwhile s[i-n:]:print(' '*n+s)[i:n+i];i+=1 \$\endgroup\$ – xnor May 14 '15 at 1:52
  • \$\begingroup\$ @xnor Exponentiation a couple times works pretty well for a computation. \$\endgroup\$ – mbomb007 May 14 '15 at 14:40
  • \$\begingroup\$ @mbomb007 I don't think you need to store the value of the computation to get python to actually do it (so you can save 'q='). Also, x^7^7 is mathematically equivalent to x^49, although python seems to resolve the latter slightly faster for me. You can save a few characters that way. \$\endgroup\$ – Sompom May 14 '15 at 20:13
  • \$\begingroup\$ @Sompom Try it in the repl. If I consolidate the expression into a single exponentiation, it completely removes the time delay. But I will remove q=. Thanks. \$\endgroup\$ – mbomb007 May 14 '15 at 21:51
  • \$\begingroup\$ @Sompom Exponentiation is right-associative, so this is actually i**(7**7) \$\endgroup\$ – Sp3000 May 14 '15 at 23:17
3
\$\begingroup\$

Pyth, 12 bytes

jb.:X*dyQQzQ

Demonstration.


Pyth, 17 - 10 = 7 bytes

FN.:X*dyQQzQ&.p9N

This version employs a delay between line prints. This can be seen on the command line compiler, which you can get here.

Run the following:

pyth -c 'FN.:X*dyQQzQ&.p9N' <<< 'testing
4'

This has a delay of about 0.3 seconds before each print. If you prefer a longer delay, you can use:

FN.:X*dyQQzQ&.pTN

This has about a 4 second delay.

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

Java, 133 119 115

int i;void f(String s,int n){for(;++i<n;)s=" "+s+" ";for(;i<=s.length();)System.out.println(s.substring(i-n,i++));}

Long version:

int i;
void f(String s, int n) {
    for(; ++i < n;)
        s = " " + s + " ";
    for(; i<=s.length();)
        System.out.println(s.substring(i-n, i++));
}

Padding is applied to the string, and then substrings of the padded string are printed to the console.

-4 bytes thanks to @KevinCruijssen.

\$\endgroup\$
  • \$\begingroup\$ I know it's been more than a year, but you can golf the second for-loop a bit: for(;i<= s.length();System.out.println(s.substring(i-n,i++))); (-3 bytes) \$\endgroup\$ – Kevin Cruijssen Oct 19 '16 at 8:48
  • 1
    \$\begingroup\$ Doesn't mean it can't be improved. :) Thanks. \$\endgroup\$ – TNT Oct 19 '16 at 14:37
2
\$\begingroup\$

Matlab, 95

As always, it is a manipulation of matrices. The core here is the command spdiags which lets you create diagonal matrices very easily.

t=input('');
n=numel(t);
k=input('');
flipud(char(full(spdiags(repmat(t,n+k-1,1),1-n:0,n+k-1,k))))

With hardcoding 71 bytes (expected string stored in t and the number in k)

n=numel(t);flipud(char(full(spdiags(repmat(t,n+k-1,1),1-n:0,n+k-1,k))))
\$\endgroup\$
2
\$\begingroup\$

APL, 50 - 10 = 40 chars

I'm sure it could be shorter. 50 is the length of the program without the two constants.

{⍵≡⍬:⍬⋄⎕←↑⍵⋄⎕DL 99⋄∇1↓⍵}⊂[0]⊖s⍴⍨n,¯1+⍴s←'brace yourself',' '⍴⍨n←6

Explanation:

                               ' '⍴⍨n←6   call the number 'n' and make n spaces
            s←'brace yourself',           append them to the string and call it 's'
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
                             s⍴⍨n,¯1+⍴s   make a len(s)-1 by n matrix by repeating s 
                        ⊂[0]⊖             reflect the matrix and extract the columns
{                      }                  pass the list of columns to this function
 ⍵≡⍬:⍬⋄                                   return if the list is empty
       ⎕←↑⍵⋄                              print the first column (as a row)
            ⎕DL 99⋄                       wait for 99ms
                   ∇1↓⍵                   recurse with the rest of the columns

Developed for ngn APL on the terminal.

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

Powershell - 85 83 bytes

It's late, it's not going to win :-) But I thought I'd throw in a Powershell one for completeness:

function m($s,$n){1..$($n+$s.length)|%{-join(" "*$n+$s+" "*$n)[$_-1..$($n+$_-1)]}}

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

Cobra - 60

def f(n,s)
    s=' '.repeat(n)+s
    while''<s,print (s=s[1:])[:n]
\$\endgroup\$
1
\$\begingroup\$

Groovy - 82

n=args[1]as int;t=" "*n+args[0]+" "*n;(0..t.size()-n).each{println t[it..it+n-1]}
\$\endgroup\$
1
\$\begingroup\$

Lua, 79 bytes

r=io.read;t=r()w=r()s=" "t=s:rep(w)..t;for i=1,t:len()do print(t:sub(i,i+w))end
\$\endgroup\$
1
\$\begingroup\$

C#, 112 bytes

s=>n=>{var r=new string(' ',n-1);s=r+s+r;r="";for(int i=0;i<s.Length-n+1;)r+=s.Substring(i++,n)+"\n";return r;};

Full program with ungolfed method and test cases:

using System;

namespace FakeMarqueeText
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string,Func<int,string>>f= s=>n=>
            {
                var r=new string(' ',n-1);
                s=r+s+r;
                r="";
                for(int i=0;i<s.Length-n+1;)
                    r+=s.Substring(i++,n)+"\n";

                return r;
            };

            // test cases:
            Console.WriteLine(f("testing")(4));
            Console.WriteLine(f("hello")(2));
            Console.WriteLine(f("foobarz")(3));
            Console.WriteLine(f("Something a bit longer")(10));
            Console.WriteLine(f("small")(15));
            Console.WriteLine(f("aaa")(3));
            Console.WriteLine(f("brace yourself")(6));

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

Jelly, 15 bytes - 10 = 5 (non-competing?)

ḶṚ⁶ẋ;€⁹ZṄœS$€ṛ“

Try it online!

Sleeps for a second between each line. Prints a trailing newline, if that's acceptable.

yay Jelly beats Charcoal

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

PHP4.1, 85-10=75 bytes

Yes, this is a very old version, but it has a functionality I need.
You can still run it in any more recent versions of PHP, but you need to set the variables yourself before running the code below.

That helps me to reduce the size of my code a lot!

It is really basic:

<?for($s=str_repeat(' ',$n).$s;$i++<strlen($s)+$n;sleep(1))echo substr($s,$i,$n),'
';

I ran for the bonus due to this, quoting O.P.:

If your program is standalone (i.e. when run actually prints the lines) (Input can be hard-coded but easy to change) and sleeps a bit between each line of output you get a -10 bonus.

As you can obviously see, it has a sleep.

This assumes that you have register_globals enabled by default, which were the default settings for this version.


You can easily test in your browser, with minimal some changes:

//detects if it is running in js or php
//true for js, false for php
if('\0'=="\0")
{
	function strlen($s){
		return $s.length;
	}
	
	function str_repeat($s,$n){
		return Array($n+1).join($s);
	}
	
	function substr($s,$n,$m){
		return $s.substr($n,$m);
	}
	
	function printf($s){
		document.write($s);
	}
	
	function concat($a,$b){
		return $a+$b;
	}
}
else
{
	function concat($a,$b){
		return $a.$b;
	}
}

//sets the variables, not required for PHP if you pass the values by GET or POST
$i=0;
$s='example';
$n=6;



for($s=concat(str_repeat('-',$n),$s);$i++<strlen($s)+$n;)printf(concat(substr($s,$i,$n),'<br>'));
*{font-family:monospace}

The above code is a polyglot and you can run in your browser or in a PHP interpreter. Shouldn't I get a prize for this? A cookie, perhaps?

List of changes:

  • Removed the sleep(1) in this test
  • Created 2 versions of the function concat
    The goal is to overcome PHP and JS differences in concatenating strings.
  • Instead of a space, a - is used to fill the space
  • Instead of echo, printf is uses instead (PHP limitation)
  • Instead or a 'real' newline, <br> is uses instead
\$\endgroup\$
1
\$\begingroup\$

J, 15 14 bytes

,.~/@(' '&,~<)

Try it online!

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

APL (Dyalog), 17 bytes

⌽⍉↑(⎕,⍨' '/⍨⊢)¨⍳⎕

Try it online!

(the program assumes ⎕IO←0 which is default on many machines)

Explanation

⍳⎕               Create a range 0 .. input-1
¨                For each element in this range do:
 ' '/⍨⊢           A space duplicated right argument (which is the element in  the range) times
 ⎕,⍨              Concatenated with the input string to its right
⌽⍉               Transpose and reflect horizontally
\$\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.