Print Triangle Wave of Numbers
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
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←⎕