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.

Given the amplitude and period for a wave, print the wave. See sample output for more details. The total number of wave forms equals the period, and the height of each wave equals the amplitude. Amplitude and Period are less than 10. You can ignore the trailing spaces but not the leading spaces.

Sample Input
3 2

Sample Output
  3           3    
 232         232  
12321 12321 12321 12321
       232         232
        3           3  
share|improve this question
1  
This looks more like triangles than sines. –  J B Feb 15 '11 at 16:25
    
I'm thinking this falls under the ascii-art tag. But the art part is not quite present, maybe there should be another tag for ascii graphics? –  Juan Feb 15 '11 at 16:30
    
I guess, you mean "number of periods" and not frequency. Frequency is (number of periods)/time, like RPM in cars. –  belisarius Feb 15 '11 at 17:10
    
@Juan, I think people searching for ascii-art questions probably wouldn't mind seeing this one included in the results –  gnibbler Feb 15 '11 at 20:08
    
Am I allowed to have leading whitespace in each line? Would save me three chars. –  FUZxxl Feb 15 '11 at 21:26
show 6 more comments

4 Answers

up vote 3 down vote accepted

J, 87 characters

As a program:

b=:]\@(]#~' '~:])(":@:>:@i.@-)
,.~^:(<:Y)(,.|.)@(' ',.~((<:({."1|."1)b),.b),' '$~2<:])X
        Y                                              X

runs like this:

,.~^:(<:2)(,.|.)@(' ',.~((<:({."1|."1)b),.b),' '$~2#<:) 3
  3           3         
 232         232        
12321 12321 12321 12321 
       232         232  
        3           3   
,.~^:(<:4)(,.|.)@(' ',.~((<:({."1|."1)b),.b),' '$~2#<:) 2
 2       2       2       2       2       2       2       2      
121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 121 
     2       2       2       2       2       2       2       2 

It's 5 more characters if we need it as a function F:

3 F 2
  3           3         
 232         232        
12321 12321 12321 12321 
       232         232  
        3           3   
share|improve this answer
    
I'm doubtful as to whether that counts as taking arguments. –  user475 Feb 16 '11 at 11:44
add comment

Python - 135 chars

A,F=map(int,raw_input().split());R=range
for y in R(-A+1,A):print"".join((" %s"%x)[-x<s*y<1]for s in(1,-1)for x in R(1,A)+R(A,-1,-1))*F

This version with a leading space is 132 chars

A,F=map(int,raw_input().split());R=range
for y in R(-A+1,A):print"".join((" %s"%x)[-x<s*y<1]for s in(1,-1)for x in R(A)+R(A,0,-1))*F

It also can be considerably shorter if not required to read from stdin or even if the input is comma separated

For comma separated input, the first line becomes

A,F=input();R=range
share|improve this answer
add comment

Haskell (226 225 222 220 214)

My try in Haskell:

import List
n!k|n>k=p:n!(k+1)++[p]|0<1=[p]where p=(n-1)?" "++k?show k++(n-k)?" ">>=id
f[n,k]=k?(n!1++(2*n-1)?' ':map reverse(n!1)++[(2*n-1)?' '])>>=id
main=interact$unlines.transpose.f.map read.words
(?)=replicate

Sorry guys, (€) is optimized away, it takes three bytes for one € as opposed to ! which only takes one byte each.
Here is a "beta Version", that doesn't satisfies the spec:

import List

-- Creates a single wave of numbers. k should be equal to 1
-- and is used for internal stuff,
wave n k|n==k=[peek]
        |otherwise = peek:wave n(k+1)++[peek] where
  peek=replicate(n-1)" "++replicate k(show k)++replicate(n-k)" ">>=id

-- Creates a full wave
-- k: number of waves, n: size of waves
fullWave[n,k]=unlines.transpose.concat.replicate k$wave n 1++map reverse(wave n 1)

main=interact$fullWave.map read.words
share|improve this answer
1  
The EUR operator! First time I encounter it :) –  J B Feb 15 '11 at 20:07
1  
I thought that € is discriminated way too much in programming languages. And because I was looking for an unused op, this came in very handy. –  FUZxxl Feb 15 '11 at 20:11
4  
What does it do? Is it 1.35 * the US operator? :) –  gnibbler Feb 15 '11 at 20:12
    
ideone.com/zBq0U –  FUZxxl Mar 6 '11 at 21:06
add comment

APL (77)

,/{×⍎⍵:⍵⋄' '}¨¨⊃∘↑∘⍕¨¨K⍴⊂(⊖M),⍨M←(2⍴N+N-1)↑(0 1↓M),⍨⌽M←(⌽⊖/¨M)×≥/¨M←⍳2⍴⊃N K←⎕
share|improve this answer
add comment

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.