Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Range-consolidation/Python/range-consolidation-1.py
Normal file
22
Task/Range-consolidation/Python/range-consolidation-1.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def normalize(s):
|
||||
return sorted(sorted(bounds) for bounds in s if bounds)
|
||||
|
||||
def consolidate(ranges):
|
||||
norm = normalize(ranges)
|
||||
for i, r1 in enumerate(norm):
|
||||
if r1:
|
||||
for r2 in norm[i+1:]:
|
||||
if r2 and r1[-1] >= r2[0]: # intersect?
|
||||
r1[:] = [r1[0], max(r1[-1], r2[-1])]
|
||||
r2.clear()
|
||||
return [rnge for rnge in norm if rnge]
|
||||
|
||||
if __name__ == '__main__':
|
||||
for s in [
|
||||
[[1.1, 2.2]],
|
||||
[[6.1, 7.2], [7.2, 8.3]],
|
||||
[[4, 3], [2, 1]],
|
||||
[[4, 3], [2, 1], [-1, -2], [3.9, 10]],
|
||||
[[1, 3], [-6, -1], [-4, -5], [8, 2], [-6, -6]],
|
||||
]:
|
||||
print(f"{str(s)[1:-1]} => {str(consolidate(s))[1:-1]}")
|
||||
88
Task/Range-consolidation/Python/range-consolidation-2.py
Normal file
88
Task/Range-consolidation/Python/range-consolidation-2.py
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
'''Range consolidation'''
|
||||
|
||||
from functools import reduce
|
||||
|
||||
|
||||
# consolidated :: [(Float, Float)] -> [(Float, Float)]
|
||||
def consolidated(xs):
|
||||
'''A consolidated list of
|
||||
[(Float, Float)] ranges.'''
|
||||
|
||||
def go(abetc, xy):
|
||||
'''A copy of the accumulator abetc,
|
||||
with its head range ab either:
|
||||
1. replaced by or
|
||||
2. merged with
|
||||
the next range xy, or
|
||||
with xy simply prepended.'''
|
||||
if abetc:
|
||||
a, b = abetc[0]
|
||||
etc = abetc[1:]
|
||||
x, y = xy
|
||||
return [xy] + etc if y >= b else ( # ab replaced.
|
||||
[(x, b)] + etc if y >= a else ( # xy + ab merged.
|
||||
[xy] + abetc # xy simply prepended.
|
||||
)
|
||||
)
|
||||
else:
|
||||
return [xy]
|
||||
|
||||
def tupleSort(ab):
|
||||
a, b = ab
|
||||
return ab if a <= b else (b, a)
|
||||
|
||||
return reduce(
|
||||
go,
|
||||
sorted(map(tupleSort, xs), reverse=True),
|
||||
[]
|
||||
)
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Tests'''
|
||||
|
||||
print(
|
||||
tabulated('Consolidation of numeric ranges:')(str)(str)(
|
||||
consolidated
|
||||
)([
|
||||
[(1.1, 2.2)],
|
||||
[(6.1, 7.2), (7.2, 8.3)],
|
||||
[(4, 3), (2, 1)],
|
||||
[(4, 3), (2, 1), (-1, -2), (3.9, 10)],
|
||||
[(1, 3), (-6, -1), (-4, -5), (8, 2), (-6, -6)]
|
||||
])
|
||||
)
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS FOR DISPLAY ---------------------------
|
||||
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g):
|
||||
'''Right to left function composition.'''
|
||||
return lambda f: lambda x: g(f(x))
|
||||
|
||||
|
||||
# tabulated :: String -> (a -> String) ->
|
||||
# (b -> String) ->
|
||||
# (a -> b) -> [a] -> String
|
||||
def tabulated(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> value list -> tabular string.'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
w = max(map(compose(len)(xShow), xs))
|
||||
return s + '\n' + '\n'.join([
|
||||
xShow(x).rjust(w, ' ') + ' -> ' + fxShow(f(x)) for x in xs
|
||||
])
|
||||
return lambda xShow: lambda fxShow: (
|
||||
lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue