Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
1
Task/List-comprehensions/Python/list-comprehensions-1.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-1.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
[(x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2]
|
||||
1
Task/List-comprehensions/Python/list-comprehensions-2.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-2.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
((x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2)
|
||||
1
Task/List-comprehensions/Python/list-comprehensions-3.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-3.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
[(x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z]
|
||||
1
Task/List-comprehensions/Python/list-comprehensions-4.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-4.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
((x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z)
|
||||
5
Task/List-comprehensions/Python/list-comprehensions-5.py
Normal file
5
Task/List-comprehensions/Python/list-comprehensions-5.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def triplets(n):
|
||||
for x in xrange(1, n + 1):
|
||||
for y in xrange(x, n + 1):
|
||||
for z in xrange(y, n + 1):
|
||||
yield x, y, z
|
||||
1
Task/List-comprehensions/Python/list-comprehensions-6.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-6.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
[(x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2]
|
||||
1
Task/List-comprehensions/Python/list-comprehensions-7.py
Normal file
1
Task/List-comprehensions/Python/list-comprehensions-7.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
((x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2)
|
||||
59
Task/List-comprehensions/Python/list-comprehensions-8.py
Normal file
59
Task/List-comprehensions/Python/list-comprehensions-8.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
from functools import (reduce)
|
||||
from operator import (add)
|
||||
|
||||
|
||||
# pts :: Int -> [(Int, Int, Int)]
|
||||
def pts(n):
|
||||
m = 1 + n
|
||||
return [(x, y, z) for x in xrange(1, m)
|
||||
for y in xrange(x, m)
|
||||
for z in xrange(y, m) if x**2 + y**2 == z**2]
|
||||
|
||||
|
||||
# pts2 :: Int -> [(Int, Int, Int)]
|
||||
def pts2(n):
|
||||
m = 1 + n
|
||||
return bindList(
|
||||
xrange(1, m)
|
||||
)(lambda x: bindList(
|
||||
xrange(x, m)
|
||||
)(lambda y: bindList(
|
||||
xrange(y, m)
|
||||
)(lambda z: [(x, y, z)] if x**2 + y**2 == z**2 else [])))
|
||||
|
||||
|
||||
# pts3 :: Int -> [(Int, Int, Int)]
|
||||
def pts3(n):
|
||||
m = 1 + n
|
||||
return concatMap(
|
||||
lambda x: concatMap(
|
||||
lambda y: concatMap(
|
||||
lambda z: [(x, y, z)] if x**2 + y**2 == z**2 else []
|
||||
)(xrange(y, m))
|
||||
)(xrange(x, m))
|
||||
)(xrange(1, m))
|
||||
|
||||
|
||||
# GENERIC ---------------------------------------------------------
|
||||
|
||||
# concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
def concatMap(f):
|
||||
return lambda xs: (
|
||||
reduce(add, map(f, xs), [])
|
||||
)
|
||||
|
||||
|
||||
# (flip concatMap)
|
||||
# bindList :: [a] -> (a -> [b]) -> [b]
|
||||
def bindList(xs):
|
||||
return lambda f: (
|
||||
reduce(add, map(f, xs), [])
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
for f in [pts, pts2, pts3]:
|
||||
print (f(20))
|
||||
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue