44
\$\begingroup\$

Print a continuous sinusoidal wave scrolling vertically on a terminal. The program should not terminate and should continuously scroll down the wave (except until it is somehow interrupted). You may assume overflow is not a problem (i.e. you may use infinite loops with incrementing counters, or infinite recursion).

The wave should satisfy the following properties:

  • Amplitude = 20 chars (peak amplitude)
  • Period = 60 to 65 lines (inclusive)
  • The output should only consist of spaces, newline and |
  • After each line of output, pause for 50ms

Sample output:

                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
            |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
          |
        |
       |
     |
    |
   |
  |
 |
 |
 |
 |
 |
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |

The above output should go on forever unless otherwise interrupted, e.g. SIGINT or SIGKILL, or closing terminal window, or you power off your machine, or the Sun swallows the Earth, etc.

Shortest code wins.

Note. I am aware of a similar problem on Display Scrolling Waves but this isn't exactly the same. In my problem, the wave is not to be scrolled "in place" - just output it on a terminal. Also, this is an ascii-art problem, so don't use Mathematica to plot it.

\$\endgroup\$
14
  • 1
    \$\begingroup\$ Peak amplitude, peak-to-peak amplitude, or root-square amplitude? \$\endgroup\$
    – DavidC
    Commented Jan 16, 2014 at 20:31
  • \$\begingroup\$ Peak amplitude. \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 20:38
  • 1
    \$\begingroup\$ Is it ok to draw a wave with just |s and no spaces? \$\endgroup\$
    – Gelatin
    Commented Jan 17, 2014 at 1:23
  • 1
    \$\begingroup\$ All answers yet are invalid. They also stop for SIGKILL not just for SIGINT. \$\endgroup\$
    – bot47
    Commented Apr 4, 2014 at 9:02
  • 1
    \$\begingroup\$ @Max Ried fine, I will change it to "should go on forever unless otherwise interrupted". \$\endgroup\$
    – user12205
    Commented Apr 4, 2014 at 10:28

41 Answers 41

23
\$\begingroup\$

C, 74 73 70 69 67 characters

67 character solution with many good ideas from @ugoren & others:

i;main(j){main(poll(printf("%*c|\n",j=21+sin(i++*.1)*20,0),0,50));}

69 character solution with while loop instead of recursion:

i;main(j){while(printf("%*c|\n",j=21+sin(i++*.1)*20,0))poll(0,0,50);}

Approaching perl territory. :)

\$\endgroup\$
14
  • 1
    \$\begingroup\$ This was inspired by @ace's own C answer. \$\endgroup\$
    – treamur
    Commented Jan 16, 2014 at 21:41
  • 2
    \$\begingroup\$ I think you could use 5E4 instead of 50000. \$\endgroup\$
    – musiphil
    Commented Jan 17, 2014 at 0:00
  • 2
    \$\begingroup\$ I think you could use *.1 instead of /10. \$\endgroup\$
    – moala
    Commented Jan 17, 2014 at 1:58
  • 1
    \$\begingroup\$ @musiphil, I also thought about using 5E4, but it turns out that it does not work: Without showing the compiler usleep() prototype, you would have to explicitly cast the 5E4. \$\endgroup\$
    – treamur
    Commented Jan 17, 2014 at 5:32
  • 2
    \$\begingroup\$ You can cut out two more characters by moving the assignment to j into the printf, like this: printf("%*s\n",j=21+sin(i++*.1)*20,"|"). The resulting type is still int so it's a valid field width argument. \$\endgroup\$
    – Art
    Commented Jan 17, 2014 at 10:17
13
\$\begingroup\$

APL (35)

(Yes, it does fit in 35 bytes, here's a 1-byte APL encoding)

{∇⍵+⌈⎕DL.05⊣⎕←'|'↑⍨-21+⌈20×1○⍵×.1}1

Explanation:

  • {...}1: call the function with 1 at the beginning
  • 1○⍵×.1: close enough for government work to sin(⍵×π÷30). (1○ is sin).
  • -21+⌈20: normalize to the range 1..40 and negate
  • '|'↑⍨: take the last N characters from the string '|' (which results in a string of spaces with a | at the end
  • ⎕←: display
  • ⌈⎕DL.05: wait 50 ms and return 1. (⎕DL returns the amount of time it actually waited, which is going to be close to 0.05, rounding that value up gives 1).
  • ∇⍵+: add that number (1) to and run the function again.
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Darn... I thought trigonometric functions plus the time delay would leave you guys out \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 20:22
  • 2
    \$\begingroup\$ Here a 33 char one: {⎕←'|'↑⍨-⌈20×1+1○⍵⋄∇.1+⍵⊣⎕DL.05}0 \$\endgroup\$
    – Tobia
    Commented Jan 18, 2014 at 0:03
  • 4
    \$\begingroup\$ @ace LOL. You should check APL out, it's not a novelty language. It's very old and has been in use in large systems for decades. It's quite unique, compared to anything else. IMHO the symbols make it much more readable that the ASCII-only derivatives (J) \$\endgroup\$
    – Tobia
    Commented Jan 18, 2014 at 0:16
12
\$\begingroup\$

Mathematica 121 104 80 67 64

n=1;While[0<1,Spacer[70 Sin[n Pi/32]+70]~Print~"|";[email protected]; n++]

sine

\$\endgroup\$
8
  • \$\begingroup\$ question says not to use mathematica to plot it. is this different than that somehow? \$\endgroup\$
    – Malachi
    Commented Jan 16, 2014 at 23:37
  • 14
    \$\begingroup\$ @Malachi Yes. This uses mathematica to calculate it, just like any other answer. Using mathematica to plot would be telling mathematica to plot x=20*sin(pi*y/30)+20 or something similar. \$\endgroup\$
    – Justin
    Commented Jan 16, 2014 at 23:49
  • \$\begingroup\$ ok I get what you are saying thank you for the clarification \$\endgroup\$
    – Malachi
    Commented Jan 17, 2014 at 0:41
  • 1
    \$\begingroup\$ And here is a 58 char version Do[Spacer[70*Sin[n*Pi/32]+70]~Print~"|";[email protected],{n,18!}] \$\endgroup\$
    – Ajasja
    Commented Jan 17, 2014 at 10:22
  • 1
    \$\begingroup\$ Im not a Mathematica user, but i think you can change 1 == 1 to 0 < 1 to decrease 1 char. \$\endgroup\$
    – CCP
    Commented Mar 24, 2014 at 23:27
12
\$\begingroup\$

Perl - 64 (or 60) bytes

The following uses a Windows-specific shell command:

`sleep/m50`,print$"x(20.5-$_*(32-abs)/12.8),'|
'for-32..31;do$0

The following uses a GNU/Linux-specific shell command:

`sleep .05`,print$"x(20.5-$_*(32-abs)/12.8),'|
'for-32..31;do$0

Both at 64 bytes.

  • Period is 64.
  • Maximum amplitude is exactly 20.
  • The curve is perfectly symmetric.
  • Every period is identical.
                    |
                      |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                      |
                    |
                  |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                  |
                    |
                      |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                      |
                    |
                  |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
 |
  |
   |
    |
     |
      |
        |
         |
           |
             |
               |
                  |
                    |

Note that this isn't exactly a sinusoidal wave, but rather a quadratic interpolation. Plotted against an actual sin:

At the granularity required, these are visually indistinguishable.

If the aesthetics aren't so important, I offer a 60 byte alternative, with period length 62, maximum amplitude of ~20.02, and slight asymmetries:

`sleep/m50`,print$"x(20-$_*(31-abs)/12),'|
'for-31..30;do$0
\$\endgroup\$
2
  • \$\begingroup\$ This isn't a sinusoidal wave; it is simply parabolas (if I read your code right). (If you can represent this with some sinusoidal wave, I'd love to see the function). \$\endgroup\$
    – Justin
    Commented Jan 21, 2014 at 4:01
  • \$\begingroup\$ Sine is a formula, if you replicate the formula it is still a Sinusoidal wave. and this is probably a variant of Sine in some fashion. \$\endgroup\$
    – Malachi
    Commented Feb 10, 2014 at 15:13
11
\$\begingroup\$

Perl, 48 (68)

GNU sleep version: 48

print$"x(25+20*sin).'|
';$_+=.1;`sleep .05`;do$0

Cross platform: 68

use Time::HiRes"sleep";print$"x(25+20*sin).'|
';$_+=.1;sleep.05;do$0

Removed the use of Time::HiRes module by using shell sleep function. Shortened increment as per Ruby example. Shortened using $" and $0 seeing hints from Primo's work Thanks for hints Primo.

\$\endgroup\$
8
  • \$\begingroup\$ I saved this as a file test.pl and ran perl ./test.pl, however the waiting time does not match the specification. Also, the amplitude of the wave is too small. (This amplitude refers to the length between the peak and the equilibrium position.) \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 21:42
  • \$\begingroup\$ I guess if I changed the increment from .105 to .1 I would beat ruby at 56 chars! \$\endgroup\$ Commented Jan 16, 2014 at 22:50
  • \$\begingroup\$ @primo - my shell sleep does do times shorter than 1 second... \$\endgroup\$ Commented Jan 17, 2014 at 8:32
  • \$\begingroup\$ man sleepunsigned int sleep(unsigned int seconds);. It won't error, but the actual sleep interval is zero. Some suggestions to make yours shorter: change $d to $_, and then use (25+20*sin), and change the \n for a literal newline. \$\endgroup\$
    – primo
    Commented Jan 17, 2014 at 8:45
  • 2
    \$\begingroup\$ @primo man 1 sleep on a GNU/Linux bash shell tells us that Unlike most implementations that require NUMBER be an integer, here NUMBER may be an arbitrary floating point number. \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 8:58
8
\$\begingroup\$

Ruby 56

i=0
loop{puts" "*(20*Math.sin(i+=0.1)+20)+?|;sleep 0.05}
\$\endgroup\$
2
  • \$\begingroup\$ Is replacing puts with p allowed? \$\endgroup\$
    – Slicedpan
    Commented Jan 17, 2014 at 10:12
  • 1
    \$\begingroup\$ @Slicedpan I think I won't, since this is a challenge to draw something. p will add double quotes around around each line and alter the "drawing". \$\endgroup\$
    – daniero
    Commented Jan 17, 2014 at 15:37
7
\$\begingroup\$

Befunge 98 - 103 100

:1g:02p' \k:02gk,'|,a,$ff*:*8*kz1+:'<\`*
468:<=?ABDEFGGGHGGGFEDBA?=<:86420.,+)'&$#"!!! !!!"#$&')+,.02

Cheers for a program that does this, in a language without trigonometric capabilities; the first program in fact. The second line is simply data; the character corresponding with the ascii value of the sin, added to a space character.

EDIT: I saved 3 chars by not subtracting the space away; the sinusoid is translated 32 units to the right (which is valid).

Befunge also does not have a sleep command, or something similar. It would be nice to find a fingerprint, but I couldn't find one, so ff*:*8* pushes 8*225**2 (405000) and kz runs a noop that many times (well, that many times + 1). On windows command line with pyfunge, this turns out to be about 50 milliseconds, so I say I'm good. Note: if anyone knows a good fingerprint for this, please let me know.

The last part of the code simply checks if the counter (for the data line) is past the data, if it is, the the counter is reset to 0.

I used this to generate the data.


Taylor Series

Although this version is 105 chars, I just had to include it:

:::f`!4*jf2*-:::*:*9*\:*aa*:*:01p*-01g9*/a2*+\$\f`!4*j01-*b2*+:01p' \k:01gk,$'|,a,ff*:*8*kz1+:f3*`!3*j$e-

I was trying to shorten my program, and decided to look at the taylor series for cosine (sine is harder to calculate). I changed x to pi * x / 30 to match the period requested here, then multiplied by 20 to match the amplitude. I made some simplifications (adjusted factors for canceling, without changing the value of the function by much). Then I implemented it. Sadly, it is not a shorter implementation.

:f`!4*jf2*-

checks whether the values of the taylor series are getting inaccurate (about x = 15). If they are, then I compute the taylor series for x - 30 instead of x.

:::*:*9*\:*aa*:*:01p*-01g9*/a2*+

is my implementation of the taylor series at x = 0, when x is the value on the stack.

\$\f`!4*j01-* 

negates the value of the taylor series if the taylor series needed adjustment.

b2*+

make the cosine wave positive; otherwise, the printing would not work.

:01p' \k:01gk,$'|,a,

prints the wave

ff*:*8*kz1+

makeshift wait for 50 milliseconds, then increment x

:f3*`!3*j$e-

If x is greater than 45, change it to -14 (again, taylor series error adjustment).

\$\endgroup\$
3
  • \$\begingroup\$ This is exactly the kind of answer I'm looking forward to, hope you can golf it down :) \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 8:08
  • 1
    \$\begingroup\$ There! I successfully decreased the code length by -5 chars! And there is still room for improvement! \$\endgroup\$
    – Justin
    Commented Jan 19, 2014 at 9:42
  • \$\begingroup\$ @Quincunx my perl solution also does not use any built in trig functions ;) \$\endgroup\$
    – primo
    Commented Jan 21, 2014 at 3:05
6
\$\begingroup\$

Python, 108,93,90,89,88

import math,time
a=0
while 1:print" "*int(20+20*math.sin(a))+"|";time.sleep(.05);a+=.1

Now with infinite scrolling :)

Edit: ok, 90. Enough?

Edit:Edit: no, 89.

Edit:Edit:Edit: 88 thanks to boothby.

\$\endgroup\$
2
  • \$\begingroup\$ Sorry if I haven't made the question clear - your program should not terminate and should continuously scroll down the wave (except until SIGINT) \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 19:26
  • 1
    \$\begingroup\$ a=0. -> a=0 gets you to 88 \$\endgroup\$
    – boothby
    Commented Jan 16, 2014 at 23:03
5
\$\begingroup\$

PHP, 59 characters

<?for(;;usleep(5e4))echo str_pad('',22+20*sin($a+=.1)).~ƒõ;
\$\endgroup\$
7
  • 1
    \$\begingroup\$ You can save yourself some bytes by using echo ...; in place of fwrite(STDOUT,...);. \$\endgroup\$
    – primo
    Commented Jan 17, 2014 at 6:08
  • \$\begingroup\$ That makes sense when calling from the command line anyway. 10 characters saved - thanks primo. \$\endgroup\$ Commented Jan 17, 2014 at 10:02
  • 1
    \$\begingroup\$ 58: <?for(;;)echo~str_pad(ƒõ,22+20*sin($a+=.1),ß,usleep(5e4)); \$\endgroup\$
    – primo
    Commented Jan 17, 2014 at 10:56
  • \$\begingroup\$ Very nice. I won't edit my answer with those changes, you should post as your own. \$\endgroup\$ Commented Jan 17, 2014 at 11:05
  • 1
    \$\begingroup\$ @ace it needs to be saved with an ansi encoding. ideone automatically converts everything to utf-8, which breaks. ~ƒõ is just shorthand for "|\n". \$\endgroup\$
    – primo
    Commented Jan 17, 2014 at 11:17
5
\$\begingroup\$

C64 BASIC, 64 PETSCII chars

enter image description here

On a PAL C64, For i=0 to 2:next i cycles for approx. 0,05 seconds, so the delay time is respected.

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

Javascript 88 76 78 characters

setInterval('console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")',i=50)

Based on Kendall Frey's code.

\$\endgroup\$
2
  • \$\begingroup\$ You never initialize i, so it prints a straight line instead of a wave. \$\endgroup\$
    – gilly3
    Commented Jan 16, 2014 at 22:37
  • \$\begingroup\$ My mistake... It probably worked because I had ran Kendall's script already in my console, so i was initialised already for me. \$\endgroup\$
    – joeytje50
    Commented Jan 16, 2014 at 22:41
4
\$\begingroup\$

C - 86+3 characters

Thanks shiona and Josh for the edit

i;main(j){for(;j++<21+sin(i*.1)*20;)putchar(32);puts("|");usleep(50000);i++;main(1);}

i;main(j){for(j=0;j++<20+sin(i/10.)*20;)putchar(32);puts("|");usleep(50000);i++;main();}

float i;main(j){for(j=0;j++<20+sin(i)*20;)putchar(32);puts("|");usleep(50000);i+=.1;main();}

Compiled with the -lm flag, I assume I need to add 3 chars

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Would it work if you made i an int and just divided it by 10.0 (or 9.9 to save a char?) within the call to sin()? i;main(j){for(j=0;j++<20+sin(i/10.0)*20;)putchar(32);puts("|");usleep(50000);i++;main();} \$\endgroup\$
    – shiona
    Commented Jan 16, 2014 at 18:24
  • \$\begingroup\$ You can bring the size down to 76 characters or so by using printf() to replace the for loop: printf("%*s\n",(int)(21+sin(i++/10.)*20),"|") \$\endgroup\$
    – treamur
    Commented Jan 16, 2014 at 21:28
  • 1
    \$\begingroup\$ Hmm... I would feel really guilty if I use this idea in my answer, especially when this is my own question... Would you consider posting an answer yourself? \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 21:36
  • 1
    \$\begingroup\$ You can shave off two more characters if you remove the j=0: i;main(j){for(;j++<21+sin(i/10.)*20;)putchar(32);puts("|");usleep(50000);i++;main(1);}. This relies on the assumption that the program is called with 0 arguments. \$\endgroup\$
    – Josh
    Commented Jan 16, 2014 at 22:43
  • \$\begingroup\$ Most conversations regarding interpreter/compiler flags have resolved with each flag used costing a byte, but the - before being free. \$\endgroup\$
    – primo
    Commented Jan 17, 2014 at 11:28
4
\$\begingroup\$

Ti-Basic, 33 bytes

While 1:Output(8,int(7sin(X)+8),"!":Disp "":π/30+X→X:End

The following caveats exist:

  1. Due to screen limitation of 16x8, this sine wave only has an amplitude of 7 (period of 60 is still maintained)

  2. Due to lack of an easy way to access the | char, ! is used instead

  3. Due to lack of an accurate system timer, the delay is not implemented. However, run speed appears approximately correct.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Heh, since TI-BASIC is counted in one-/two- byte tokens, this is actually 33 bytes (not "56 chars"), so it actually should have won the challenge! \$\endgroup\$
    – user39326
    Commented Jun 15, 2015 at 4:12
  • \$\begingroup\$ Except for the amplitude thing... \$\endgroup\$
    – lirtosiast
    Commented Jun 16, 2015 at 4:01
  • \$\begingroup\$ Well, yes, but going by bytes it's fine. \$\endgroup\$
    – user39326
    Commented Jun 16, 2015 at 4:46
3
\$\begingroup\$

fugly Javascript - 77

i=setInterval("console.log(Array(Math.sin(i+=.1)*20+20|0).join(' ')+'|')",50)

and if we do it in Firefox - 73

i=setInterval("console.log(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)

and if we're nasty - 67

i=setInterval("throw(' '.repeat(Math.sin(i+=.1)*20+20|0)+'|');",50)
\$\endgroup\$
3
\$\begingroup\$

R, 68 67 63 bytes

Thankfully, R has a builtin infinite repeat loop!

-1 byte from removing curly braces and replacing ; with |

-4 bytes golfed by pajonk

repeat cat(strrep(" ",20+20*sin(F<-F+.1)),"|\n")|Sys.sleep(.05)

Attempt This Online!

\$\endgroup\$
2
  • \$\begingroup\$ -4 bytes but with an additional leading space every line (the leftmost pipes in the example suggest it's acceptable). \$\endgroup\$
    – pajonk
    Commented Apr 11 at 10:18
  • \$\begingroup\$ @pajonk never heard of strrep - cool job! \$\endgroup\$
    – Patric
    Commented Apr 11 at 18:18
2
\$\begingroup\$

JavaScript - 88

setInterval(function(){console.log(Array(Math.sin(i++/10)*20+21|0).join(" ")+"|")},i=50)

I'm sure someone can come up with something that's actually clever.

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

J - 103,58,57,54

Thanks to awesome guys from IRC

(0.1&+[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)^:_]0

In words from right to left it reads: starting from 0 infinite times do: sin, add 1 ,multiply by 20, floor, append 1 (so it becomes array of 2 elements), copy two bytes ' |' correspondingly, print it, wait 0.05s and add 0.1

Instead of infinite loop we can use recursion, it would save 2 characters, but will also produce a stack error after some number of iterations

($:+&0.1[6!:3@]&0.05[2:1!:2~' |'#~1,~[:<.20*1+1&o.)0  

Where $: is a recursive call.

\$\endgroup\$
2
  • \$\begingroup\$ Would you mind adding a little explanation, so that people unfamiliar with the J syntax (like me) can also understand your answer? \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 8:09
  • \$\begingroup\$ It's possible to shorten this to 50 characters by fussing about with the train's structure: (+2*(%20)6!:3@[2:1!:2~' |'#~1,~[:<.20*1+1&o.)^:_]0. The recursion version only saves 1 char this time $:@(+2*(%20)6!:3@[2:1!:2~' |'#~1,~[:<.20*1+1&o.)0 though it appears to last longer before bottoming out. \$\endgroup\$ Commented Feb 15, 2014 at 8:28
2
\$\begingroup\$

Haskell - 75

main=putStr$concat["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]]

Unfortunately, I couldn't get the program to pause 50 ms without doubling my char count, so it just floods the console, but it does produce the sine wave.


Here's the full code with pausing (138 chars with newlines):

import GHC.Conc
import Control.Monad
main=mapM_(\a->putStr a>>threadDelay 50000)(["|\n"++take(floor$20+20*sin x)(repeat ' ')|x<-[0,0.1..]])
\$\endgroup\$
4
  • 2
    \$\begingroup\$ Pausing was one of the requirements. Can you also post the code with the pause? \$\endgroup\$
    – Justin
    Commented Jan 16, 2014 at 23:03
  • \$\begingroup\$ Okay, I posted it. I wish Haskell let you pause code without imports. \$\endgroup\$
    – Zaq
    Commented Jan 16, 2014 at 23:29
  • \$\begingroup\$ By amplitude I mean the peak amplitude, i.e. twice the amplitude of your current program. You may wish to change it to 20+20*sin x instead to qualify. \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 13:52
  • \$\begingroup\$ Oh, sure. I guess I misinterpreted that part of the question. \$\endgroup\$
    – Zaq
    Commented Jan 18, 2014 at 2:56
2
\$\begingroup\$

Perl 6: 46 chars

sleep .05*say ' 'x(25+20*.sin),'|'for 0,.1...*

Create an infinite lazy Range using 0,0.1 ... *, loop over that. say returns Bool::True which numifies as 1 in multiplication, this way I can keep it in a single statement.

\$\endgroup\$
4
  • \$\begingroup\$ I can see why sleep and .05 have to be separated. But I wonder if the space between say and ' ' is mandatory? \$\endgroup\$
    – Matthias
    Commented Jan 19, 2014 at 22:35
  • \$\begingroup\$ Yes :s It gives "2 terms in a row" error for say' ' One can use say(' ') but that's 1 char extra in this case... \$\endgroup\$
    – Ayiko
    Commented Jan 20, 2014 at 18:07
  • 1
    \$\begingroup\$ @Matthias: In Perl 6, listops either have to not take arguments, have a space after them, or use parenthesis. It's not a language designed for code golf, unlike Perl 5 (but it contains many nice builtin features, so it's usable). \$\endgroup\$
    – null
    Commented Jan 22, 2014 at 16:43
  • \$\begingroup\$ @xfix Thank you for the explanation. I like the language, but I did not look into it thoroughly yet, because I still cannot use it in a work project yet. However, I always plan to write some Perl 6 scripts. @ Ayiko, I appreciate your Perl 6 posts :-) \$\endgroup\$
    – Matthias
    Commented Jan 22, 2014 at 23:16
2
\$\begingroup\$

C#

The Magic Line [91] Characters

for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Thread.Sleep(50);

Working Program Below. [148] Characters

namespace System{class P{static void Main(){for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Threading.Thread.Sleep(50);}}}
\$\endgroup\$
12
  • \$\begingroup\$ Sorry if I haven't made the question clear - your program should not terminate and should continuously scroll down the wave (except until SIGINT). Also, please add a character count. \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 20:08
  • \$\begingroup\$ Sorry forgot about that bit. Fixed now. \$\endgroup\$ Commented Jan 16, 2014 at 20:24
  • \$\begingroup\$ I think you can lose "Thread.Sleep"s and change "float" with var :) 117 chars. -- Sorry didn't see the wait time.. 133 chars now. using System;class P{static void Main(){for(var i=0d;;Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|'))Thread.Sleep(50);}} \$\endgroup\$ Commented Jan 16, 2014 at 21:17
  • 1
    \$\begingroup\$ I can't get it to compile in VS2010 with Threading.Thread.Sleep(50) am I doing something wrong? \$\endgroup\$
    – Malachi
    Commented Jan 17, 2014 at 15:57
  • 1
    \$\begingroup\$ I was able to get it to run, but I had to add some Brackets and Semi-colons and it doesn't look the same every period \$\endgroup\$
    – Malachi
    Commented Jan 17, 2014 at 16:17
2
\$\begingroup\$

Bash+bc (to do the math), 80

$ for((;;i++)){ printf "%$(bc -l<<<"a=20*s($i/10);scale=0;a/1+20")s|
";sleep .05;}
                |
                 |
                   |
                     |
                       |
                         |
                           |
                            |
                              |
                               |
                                |
                                 |
                                  |
                                   |
                                   |
                                   |
                                   |
                                   |
                                   |
\$\endgroup\$
2
\$\begingroup\$

TI-BASIC, 30 bytes

Small size improvement over the other answer, at the cost of some accuracy. Note that TI-Basic technically has the | character, but you have to transfer it via computer or use an assembly program to access it.

While 1
Output(8,int(8+7sin(Ans)),":
Disp "
.03π+Ans
End
\$\endgroup\$
4
  • \$\begingroup\$ Woah, did not see how old this challenge was! Was going to try to golf this more (which is definitely possible) but it's really not worth it... \$\endgroup\$
    – user39326
    Commented Jun 15, 2015 at 4:11
  • \$\begingroup\$ By the way, .03π can be .1, which is still within the required interval. \$\endgroup\$
    – lirtosiast
    Commented Jun 16, 2015 at 3:59
  • \$\begingroup\$ Nice catch, thanks! Do you see any way I could golf the output command? Also, since I have a CSE, I could get this to the right amplitude (26-char screen) at the cost of a few bytes. \$\endgroup\$
    – user39326
    Commented Jun 16, 2015 at 4:48
  • \$\begingroup\$ No, the output command looks fine--too bad the Disp needs a quote. The amplitude should be 20 chars, actually, making the screen width requirement 39. So it would only work on the graph screen, and there's no short way of doing that. \$\endgroup\$
    – lirtosiast
    Commented Jun 16, 2015 at 5:22
2
\$\begingroup\$

Julia - 68

Edit: thanks to M L and ace.

i=0;while 0<1;println(" "^int(20sin(.1i)+20)*"|");i+=1;sleep(.05)end

Well, it can not compete vs APL, but here's my attempt.

Output:

                    |
                      |
                        |
                          |
                            |
                              |
                               |
                                 |
                                  |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                  |
                                |
                              |
                             |
                           |
                         |
                       |
                     |
                   |
                 |
               |
             |
           |
         |
        |
      |
     |
    |
   |
  |
 |
|
|
|
|
|
 |
 |
  |
   |
     |
      |
       |
         |
           |
             |
              |
                |
                  |
                    |
                      |
                        |
                          |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                   |
                                 |
                                |
                              |
                            |
                          |
                        |
                      |
                    |
                  |
                 |
               |
             |
           |
         |
       |
      |
     |
   |
  |
  |
 |
|
|
|
|
|
 |
  |
  |
    |
     |
      |
        |
         |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                            |
                              |
                                |
                                 |
                                   |
                                    |
                                     |
                                      |
                                       |
                                       |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                  |
                                 |
                               |
                              |
                            |
                          |
                        |
                      |
                    |
                  |
                |
              |
            |
           |
         |
       |
      |
    |
   |
  |
 |
 |
|
|
|
|
|
 |
  |
   |
    |
     |
      |
        |
          |
           |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                              |
                                |
                                  |
                                   |
                                    |
                                     |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                       |
                                      |
                                     |
                                    |
                                  |
                                 |
                               |
                             |
                            |
                          |
                        |
                      |
                    |
                  |
                |
              |
            |
          |
         |
       |
      |
    |
   |
  |
 |
 |
|
|
|
|
 |
 |
  |
   |
    |
     |
       |
        |
          |
            |
             |
               |
                 |
                   |
                     |
                       |
                         |
                           |
                             |
                               |
                                |
                                  |
                                   |
                                    |
                                      |
                                      |
                                       |
                                        |
                                        |
                                        |
                                        |
                                        |
                                       |
                                      |
                                      |
                                     |
                                   |
                                  |
                                |
                               |
                             |
                           |
                         |
                       |
                     |
                   |
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I don't know Julia, but is it possible to use .05 instead of 0.05 in sleep? \$\endgroup\$
    – user12205
    Commented Mar 25, 2014 at 3:13
  • \$\begingroup\$ Actually yes! Thanks \$\endgroup\$
    – CCP
    Commented Mar 25, 2014 at 19:44
  • \$\begingroup\$ Cut it down to 68 characters: i=0;while 0<1;println(" "^int(20sin(.1i)+20)*"|");i+=1;sleep(.05)end———sin(i/10)*20 is equal to 20sin(.1i) \$\endgroup\$
    – M L
    Commented Jun 20, 2015 at 2:56
  • \$\begingroup\$ I don't know Julia, but could you use a for loop iterating over all the natural numbers instead? \$\endgroup\$
    – lirtosiast
    Commented Jun 22, 2015 at 3:31
2
\$\begingroup\$

MATLAB, 81 bytes

t=0;while(fprintf('%s\n',i))i=[];t=t+1;i(fix(21+20*sind(t*6)))='|';pause(.05);end

I abused the fact that i is always initialized in MATLAB, which meant that I could put the fprintf in the while statement without initializing i first. This does mean the program first outputs an empty line, but I think this is not forbidden in the spec.

Furthermore, it abuses the fact that Matlab will ignore most ASCII control characters, printing a space instead of NULL (also for the first empty line).

\$\endgroup\$
1
  • \$\begingroup\$ "I abused the fact that i is always initialized in MATLAB, which meant that I could put the fprintf in the while statement without initializing i first." Really clever! +1! \$\endgroup\$ Commented Jan 20, 2016 at 21:20
2
\$\begingroup\$

05AB1E, 20 bytes

['|NT/Ž20*21+ú,50.W

Try it online.

Explanation:

[               # Start an infinite loop:
 '|            '#  Push character "|"
 N              #  Push the 0-based loop-index
  T/            #  Divide it by 10
    Ž          #  Take the sine of that
      20*       #  Multiply it by 20
         21+    #  Add 21
            ú   #  Pad that many leading spaces before the "|"
                #  (`ú` will ignore any decimal digits and interpret it as an integer)
             ,  #  Pop and print this string with trailing newline
 50.W           #  Sleep 50 ms
\$\endgroup\$
2
\$\begingroup\$

Japt, 20 bytes

Open your browser's console.

50i@Ol|ùMsU°/A Ñ0+21

Test it

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

Scala, 92,89,87

def f(i:Int){println(" "*(20+20*math.sin(i*.1)).toInt+"|");Thread sleep 50;f(i+1)};f(1)
\$\endgroup\$
2
  • \$\begingroup\$ (20+20*math.sin(i*.1)) reduces it by 1 char, assuming this is valid syntax (I have no experience with Scala) \$\endgroup\$
    – user12205
    Commented Jan 16, 2014 at 21:14
  • \$\begingroup\$ Thanks, but I have just discovered that myself :) \$\endgroup\$ Commented Jan 16, 2014 at 21:20
1
\$\begingroup\$

Python 3, 103

Stupid frikk'n imports...

import time,math
t=0
while 1:t+=(.05+t<time.clock())and(print(' '*int(20+20*math.cos(t*1.9))+'|')or.05)

Rather than "sleep", this implementation grinds at the cpu because python makes it easier to get a floating-point cpu clock than wall clock. This approach won't beat friol's, but it's fun so I'm leaving it up.

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

C#

[152] Characters

namespace System{class P{static void Main(){for(var i=0d;;){Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|');Threading.Thread.Sleep(50);}}}}

I could not get the Existing C# answer to Run and I couldn't downvote because I don't have enough Reputation

it was missing a couple of { and missing a ) after the For Loop Declaration.

I figure that the variance in the look of the wave when it is run is because of the way we are trying to display this wave.


if we aren't counting the Namespace and the Method Declaration then it would be [104] characters for the working version

for(var i=0d;;){Console.Write("{0,"+(int)(40+20*Math.Sin(i+=.1))+"}\n",'|');Threading.Thread.Sleep(50);}
\$\endgroup\$
2
  • \$\begingroup\$ The other C# answer works on gmcs. It fails to compile at first, but I think it is because there is some non-printable character in the source code. After typing it again on an empty file, the compilation is successful. \$\endgroup\$
    – user12205
    Commented Jan 17, 2014 at 16:38
  • \$\begingroup\$ Compilers can be picky, huh? \$\endgroup\$
    – Malachi
    Commented Jan 17, 2014 at 16:49
1
\$\begingroup\$

VB [236][178]

not sure how you would count the tabs, I just took the count from Notepadd++ before I pasted here. newlines are mandatory, probably why no one likes using it for code golfing.

Module Module1
Sub Main()
Dim i
While True
Console.WriteLine("{0:" & (40 + 20 * Math.Sin(i = i + 0.1)) & "}", "|")
Threading.Thread.Sleep(50)
End While
End Sub
End Module
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.