# Print Triangle Wave of Numbers

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

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

# Dyalog APL, 43 40 bytes

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

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

• Haha, and I was so happy to have beaten APL by a considerable margin for once. :D Jan 29, 2015 at 22:27
• 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.
– ngn
Jan 29, 2015 at 22:40
• Oh, good catch, fixed! Jan 29, 2015 at 23:03
• You can golf it by 2 more: b⍴¨⍕¨b← can be rewritten as ⍴∘⍕¨⍨ I think. Great answer btw, I like it a lot! Jan 30, 2015 at 10:59
• That's very kind of you! I've just realised I can also shorten (n,1-n←2×⍵) to n(1-n←2×⍵).
– ngn
Jan 30, 2015 at 20:17

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


## APL (77)

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


## 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  • I'm doubtful as to whether that counts as taking arguments. – user475 Feb 16, 2011 at 11:44 ## Haskell (226225222220 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

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

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

# Canvas, 24 bytes

Ｈ（¹）＊！∔｝─Ｌ╷ ×；＋ ∔：↔∔⤢╶×↔


Try it here!