Skip to content

Instantly share code, notes, and snippets.

@patrick91
Created May 30, 2015 13:24
Show Gist options
  • Save patrick91/2205b353a45ab62272c7 to your computer and use it in GitHub Desktop.
Save patrick91/2205b353a45ab62272c7 to your computer and use it in GitHub Desktop.
def merge(range1, range2):
"""
returns a merged range if range can be merged,
otherwise returns False.
we suppose that range1[0] <= range2[0]
"""
if range1 == range2:
return range1
a, b = range1
c, d = range2
# A B C D
# [0, 10] [5, 20]
# [0, 10] [5, 7]
# [4, 18] [19, 20]
if b >= c or c - b == 1:
return [a, max(b, d)]
return False
def answer(times):
times = sorted(times, key=lambda x: x[0])
merged_ranges = []
index = 0
done = False
while not done:
if index >= len(times):
break
current_range = times[index]
skipped = 0
while not done:
if index + 1 + skipped >= len(times):
merged_ranges.append(current_range)
done = True
break
next_range = times[index + 1 + skipped]
merged = merge(current_range, next_range)
if merged:
current_range = merged
skipped += 1
else:
index += skipped
merged_ranges.append(current_range)
break
index += 1
value = 0
for x in merged_ranges:
value += x[1] - x[0]
return value
assert answer([[10, 14], [4, 18], [19, 20], [19, 20], [13, 20]]) == 16
assert answer([[1, 3], [3, 6]]) == 5
@Zenaedres
Copy link

def func(z):
return list(map(lambda y: oct(y), z))

def main(N):
z = []
m = []
if N % 3 == 0:
z.append(True)
else: z.append(False)
z.append(chr(N))
x = 1
while len(m) < 5:
if (N + x) % 3 != 0:
m.append(N + x)
x = x + 1
z.append(m)
z.append( func(m))
return z

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment