From 62a4966e9720150386a6d037ce2e08a42fd99dba Mon Sep 17 00:00:00 2001 From: Orien Vandenbergh Date: Thu, 25 Aug 2016 17:06:21 -0600 Subject: [PATCH] Preserving my solutions for foobar --- python/foobar/one/calc.py | 19 ++++++++++++++++ python/foobar/two/zombit.py | 44 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 python/foobar/one/calc.py create mode 100644 python/foobar/two/zombit.py diff --git a/python/foobar/one/calc.py b/python/foobar/one/calc.py new file mode 100644 index 0000000..9fdf987 --- /dev/null +++ b/python/foobar/one/calc.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +samples = [ [ "2+3*2", "232*+" ], ["2*4*3+9*3+5", "243**93*5++"], ["8*9+7*6+1+2+4*5*6", "89*76*12456**++++" ] ] + +def answer(str): + rpn = "" + groups = str.split('+') + for group in groups: + entries = group.split('*') + for entry in entries: + rpn += entry + rpn += "*"*(len(entries)-1) + rpn += "+"*(len(groups)-1) + return rpn + +for sample in samples: + print answer(sample[0]) + print sample[1] + diff --git a/python/foobar/two/zombit.py b/python/foobar/two/zombit.py new file mode 100644 index 0000000..b93cb46 --- /dev/null +++ b/python/foobar/two/zombit.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +samples = [ [[[1,3],[3,6]],5], [[[10,14],[4,18],[19,20],[19,20],[13,20]],16] ] + +def answer(intervals): + """Calculate the total number of units covered by the supplied collection of intervals""" + while True: + changed, intervals = iterate(intervals) + if not changed: + break + + covered = 0 + for interval in intervals: + covered += interval[1] - interval[0] + + return covered + +def iterate(intervals): + ranges = [intervals.pop(0)] + changed = False + while (len(intervals)>0): + interval = intervals.pop() + merged = False + for i in range(0,len(ranges)): + if overlaps(interval,ranges[i]): + ranges[i] = merge(ranges[i],interval) + merged = True + changed = True + if not merged: + ranges.append(interval) + return changed, ranges + +def overlaps(a,b): + if (a[0]b[0]): + return True + else: + return False + +def merge(a,b): + return [min(a[0],b[0]),max(a[1],b[1])] + +for sample in samples: + print answer(sample[0]) + print sample[1]