Calling J from Python
Nick Craig-Wood
nick at craig-wood.com
Sat Feb 10 05:30:11 EST 2007
George Sakkis <george.sakkis at gmail.com> wrote:
> On Feb 9, 9:20 pm, bearophileH... at lycos.com wrote:
> > This is a bit simpler, but probably there are simpler solutions using
> > modular arithmetic:
> >
> > l = [1]
> > for _ in range(15):
> > print ''.join(" *"[x] for x in l)
> > l = [1] + [l[i+1]^l[i] for i in range(len(l)-1)] + [1]
> >
> > Bye,
> > bearophile
>
> Here's another one, adapted from the example (in Java) in Wikipedia's
> entry (http://en.wikipedia.org/wiki/Sierpinski_triangle):
>
> N=15
> for x in xrange(N,0,-1):
> print ''.join('* '[x&y!=0] for y in xrange(N+1-x))
>
This is my solution after a few minutes thought.
It uses a different algorithm for generating the triangle. If python
could output binary numbers it would be more elegant...
>>> n = 1
>>> for i in range(16):
... print ("%X" % n).replace('0', ' ').replace('1', '*')
... n = n ^ (n << 4)
...
*
**
* *
****
* *
** **
* * * *
********
* *
** **
* * * *
**** ****
* * * *
** ** ** **
* * * * * * * *
****************
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list