September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -1,3 +1,3 @@
|
|||
def floyd(rowcount=5):
|
||||
return [list(range(i*(i-1)//2+1, i*(i+1)//2+1))
|
||||
for i in range(1, rowcount+1)]
|
||||
return [list(range(i * (i - 1) // 2 + 1, i * (i + 1) // 2 + 1))
|
||||
for i in range(1, rowcount + 1)]
|
||||
|
|
|
|||
72
Task/Floyds-triangle/Python/floyds-triangle-3.py
Normal file
72
Task/Floyds-triangle/Python/floyds-triangle-3.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
'''Floyd triangle in terms of concatMap'''
|
||||
|
||||
from itertools import chain
|
||||
|
||||
|
||||
# floyd :: Int -> [[Int]]
|
||||
def floyd(n):
|
||||
'''n rows of a Floyd triangle.'''
|
||||
def f(i):
|
||||
return [
|
||||
enumFromTo(i * pred(i) // 2 + 1)(
|
||||
i * succ(i) // 2
|
||||
)
|
||||
]
|
||||
return concatMap(f)(enumFromTo(1)(n))
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test'''
|
||||
print(unlines(
|
||||
map(str, floyd(5))
|
||||
))
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
|
||||
# enumFromTo :: (Int, Int) -> [Int]
|
||||
def enumFromTo(m):
|
||||
'''Integer enumeration from m to n.'''
|
||||
return lambda n: list(range(m, 1 + n))
|
||||
|
||||
|
||||
# concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
def concatMap(f):
|
||||
'''Concatenated list over which a function has been mapped.
|
||||
The list monad can be derived by using a function f which
|
||||
wraps its output in a list,
|
||||
(using an empty list to represent computational failure).'''
|
||||
return lambda xs: list(
|
||||
chain.from_iterable(
|
||||
map(f, xs)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# pred :: Enum a => a -> a
|
||||
def pred(x):
|
||||
'''The predecessor of a value. For numeric types, (- 1).'''
|
||||
return x - 1 if isinstance(x, int) else (
|
||||
chr(ord(x) - 1)
|
||||
)
|
||||
|
||||
|
||||
# succ :: Enum a => a -> a
|
||||
def succ(x):
|
||||
'''The successor of a value. For numeric types, (1 +).'''
|
||||
return 1 + x if isinstance(x, int) else (
|
||||
chr(1 + ord(x))
|
||||
)
|
||||
|
||||
|
||||
# unlines :: [String] -> String
|
||||
def unlines(xs):
|
||||
'''A single string derived by the intercalation
|
||||
of a list of strings with the newline character.'''
|
||||
return '\n'.join(xs)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
94
Task/Floyds-triangle/Python/floyds-triangle-4.py
Normal file
94
Task/Floyds-triangle/Python/floyds-triangle-4.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
'''Floyd triangle in terms of iterate(f)(x)'''
|
||||
|
||||
from itertools import islice
|
||||
|
||||
|
||||
# floyd :: Int -> [[Int]]
|
||||
def floyd(n):
|
||||
'''n rows of a Floyd triangle.'''
|
||||
return take(n)(iterate(nextFloyd)([1]))
|
||||
|
||||
|
||||
# nextFloyd :: [Int] -> [Int]
|
||||
def nextFloyd(xs):
|
||||
'''A Floyd triangle row derived from
|
||||
the preceding row.'''
|
||||
n = succ(len(xs))
|
||||
return [1] if n < 2 else (
|
||||
enumFromTo(succ(n * pred(n) // 2))(
|
||||
n * succ(n) // 2
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# showFloyd :: [[Int]] -> String
|
||||
def showFloyd(xs):
|
||||
'''A stringification of Floyd triangle rows.'''
|
||||
return unlines(str(x) for x in xs)
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test'''
|
||||
print(showFloyd(
|
||||
floyd(5)
|
||||
))
|
||||
|
||||
|
||||
# GENERIC ABSTRACTIONS ------------------------------------
|
||||
|
||||
# enumFromTo :: (Int, Int) -> [Int]
|
||||
def enumFromTo(m):
|
||||
'''Integer enumeration from m to n.'''
|
||||
return lambda n: list(range(m, 1 + n))
|
||||
|
||||
|
||||
# iterate :: (a -> a) -> a -> Gen [a]
|
||||
def iterate(f):
|
||||
'''An infinite list of repeated applications of f to x.'''
|
||||
def go(x):
|
||||
v = x
|
||||
while True:
|
||||
yield v
|
||||
v = f(v)
|
||||
return lambda x: go(x)
|
||||
|
||||
|
||||
# pred :: Enum a => a -> a
|
||||
def pred(x):
|
||||
'''The predecessor of a value. For numeric types, (- 1).'''
|
||||
return x - 1 if isinstance(x, int) else (
|
||||
chr(ord(x) - 1)
|
||||
)
|
||||
|
||||
|
||||
# succ :: Enum a => a -> a
|
||||
def succ(x):
|
||||
'''The successor of a value. For numeric types, (1 +).'''
|
||||
return 1 + x if isinstance(x, int) else (
|
||||
chr(1 + ord(x))
|
||||
)
|
||||
|
||||
|
||||
# take :: Int -> [a] -> [a]
|
||||
# take :: Int -> String -> String
|
||||
def take(n):
|
||||
'''The prefix of xs of length n,
|
||||
or xs itself if n > length xs.'''
|
||||
return lambda xs: (
|
||||
xs[0:n]
|
||||
if isinstance(xs, list)
|
||||
else list(islice(xs, n))
|
||||
)
|
||||
|
||||
|
||||
# unlines :: [String] -> String
|
||||
def unlines(xs):
|
||||
'''A single string derived by the intercalation
|
||||
of a list of strings with the newline character.'''
|
||||
return '\n'.join(xs)
|
||||
|
||||
|
||||
# MAIN ----------------------------------------------------
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue