15
\$\begingroup\$

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  
\$\endgroup\$
12
  • 2
    \$\begingroup\$ This looks more like triangles than sines. \$\endgroup\$
    – J B
    Feb 15, 2011 at 16:25
  • \$\begingroup\$ 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? \$\endgroup\$
    – Juan
    Feb 15, 2011 at 16:30
  • \$\begingroup\$ I guess, you mean "number of periods" and not frequency. Frequency is (number of periods)/time, like RPM in cars. \$\endgroup\$ Feb 15, 2011 at 17:10
  • \$\begingroup\$ @Juan, I think people searching for ascii-art questions probably wouldn't mind seeing this one included in the results \$\endgroup\$
    – gnibbler
    Feb 15, 2011 at 20:08
  • \$\begingroup\$ Am I allowed to have leading whitespace in each line? Would save me three chars. \$\endgroup\$
    – FUZxxl
    Feb 15, 2011 at 21:26

7 Answers 7

5
\$\begingroup\$

Dyalog APL, 43 40 bytes

{⍉⊃⍪/⍺⍴⊂(⌽⍪⊢)(n,1-n←2×⍵)↑↑b⍴¨⍕¨b←a,1↓⌽a←⍳⍵}

{⍉⊃⍪/⍺⍴⊂(⌽⍪⊢)n(1-n←2×⍵)↑↑⍴∘⍕¨⍨a,1↓⌽a←⍳⍵} (Thanks, Moris Zucca)

This is a dyadic function with the amplitude as the right argument () and the period as the left argument (). A program that reads user input would take the same number of characters.

Drawing some inspiration from Martin Büttner's CJam answer:

{⍉⊃⍪/⍺⍴⊂(⌽⍪⊢)n(1-n←2×⍵)↑↑⍴∘⍕¨⍨a,1↓⌽a←⍳⍵}
                                   a←⍳⍵ ⍝ numbers 1 2 3, call them "a"
                                  ⌽     ⍝ reverse them: 3 2 1
                                1↓      ⍝ drop one: 2 1
                              a,        ⍝ prepend "a": 1 2 3 2 1
                         ⍴∘⍕¨⍨          ⍝ format a[i] and repeat it a[i] times:
                                        ⍝     (,'1') '22' '333' '22' (,'1')
                        ↑               ⍝ mix, i.e. obtain a character matrix:
                                        ⍝    ┌───┐
                                        ⍝    │1  │
                                        ⍝    │22 │
                                        ⍝    │333│
                                        ⍝    │22 │
                                        ⍝    │1  │
                                        ⍝    └───┘
             n(1-n←2×⍵)↑                ⍝ take a 2×⍵ by 1-2×⍵ matrix
                                        ⍝ (negative length extends backwards):
                                        ⍝    ┌─────┐
                                        ⍝    │  1  │
                                        ⍝    │  22 │
                                        ⍝    │  333│
                                        ⍝    │  22 │
                                        ⍝    │  1  │
                                        ⍝    │     │
                                        ⍝    └─────┘
        (⌽⍪⊢)                           ⍝ the reverse of it, vertically joined with it
                                        ⍝    ┌─────┐
                                        ⍝    │  1  │
                                        ⍝    │ 22  │
                                        ⍝    │333  │
                                        ⍝    │ 22  │
                                        ⍝    │  1  │
                                        ⍝    │     │
                                        ⍝    │  1  │
                                        ⍝    │  22 │
                                        ⍝    │  333│
                                        ⍝    │  22 │
                                        ⍝    │  1  │
                                        ⍝    │     │
                                        ⍝    └─────┘
     ⍺⍴⊂                                ⍝ take ⍺ copies
  ⊃⍪/                                   ⍝ join them vertically
 ⍉                                      ⍝ transpose
\$\endgroup\$
5
  • \$\begingroup\$ Haha, and I was so happy to have beaten APL by a considerable margin for once. :D \$\endgroup\$ Jan 29, 2015 at 22:27
  • \$\begingroup\$ I wouldn't have tried if you hadn't :) By the way, it looks like your answer as well as the other APL answer are producing wrong output. According to the sample, triangles should meet at the central line. \$\endgroup\$
    – ngn
    Jan 29, 2015 at 22:40
  • \$\begingroup\$ Oh, good catch, fixed! \$\endgroup\$ Jan 29, 2015 at 23:03
  • 1
    \$\begingroup\$ You can golf it by 2 more: b⍴¨⍕¨b← can be rewritten as ⍴∘⍕¨⍨ I think. Great answer btw, I like it a lot! \$\endgroup\$ Jan 30, 2015 at 10:59
  • \$\begingroup\$ That's very kind of you! I've just realised I can also shorten (n,1-n←2×⍵) to n(1-n←2×⍵). \$\endgroup\$
    – ngn
    Jan 30, 2015 at 20:17
4
\$\begingroup\$

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
\$\endgroup\$
4
\$\begingroup\$

APL (77)

,/{×⍎⍵:⍵⋄' '}¨¨⊃∘↑∘⍕¨¨K⍴⊂(⊖M),⍨M←(2⍴N+N-1)↑(0 1↓M),⍨⌽M←(⌽⊖/¨M)×≥/¨M←⍳2⍴⊃N K←⎕
\$\endgroup\$
3
\$\begingroup\$

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   
\$\endgroup\$
1
  • \$\begingroup\$ I'm doubtful as to whether that counts as taking arguments. \$\endgroup\$
    – user475
    Feb 16, 2011 at 11:44
2
\$\begingroup\$

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
\$\endgroup\$
4
  • 1
    \$\begingroup\$ The EUR operator! First time I encounter it :) \$\endgroup\$
    – J B
    Feb 15, 2011 at 20:07
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – FUZxxl
    Feb 15, 2011 at 20:11
  • 4
    \$\begingroup\$ What does it do? Is it 1.35 * the US operator? :) \$\endgroup\$
    – gnibbler
    Feb 15, 2011 at 20:12
  • \$\begingroup\$ ideone.com/zBq0U \$\endgroup\$
    – FUZxxl
    Mar 6, 2011 at 21:06
1
\$\begingroup\$

CJam, 45 bytes

CJam is a lot younger than this challenge, so this answer is not eligible for the green checkmark (which should btw be updated to marinus's APL answer). This was still a fun little exercise though.

r~:I2*,{)IS*I@I\-z-_a*+I~)>I(S*+}%_Wf%+r~*zN*

Test it here.

The idea is to generate half a period vertically, like so:

  1  
 22  
333  
 22  
  1  

(Plus the next empty row which SE swallows). This then duplicated, each row is reversed, and the second half of the period is appended. Then the entire thing is repeated by the number of periods, and ultimately the grid is transposed to orientate the wave along the horizontal.

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

Canvas, 24 bytes

H(¹)*!∔}─L╷ ×;+ ∔:↔∔⤢╶×↔

Try it here!

\$\endgroup\$

Your Answer

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

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