Ascending Pea Pattern generator
APL, 32 characters
⍪⌽({⍵,⍨,/{⌽⍵,+/⍵⍷d}¨⍳⌈/d←⊃⍵}⍣⎕)1
This generates lines starting from 0 (i.e. 0
generates 1
, 1
generates 1
followed by 1 1
, etc.), as specified by user input. I used Dyalog APL for this, and ⎕IO
should be set to its default of 1.
Example:
⍪⌽({⍵,⍨,/{⌽⍵,+/⍵⍷d}¨⍳⌈/d←⊃⍵}⍣⎕)1
⎕:
0
1
⍪⌽({⍵,⍨,/{⌽⍵,+/⍵⍷d}¨⍳⌈/d←⊃⍵}⍣⎕)1
⎕:
13
1
1 1
2 1
1 1 1 2
3 1 1 2
2 1 1 2 1 3
3 1 2 2 1 3
2 1 2 2 2 3
1 1 4 2 1 3
3 1 1 2 1 3 1 4
4 1 1 2 2 3 1 4
3 1 2 2 1 3 2 4
2 1 3 2 2 3 1 4
2 1 3 2 2 3 1 4
Once I get some more time, I'll write up an explanation. ⍨
Python (2.x), 81 80 characters
l='1'
exec"print l;l=''.join(`l.count(k)`+k for k in sorted(set(l)))\n"*input()
All tips or comments welcome!
usage: python peapattern.py 15 # enter the number of iterations 1 11 21 1112 3112 211213 312213 212223 114213 31121314 41122314 31221324 21322314 21322314 21322314
J, 60 46 39 26 characters
1([:,(#,{.)/.~@/:~)@[&0~i.
Edit 3: Came up with a much nicer way of expressing this.
1([:;[:|."1[:/:~~.,.[:+/"1[:~.=)@[&0~i.
Edit 2: Finally found a way to move the argument to the end of the sequence and get rid of the unnecessary assignment stuff.
Previously:
p=.3 :'([:,[:|."1[:/:~~.,.[:+/"1[:~.=)^:(i.y)1
Edit 1: Fixes the output which should be y
rows rather than the y
th row. Also shortens things a bit. Shame about the 0
s, can't seem to get rid of the damn things.
Usage:
1([:,(#,{.)/.~@/:~)@[&0~i. 1
1
1([:,(#,{.)/.~@/:~)@[&0~i. 6
1 0 0 0 0 0
1 1 0 0 0 0
2 1 0 0 0 0
1 1 1 2 0 0
3 1 1 2 0 0
2 1 1 2 1 3
1([:,(#,{.)/.~@/:~)@[&0~i. 10
1 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0
2 1 0 0 0 0 0 0
1 1 1 2 0 0 0 0
3 1 1 2 0 0 0 0
2 1 1 2 1 3 0 0
3 1 2 2 1 3 0 0
2 1 2 2 2 3 0 0
1 1 4 2 1 3 0 0
3 1 1 2 1 3 1 4
Admittedly the usage is uglier now, but prettiness isn't the name of the game here...