aboutsummaryrefslogtreecommitdiff
path: root/challenge-050/user-person/python
diff options
context:
space:
mode:
authoruser-person <60802990+user-person@users.noreply.github.com>2020-03-08 15:44:37 -0400
committerGitHub <noreply@github.com>2020-03-08 15:44:37 -0400
commit3f54c0f817fd90c25485767df844d08f89974c57 (patch)
tree0647f343a269b8dae58daf78c82f62172b90bb8a /challenge-050/user-person/python
parent635adb076fcefbe49e5aba9f017132e0c480972d (diff)
downloadperlweeklychallenge-club-3f54c0f817fd90c25485767df844d08f89974c57.tar.gz
perlweeklychallenge-club-3f54c0f817fd90c25485767df844d08f89974c57.tar.bz2
perlweeklychallenge-club-3f54c0f817fd90c25485767df844d08f89974c57.zip
Create ch-1.py
Diffstat (limited to 'challenge-050/user-person/python')
-rw-r--r--challenge-050/user-person/python/ch-1.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/challenge-050/user-person/python/ch-1.py b/challenge-050/user-person/python/ch-1.py
new file mode 100644
index 0000000000..3a2721f302
--- /dev/null
+++ b/challenge-050/user-person/python/ch-1.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python3
+
+###########################################################################
+# script name: ch-1.py #
+# #
+# https://github.com/user-person #
+# #
+# https://perlweeklychallenge.org/blog/perl-weekly-challenge-050/ #
+# #
+# Merge Intervals #
+# Write a script to merge the given intervals where ever possible. #
+# #
+# [2,7], [3,9], [10,12], [15,19], [18,22] #
+# #
+# The script should merge [2, 7] and [3, 9] together to return [2, 9]. #
+# #
+# Similarly it should also merge [15, 19] and [18, 22] together to #
+# return [15, 22]. #
+# #
+# The final result should be something like below: #
+# #
+# [2, 9], [10, 12], [15, 22] #
+# #
+###########################################################################
+
+import re
+import sys
+
+input = "[2,7], [3,9], [10,12], [15,19], [18,22]"
+
+if len(sys.argv) > 1:
+ input = ' '.join(sys.argv[1:])
+
+input = re.sub(r'[][, ]+',' ',input)
+input = re.sub(r'\A\s+|\s+\Z','',input)
+sets = re.split(r' ',input)
+
+def printSets(sList):
+ lastInd = len(sList)-1
+ for k in range(0, lastInd, 2):
+ print('[' + str(sList[k]) + ', ' + str(sList[k+1]) + ']',end='')
+ if lastInd > k+2:
+ print(', ',end='')
+ print()
+
+def mergeUnits(inds):
+
+ indsVal = [sets[pos] for pos in inds]
+ indsVal.sort(key=int)
+ sets.append(indsVal[0])
+ sets.append(indsVal[3])
+
+ inds.sort(key=int,reverse=True)
+ for ind2pop in inds:
+ sets.pop(ind2pop)
+
+print('UNMERGED:')
+printSets(sets)
+
+loop = True
+while loop:
+
+ loop = False
+ for i in range(0, len(sets)-1, 2):
+
+ for j in range(0, len(sets)-1, 2):
+
+ if i == j:
+ continue
+
+ elif int(sets[j]) >= int(sets[i]) and int(sets[j]) <= int(sets[i+1]) or int(sets[j+1]) >= int(sets[i]) and int(sets[j+1]) <= int(sets[i+1]):
+ # Elegant? no, but it seems to work.
+
+ mergeUnits([i, i+1, j, j+1])
+ loop = True
+ break
+ else:
+ continue
+
+ break
+
+print('\nMERGED:')
+sets.sort(key=int)
+printSets(sets)