Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
8
Task/Sierpinski-triangle/Python/sierpinski-triangle-1.py
Normal file
8
Task/Sierpinski-triangle/Python/sierpinski-triangle-1.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
def sierpinski(n):
|
||||
d = ["*"]
|
||||
for i in xrange(n):
|
||||
sp = " " * (2 ** i)
|
||||
d = [sp+x+sp for x in d] + [x+" "+x for x in d]
|
||||
return d
|
||||
|
||||
print "\n".join(sierpinski(4))
|
||||
11
Task/Sierpinski-triangle/Python/sierpinski-triangle-2.py
Normal file
11
Task/Sierpinski-triangle/Python/sierpinski-triangle-2.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import functools
|
||||
|
||||
def sierpinski(n):
|
||||
|
||||
def aggregate(TRIANGLE, I):
|
||||
SPACE = " " * (2 ** I)
|
||||
return [SPACE+X+SPACE for X in TRIANGLE] + [X+" "+X for X in TRIANGLE]
|
||||
|
||||
return functools.reduce(aggregate, range(n), ["*"])
|
||||
|
||||
print("\n".join(sierpinski(4)))
|
||||
31
Task/Sierpinski-triangle/Python/sierpinski-triangle-3.py
Normal file
31
Task/Sierpinski-triangle/Python/sierpinski-triangle-3.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
'''Sierpinski triangle'''
|
||||
|
||||
from functools import reduce
|
||||
from operator import add
|
||||
|
||||
|
||||
# sierpinski :: Int -> String
|
||||
def sierpinski(n):
|
||||
'''Nth iteration of a Sierpinksi triangle.'''
|
||||
def go(xs, i):
|
||||
s = ' ' * (2 ** i)
|
||||
return concatMap(lambda x: [s + x + s])(xs) + (
|
||||
concatMap(lambda x: [x + ' ' + x])(xs)
|
||||
)
|
||||
return '\n'.join(reduce(go, range(n), '*'))
|
||||
|
||||
|
||||
# concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
def concatMap(f):
|
||||
'''A concatenated list or string over which a function f
|
||||
has been mapped.
|
||||
The list monad can be derived by using an (a -> [b])
|
||||
function which wraps its output in a list (using an
|
||||
empty list to represent computational failure).
|
||||
'''
|
||||
return lambda xs: (
|
||||
reduce(add, map(f, xs), [])
|
||||
)
|
||||
|
||||
|
||||
print(sierpinski(4))
|
||||
4
Task/Sierpinski-triangle/Python/sierpinski-triangle-4.py
Normal file
4
Task/Sierpinski-triangle/Python/sierpinski-triangle-4.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
x = 1
|
||||
while True:
|
||||
print(bin(x)[2:].replace('0', ' '))
|
||||
x ^= x<<1
|
||||
Loading…
Add table
Add a link
Reference in a new issue