Fake Marquee Text

46

5

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.

Caridorc

Posted 2015-05-13T13:12:10.200

Reputation: 2 254

What do you mean by "includes Output" ? – Optimizer – 2015-05-13T13:13:56.210

Also, I suggest you specify the input structure and format. For example, what if I want my text to have spaces ? – Optimizer – 2015-05-13T13:14:41.127

1Also, do we have to print the first and the last lines which are simply spaces ? – Optimizer – 2015-05-13T13:17:45.437

What about trailing spaces on the last few lines ? – Optimizer – 2015-05-13T13:18:00.523

@Optimizer Spaces can be treated exactly like the other symbols, may be inside or after the message. – Caridorc – 2015-05-13T13:20:34.163

My question was regarding trailing spaces on the lines where the complete string is printed. For example aaa 3, do we print a space after the aa on the 3rd line or not ? – Optimizer – 2015-05-13T13:23:39.497

@Optimizer only the visible output is important in this challenge, as spaces are invisible you may or may not print them on your choosing. – Caridorc – 2015-05-13T13:25:40.247

2

Inspired by this?

– Spikatrix – 2015-05-13T14:18:52.873

@CoolGuy yes, I forgot to mention that – Caridorc – 2015-05-13T14:39:30.710

1A single empty line at the beginning seems to be allowed. How about another empty line at the end? – nimi – 2015-05-13T17:57:54.367

@nimi only the visible output is important in this challenge – Caridorc – 2015-05-13T20:03:49.597

2Since you're talking about "only visible output", can we assume that the input will only be printable ASCII? (character codes 0x20 to 0x7E) – Martin Ender – 2015-05-13T23:24:40.287

@MartinBüttner input can be assumed to be in the character codes 0x20 to 0x7E – Caridorc – 2015-05-14T11:31:10.590

Why don't we just start calling marquees scrolly texts again? It would make the world a better place. – Shaun Bebbers – 2017-06-23T09:52:39.760

Old question, but this should probably specify maximums for the inputs: how large can n be? How long can the string be? My answer works for all given samples, but fails for extremely large inputs. – BradC – 2017-06-23T14:42:46.037

@BradC I guess max n should be 79 the standard max line length – Caridorc – 2017-06-23T18:30:35.827

Answers

24

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!;}/

Martin Ender

Posted 2015-05-13T13:12:10.200

Reputation: 184 808

Nice use of z. Assuming the input is printable ASCII, you can replace W% with $. – Dennis – 2015-05-13T23:23:12.827

@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.) – Martin Ender – 2015-05-13T23:25:33.680

15

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

CL-

Posted 2015-05-13T13:12:10.200

Reputation: 751

14

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

Jakube

Posted 2015-05-13T13:12:10.200

Reputation: 21 462

6

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

Caridorc

Posted 2015-05-13T13:12:10.200

Reputation: 2 254

2i haven't tested it but you should be able to remove the square brackets inside join – undergroundmonorail – 2015-05-13T13:57:22.103

@undergroundmonorail right – Caridorc – 2015-05-13T15:04:20.380

6

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

nderscore

Posted 2015-05-13T13:12:10.200

Reputation: 4 912

5

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

JohnE

Posted 2015-05-13T13:12:10.200

Reputation: 4 632

5

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.

Alex A.

Posted 2015-05-13T13:12:10.200

Reputation: 23 761

5

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.

ɐɔıʇǝɥʇuʎs

Posted 2015-05-13T13:12:10.200

Reputation: 4 449

After 3 small changes: [{."1]]\.@,~' '#~[ (18 bytes). – randomra – 2015-05-14T13:53:53.457

5

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

DLosc

Posted 2015-05-13T13:12:10.200

Reputation: 21 213

I should try QBasic again... It's what I first learned on – Canadian Luke – 2015-05-14T17:22:34.853

5

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.

DickieBoy

Posted 2015-05-13T13:12:10.200

Reputation: 160

1Looks 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]} – blutorange – 2015-05-14T12:17:51.850

@blutorange Thanks for the feedback! – DickieBoy – 2015-05-14T12:23:23.757

You're welcome. Feel free to edit the answer if you'd like ;) – blutorange – 2015-05-14T13:03:53.347

4

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

nimi

Posted 2015-05-13T13:12:10.200

Reputation: 34 639

4

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!

Sompom

Posted 2015-05-13T13:12:10.200

Reputation: 221

4

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
$ 

Digital Trauma

Posted 2015-05-13T13:12:10.200

Reputation: 64 644

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) – Sompom – 2015-05-14T12:57:19.687

3

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

hmatt1

Posted 2015-05-13T13:12:10.200

Reputation: 3 356

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! – Dom Hastings – 2016-10-19T15:43:33.917

@DomHastings thanks Dom! Nice work, I'll edit my answer later :) – hmatt1 – 2016-10-19T15:54:16.160

3

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.

TNT

Posted 2015-05-13T13:12:10.200

Reputation: 2 442

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) – Kevin Cruijssen – 2016-10-19T08:48:53.790

1Doesn't mean it can't be improved. :) Thanks. – TNT – 2016-10-19T14:37:53.853

3

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

mbomb007

Posted 2015-05-13T13:12:10.200

Reputation: 21 944

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 – xnor – 2015-05-14T01:52:19.260

@xnor Exponentiation a couple times works pretty well for a computation. – mbomb007 – 2015-05-14T14:40:46.533

@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. – Sompom – 2015-05-14T20:13:06.403

@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. – mbomb007 – 2015-05-14T21:51:14.787

@Sompom Exponentiation is right-associative, so this is actually i**(7**7) – Sp3000 – 2015-05-14T23:17:09.940

@Sp3000 and mbomb007 -- Oops. I guess that I saw a time delay was mostly based on me running it on my old and slow work computer :) . I'll ponder, but I don't know any clever trick off the top of my head. – Sompom – 2015-05-15T05:13:11.887

Something with 9e99 or the like might work better – Sp3000 – 2015-05-15T06:18:49.923

@Sp3000 I tried that already. Anything larger than i**9e1 causes an overflow error, and there is not a useful delay on the ones that run. – mbomb007 – 2015-05-15T14:05:29.417

3

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

Toby Speight

Posted 2015-05-13T13:12:10.200

Reputation: 5 058

I can add 9 chars to get the -10 bonus! [ "$1" ]&&printf "%-.${2}s" "$1" " "&&sleep 1&&$0 "${1#?}" $2 t – Toby Speight – 2015-05-13T21:09:48.710

3

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.

Sp3000

Posted 2015-05-13T13:12:10.200

Reputation: 58 729

3

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.

isaacg

Posted 2015-05-13T13:12:10.200

Reputation: 39 268

2

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

flawr

Posted 2015-05-13T13:12:10.200

Reputation: 40 560

2

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.

Tobia

Posted 2015-05-13T13:12:10.200

Reputation: 5 455

2

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

Chris J

Posted 2015-05-13T13:12:10.200

Reputation: 199

1

J, 15 14 bytes

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

Try it online!

miles

Posted 2015-05-13T13:12:10.200

Reputation: 15 654

1

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

Erik the Outgolfer

Posted 2015-05-13T13:12:10.200

Reputation: 38 134

1

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

user41805

Posted 2015-05-13T13:12:10.200

Reputation: 16 320

1

Python 3, 62 bytes

Definitely think it can be golfed further, particularly with the for loop notation.

Unfortunately I'm not that fluent with Python yet, maybe someone here can help.

def f(s,n):
 s=n*" "+s
 for i in range(len(s)):print(s[i:i+n])

Try it online!

I hope leading spaces are allowed.

Fedone

Posted 2015-05-13T13:12:10.200

Reputation: 157

Welcome to site! – Post Rock Garf Hunter – 2017-06-23T14:55:24.343

1

PowerShell, 60 bytes

param($s,$n)($t=' '*--$n+$s)|% t*y|%{-join$t[$i..($i+++$n)]}

Try it online!

mazzy

Posted 2015-05-13T13:12:10.200

Reputation: 4 832

1

Japt -R, 7 bytes

VÆiYîÃz

Run it online

Oliver

Posted 2015-05-13T13:12:10.200

Reputation: 7 160

1

Add++, 38 bytes

L#,b]*ApR1€Ω_32C€*dbR@$BcB]€¦+bU@BcBJn

Try it online!

caird coinheringaahing

Posted 2015-05-13T13:12:10.200

Reputation: 13 702

1

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

Ismael Miguel

Posted 2015-05-13T13:12:10.200

Reputation: 6 797

1

Cobra - 60

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

Οurous

Posted 2015-05-13T13:12:10.200

Reputation: 7 916

1

Groovy - 82

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

dbramwell

Posted 2015-05-13T13:12:10.200

Reputation: 201

1

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

Blab

Posted 2015-05-13T13:12:10.200

Reputation: 451

1

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

        }
    }
}

adrianmp

Posted 2015-05-13T13:12:10.200

Reputation: 1 592

0

Charcoal, 7 bytes (non-competing?)

FN«P↓η↗

Try it online!

Erik the Outgolfer

Posted 2015-05-13T13:12:10.200

Reputation: 38 134

0

Google Sheets, 79 bytes

=ARRAYFORMULA(JOIN("
",MID(REPT(" ",A2)&A1,ROW(OFFSET(A2,0,0,LEN(A1)+A2)),A2)))

Text is input in cell A1 and the marquee width is in cell A2.
ARRARYFORMULA + ROW(OFFSET()) lets us return multiple values into the MID function.
After that, all we do is return A2 characters from the middle of the text prepended with spaces as needed.
JOIN combines them all with a line break in between each.

Sample outputs:

Sample Outputs

I expected this challenge to require clearing the screen so it looks like a marquee in place but, as it (currently) doesn't, I think this answer is valid.

Engineer Toast

Posted 2015-05-13T13:12:10.200

Reputation: 5 769

0

T-SQL, 110 bytes

DECLARE @n INT,@ CHAR(99)SELECT @n=n,@=space(@n)+s FROM t
L:SET @=RIGHT(@,98)PRINT LEFT(@,@n)IF LEN(@)>0GOTO L

Input is via pre-existing table t with columns n (INT) and s (CHAR), per our IO standards.

Formatted:

DECLARE @n INT, @ CHAR(99)
SELECT @n=n, @=space(@n)+s FROM t
L:
    SET @=RIGHT(@,98)
    PRINT LEFT(@,@n)
IF LEN(@)>0 GOTO L

The question didn't specify the range of values my code had to handle; it works for all given samples, but will cut off the text if n + len(s) exceeds 99. To handle huge values of n and much longer strings without cutting off, add two bytes by changing 99 to 999 and 98 to 998.

BradC

Posted 2015-05-13T13:12:10.200

Reputation: 6 099

0

Perl 5, 43

53 bytes - 10 bonus

push@F,($")x($t=<>);sleep say@F[$_-$t..$_-1]for 1..@F

Try it online!

Xcali

Posted 2015-05-13T13:12:10.200

Reputation: 7 671