Next highest int from digits: Difference between revisions

→‎Python: Generator: Tidied, updated primitives.
(→‎Python: Generator: Tidied, updated primitives.)
Line 1,286:
A variant which defines (in terms of a concatMap over permutations), a generator of '''all''' digit-shuffle successors for a given integer:
 
<lang python>'''Digit-shuffleNext successorshighest int from digits'''
 
from itertools import chain, islice, permutations, tee
 
 
# ------------ NON-FINITE STREAM OF SUCCESSORS -------------
 
# digitShuffleSuccessors :: Int -> [Int]
def digitShuffleSuccessors(n):
'''GeneratorNon-finite stream of all digit-shuffle
successors of n, where 0 <= n.
'''
Line 1,299 ⟶ 1,301:
delta = int(''.join(ds)) - n
return [] if 0 >= delta else [delta]
return map(
for x in sorted(set(concatMap(go)(
permutations(stradd(n)),
))): sorted(
yield n + x set(concatMap(go)(
permutations(str(n))
))
)
)
 
 
# TEST -------------------------- TEST --------------------------
# main :: IO ()
def main():
Line 1,315 ⟶ 1,321:
harvest = take(n)(ys)
return (
repr(len(harvest)) + ' of ' + repr(len(list(zs))) + ': '
repr(len(list(zs))) + ': '
)
).rjust(12, ' ') + repr(harvest)
return lambda xs: go(xs)
 
print(
Line 1,335 ⟶ 1,343:
 
 
# GENERIC ------------------------ GENERIC -------------------------
 
# add (+) :: Num a => a -> a -> a
def add(a):
'''Curried addition.'''
def go(b):
return a + b
return go
 
 
# concatMap :: (a -> [b]) -> [a] -> [b]
def concatMap(f):
'''The concatenation of a mapping.
'''A concatenated list or string over which a function f
The list monad can be derived by using a function f
has been mapped.
Thewhich listwraps monadits canoutput bein deriveda bylist, using an (a -> [b])empty
empty list to represent computational failure).
function which wraps its output in a list (using an
empty list to represent computational failure).
'''
def go(xs):
return lambda xs: return chain.from_iterable(map(f, xs))
return go
 
 
Line 1,351 ⟶ 1,368:
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
fx display function -> f -> xs -> tabular string.
'''
def gogox(xShow, fxShow, f, xs):
ysdef = [xShowgofx(xfxShow) for x in xs]:
w = max(map(len, ys) def gof(f):
return s + '\n' + '\n'.join(map def goxs(xs):
lambda x, y: y.rjust(w, ' ') + ' ->ys '= + fxShow(f[xShow(x)), for x in xs]
xs w = max(map(len, ys))
 
))
def arrowed(x, y):
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
return y.rjust(w, ' ') + (
xShow, fxShow, f, xs
' -> ' + fxShow(f(x))
)
)
return s + '\n' + '\n'.join(
map(arrowed, xs, ys)
)
return goxs
return gof
return gofx
return gox
 
 
Line 1,372 ⟶ 1,397:
or xs itself if n > length xs.
'''
return lambdadef go(xs): (
xs[0:n]return (
if isinstance(xs, (list, tuple)) xs[0:n]
else list(islice if isinstance(xs, n(list, tuple))
else list(islice(xs, n))
)
)
return go
 
 
9,659

edits