September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -15,4 +15,4 @@ predicates = [
|
|||
|
||||
for sol in permutations(Names.seq):
|
||||
if all(p(sol) for p in predicates):
|
||||
print " ".join(Names.strings[s] for s in sol)
|
||||
print(" ".join(x for x, y in sorted(zip(Names.strings, sol), key=lambda x: x[1])))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
'''Dinesman's multiple-dwelling problem'''
|
||||
|
||||
from itertools import permutations
|
||||
|
||||
print([
|
||||
(
|
||||
'Baker on ' + str(b),
|
||||
'Cooper on ' + str(c),
|
||||
'Fletcher on ' + str(f),
|
||||
'Miller on ' + str(m),
|
||||
'Smith on ' + str(s)
|
||||
) for [b, c, f, m, s] in permutations(range(1, 6))
|
||||
if all([
|
||||
5 != b,
|
||||
1 != c,
|
||||
1 != f,
|
||||
5 != f,
|
||||
c < m,
|
||||
1 < abs(s - f),
|
||||
1 < abs(c - f)
|
||||
])
|
||||
])
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
'''Dinesman's multiple-dwelling problem'''
|
||||
|
||||
from itertools import chain, permutations
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Solution or null result.'''
|
||||
print(report(
|
||||
concatMap(dinesman)(
|
||||
permutations(range(1, 6))
|
||||
)
|
||||
))
|
||||
|
||||
|
||||
# dinesman :: (Int, Int, Int, Int, Int) -> [(Int, Int, Int, Int, Int)]
|
||||
def dinesman(bcfms):
|
||||
'''A list containing the given permutation of five
|
||||
integers if it matches all the dinesman conditions,
|
||||
or an empty list if it does not.
|
||||
'''
|
||||
[b, c, f, m, s] = bcfms
|
||||
return [bcfms] if all([
|
||||
5 != b,
|
||||
1 != c,
|
||||
1 != f,
|
||||
5 != f,
|
||||
c < m,
|
||||
1 < abs(s - f),
|
||||
1 < abs(c - f)
|
||||
]) else []
|
||||
|
||||
|
||||
# report :: [(Int, Int, Int, Int, Int)] -> String
|
||||
def report(xs):
|
||||
'''A message summarizing the first (if any) solution found.
|
||||
'''
|
||||
return ', '.join(list(map(
|
||||
lambda k, n: k + ' in ' + str(n),
|
||||
['Baker', 'Cooper', 'Fletcher', 'Miller', 'Smith'],
|
||||
xs[0]
|
||||
))) + '.' if xs else 'No solution found.'
|
||||
|
||||
|
||||
# GENERAL -------------------------------------------------
|
||||
|
||||
# concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
def concatMap(f):
|
||||
'''A 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))
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue