Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
1  
Peak amplitude, peak-to-peak amplitude, or root-square amplitude? –  David Carraher Jan 16 at 20:31
    
Peak amplitude. –  ace Jan 16 at 20:38
    
@David The Ecuador makes it pretty obvious. :-P –  Doorknob Jan 16 at 20:38
1  
Is it ok to draw a wave with just |s and no spaces? –  Gelatin Jan 17 at 1:23
1  
@Max Ried fine, I will change it to "should go on forever unless otherwise interrupted". –  ace Apr 4 at 10:28

31 Answers 31

up vote 6 down vote accepted

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.
share|improve this answer
    
Darn... I thought trigonometric functions plus the time delay would leave you guys out –  ace Jan 17 at 20:22
2  
Here a 33 char one: {⎕←'|'↑⍨-⌈20×1+1○⍵⋄∇.1+⍵⊣⎕DL.05}0 –  Tobia Jan 18 at 0:03
3  
@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) –  Tobia Jan 18 at 0:16

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

share|improve this answer
1  
This was inspired by @ace's own C answer. –  treamur Jan 16 at 21:41
2  
I think you could use 5E4 instead of 50000. –  musiphil Jan 17 at 0:00
2  
I think you could use *.1 instead of /10. –  moala Jan 17 at 1:58
1  
@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. –  treamur Jan 17 at 5:32
2  
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. –  Art Jan 17 at 10:17

Mathematica 121 104 80 67 64

n=1;While[0<1,Spacer[70 Sin[n Pi/32]+70]~Print~"|";Pause@.05; n++]

sine

share|improve this answer
    
question says not to use mathematica to plot it. is this different than that somehow? –  Malachi Jan 16 at 23:37
11  
@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. –  Quincunx Jan 16 at 23:49
    
ok I get what you are saying thank you for the clarification –  Malachi Jan 17 at 0:41
    
Clever rule-bending :) –  mebob Jan 17 at 5:17
1  
And here is a 58 char version Do[Spacer[70*Sin[n*Pi/32]+70]~Print~"|";Pause@.05,{n,18!}] –  Ajasja Jan 17 at 10:22

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.

share|improve this answer
    
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.) –  ace Jan 16 at 21:42
    
Thanks ace for your help. –  KevinColyer Jan 16 at 22:27
    
I guess if I changed the increment from .105 to .1 I would beat ruby at 56 chars! –  KevinColyer Jan 16 at 22:50
    
@primo - my shell sleep does do times shorter than 1 second... –  KevinColyer Jan 17 at 8:32
2  
@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. –  ace Jan 17 at 8:58

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
share|improve this answer
    
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). –  Quincunx Jan 21 at 4:01
    
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. –  Malachi Feb 10 at 15:13

Ruby 56

i=0
loop{puts" "*(20*Math.sin(i+=0.1)+20)+?|;sleep 0.05}
share|improve this answer
    
Is replacing puts with p allowed? –  Slicedpan Jan 17 at 10:12
1  
@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". –  daniero Jan 17 at 15:37

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

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

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.

share|improve this answer
    
Sorry if I haven't made the question clear - your program should not terminate and should continuously scroll down the wave (except until SIGINT) –  ace Jan 16 at 19:26
1  
a=0. -> a=0 gets you to 88 –  boothby Jan 16 at 23:03

PHP, 59 characters

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

TI-BASIC, 37

Executed from a TI-84 calculator

:While 1:DrawInv 2.9sin(X:ClrDraw:End
  • Peak Amplitude = 20
  • Period = 62
  • Pause between lines 50-60 ms (very close as there is no built-in clock)
share|improve this answer
3  
OP: this is an ascii-art problem –  MrZander Jan 17 at 0:13
1  
almost all of the programming language answers use a predefined function for Sine, so I don't see how this would be against the rules. as this uses the predefined function for the language it is written in just the same as say Javascript –  Malachi Jan 17 at 0:44
1  
The problem @MrZander is trying to bring up here is that you're not doing this with ASCII characters, but by drawing a graph. That's against the rules. –  joeytje50 Jan 17 at 1:14
3  
No, DrawInv is a graphing feature of the TI-84, so this is just like using Mathematica to plot a sine wave. tibasicdev.wikidot.com/drawinv Also, all TI-84s have RTCs. –  Kevin Chen Jan 17 at 4:56
4  
I agree with @Kevin Chen that this is essentially just like using Mathematica to plot a sine wave. So, I still do not consider this to be a valid answer. Also, according to the picture in the link given by Kevin, I do not consider this to be ascii art. I think this is just some low resolution bitmap, so this answer does not qualify. –  ace Jan 17 at 16:42

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

share|improve this answer
1  
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();} –  shiona Jan 16 at 18:24
    
It works. Thanks. –  ace Jan 16 at 18:32
    
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),"|") –  treamur Jan 16 at 21:28
1  
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? –  ace Jan 16 at 21:36
1  
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++;ma‌​in(1);}. This relies on the assumption that the program is called with 0 arguments. –  Josh Jan 16 at 22:43

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.

share|improve this answer

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.

share|improve this answer

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.

share|improve this answer
    
You never initialize i, so it prints a straight line instead of a wave. –  gilly3 Jan 16 at 22:37
    
My mistake... It probably worked because I had ran Kendall's script already in my console, so i was initialised already for me. –  joeytje50 Jan 16 at 22:41

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.

share|improve this answer
    
Would you mind adding a little explanation, so that people unfamiliar with the J syntax (like me) can also understand your answer? –  ace Jan 17 at 8:09
    
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. –  algorithmshark Feb 15 at 8:28

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..]])
share|improve this answer
2  
Pausing was one of the requirements. Can you also post the code with the pause? –  Quincunx Jan 16 at 23:03
    
Okay, I posted it. I wish Haskell let you pause code without imports. –  Zaq Jan 16 at 23:29
    
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. –  ace Jan 17 at 13:52
    
Oh, sure. I guess I misinterpreted that part of the question. –  Zaq Jan 18 at 2:56

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.

share|improve this answer
    
I can see why sleep and .05 have to be separated. But I wonder if the space between say and ' ' is mandatory? –  Matthias Jan 19 at 22:35
    
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... –  Ayiko Jan 20 at 18:07
1  
@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). –  xfix Jan 22 at 16:43
    
@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 :-) –  Matthias Jan 22 at 23:16

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)
share|improve this answer

Scala, 92,89,87

def f(i:Int){println(" "*(20+20*math.sin(i*.1)).toInt+"|");Thread sleep 50;f(i+1)};f(1)
share|improve this answer
    
(20+20*math.sin(i*.1)) reduces it by 1 char, assuming this is valid syntax (I have no experience with Scala) –  ace Jan 16 at 21:14
    
Thanks, but I have just discovered that myself :) –  ValarDohaeris Jan 16 at 21:20

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.

share|improve this answer

GTB, 14

Executed from a TI-84 calculator

[i;2.9sin(Xc;]

Amplitude = 20, Period = 62, Pause = ~50

share|improve this answer

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);}
share|improve this answer
    
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. –  ace Jan 17 at 16:38
    
Compilers can be picky, huh? –  Malachi Jan 17 at 16:49

Ti-Basic, 56 Chars

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

The following caveat exists:

  1. Due to lack of a system timer, the delay is not implemented exactly correct. However, run speed appears approximately correct (50-70).
share|improve this answer

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
share|improve this answer

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);}}}
share|improve this answer
    
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. –  ace Jan 16 at 20:08
    
Sorry forgot about that bit. Fixed now. –  John ClearZ Jan 16 at 20:24
    
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(5‌​0);}} –  Muhammed Medeni Baykal Jan 16 at 21:17
1  
I can't get it to compile in VS2010 with Threading.Thread.Sleep(50) am I doing something wrong? –  Malachi Jan 17 at 15:57
1  
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 –  Malachi Jan 17 at 16:17

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;}
                |
                 |
                   |
                     |
                       |
                         |
                           |
                            |
                              |
                               |
                                |
                                 |
                                  |
                                   |
                                   |
                                   |
                                   |
                                   |
                                   |
share|improve this answer

F# - 90 79 77 76

Here's a solution using recursion

let rec f x=printfn"%*c"(int(20.*sin x)+21)'|';Thread.Sleep 50;f(x+0.1)
f 0.

It could probably be improved further.

share|improve this answer
    
Without knowing anything about F#, I'm assuming that Thread.Sleep expects a value in ms, so you can get rid of one of the 0's and do Thread.Sleep 50. :) –  ValarDohaeris Jan 17 at 0:49
    
@ValarDohaeris You're right. I misread the requirements. –  p.s.w.g Jan 17 at 0:54

AutoHotkey 176

SetKeyDelay,-1
run Notepad.exe
WinWaitActive, ahk_class Notepad
p:=0
loop
{
sleep 50
p+=Mod(Floor(A_index/40),2)?-1:1,t:=""
loop % p
t .= " "
sendinput % t "|`n"
}
esc::Exitapp

Run the script . It opens Notepad and prints the characters. Press Esc anytime to exit.

share|improve this answer

Clojure, 121

Short version:

(loop[a 0](println(clojure.string/join(repeat(int(+ 20 (* 20 (Math/sin a)))) " ")) \|)(Thread/sleep 50)(recur(+ a 0.1)))

Pretty version:

(loop [a 0]
  (println (clojure.string/join (repeat (int (+ 20 (* 20 (Math/sin a)))) " ")) \|)    
  (Thread/sleep 50)
  (recur(+ a 0.1)))

Period is 64.

Type this into lein repl or save in file sin.clj and run with lein exec sin.clj (requires lein-exec plugin).

share|improve this answer

Groovy 87 Chars

Okay, now for a serious answer that actually follows the guidelines

def x=0;while(true){println ' '*(20*Math.sin(x)+20)+'|';x+=0.1;Thread.sleep(50)}
share|improve this answer
2  
Do you think a suitable decimal approximation for Math.PI/30 can save you a few chars? –  ace Jan 17 at 16:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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