Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add script to coalesce a monotonically increasing list of numbers to …

…a more palatable-appearing form
  • Loading branch information...
commit 6a1268dee75742722b2d042b88ea2d9ba7bf9ebb 1 parent a5bc864
@ttjoseph authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 coalesce_number_list.py
View
28 coalesce_number_list.py
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+#
+# Coalesces a list such as "1 2 3 4 5 10 15 19 20 21" to "1-5; 10; 15; 19-21"
+# for use with the print_res keyword of MMPBSA.py.
+import sys
+
+# Read and parse numbers
+data = " ".join(sys.stdin.readlines()).split()
+numbers = [int(x) for x in data]
+
+def print_range(s, e):
+ if s != e:
+ print "%d-%d;" % (s, e),
+ else:
+ print "%d;" % s,
+
+start = numbers[0]
+end = numbers[0]
+last = start
+for i in xrange(1, len(numbers)):
+ num = numbers[i]
+ if num == last + 1:
+ end = num
+ else:
+ print_range(start, end)
+ start = end = num
+ last = num
+print_range(start, end)
Please sign in to comment.
Something went wrong with that request. Please try again.