Merge branch 'master' of github.com:nichus/sundry

This commit is contained in:
Orien Vandenbergh 2016-08-28 08:42:51 -06:00
commit 48420b6499
3 changed files with 64 additions and 0 deletions

19
python/foobar/one/calc.py Normal file
View File

@ -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]

View File

@ -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[1]) and (a[1]>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]

1
vimrc
View File

@ -26,6 +26,7 @@ set softtabstop=2
set shiftwidth=2 set shiftwidth=2
set number set number
"set smartindent "set smartindent
set expandtab
set cindent set cindent
set list set list
set listchars=tab:▻\ ,trail:◈ set listchars=tab:▻\ ,trail:◈