Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
55
Task/Square-but-not-cube/Python/square-but-not-cube.py
Normal file
55
Task/Square-but-not-cube/Python/square-but-not-cube.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# nonCubeSquares :: Int -> [(Int, Bool)]
|
||||
def nonCubeSquares(n):
|
||||
upto = enumFromTo(1)
|
||||
ns = upto(n)
|
||||
setCubes = set(x ** 3 for x in ns)
|
||||
ms = upto(n + len(set(x * x for x in ns).intersection(
|
||||
setCubes
|
||||
)))
|
||||
return list(tuple([x * x, x in setCubes]) for x in ms)
|
||||
|
||||
|
||||
# squareListing :: [(Int, Bool)] -> [String]
|
||||
def squareListing(xs):
|
||||
justifyIdx = justifyRight(len(str(1 + len(xs))))(' ')
|
||||
justifySqr = justifyRight(1 + len(str(xs[-1][0])))(' ')
|
||||
return list(
|
||||
'(' + str(1 + idx) + '^2 = ' + str(n) +
|
||||
' = ' + str(round(n ** (1 / 3))) + '^3)' if bln else (
|
||||
justifyIdx(1 + idx) + ' ->' +
|
||||
justifySqr(n)
|
||||
)
|
||||
for idx, (n, bln) in enumerate(xs)
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
print(
|
||||
unlines(
|
||||
squareListing(
|
||||
nonCubeSquares(30)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# GENERIC ------------------------------------------------------------------
|
||||
|
||||
# enumFromTo :: Int -> Int -> [Int]
|
||||
def enumFromTo(m):
|
||||
return lambda n: list(range(m, 1 + n))
|
||||
|
||||
|
||||
# justifyRight :: Int -> Char -> String -> String
|
||||
def justifyRight(n):
|
||||
return lambda cFiller: lambda a: (
|
||||
((n * cFiller) + str(a))[-n:]
|
||||
)
|
||||
|
||||
|
||||
# unlines :: [String] -> String
|
||||
def unlines(xs):
|
||||
return '\n'.join(xs)
|
||||
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue