Last active
July 31, 2018 15:23
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jpp: 46.10399893007707 | |
Laurent H.: 52.31541666598059 | |
Kevin: 41.856350317946635 | |
davidak: 43.6837813219754 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import numpy as np | |
import copy | |
import timeit | |
l = [False, False, False, True, True, True, False, False, True, False, False] | |
k = [True, True, True, True, True, True, False, False, True, True, True] | |
number = 1000000 | |
def invert_edge_values_jpp(l): | |
idx1 = next(i for i, j in enumerate(l) if j) | |
idx2 = next(i for i, j in enumerate(l[::-1]) if j) | |
l = np.array(l) | |
l[:idx1] = True | |
l[len(l)-idx2:] = True | |
np.testing.assert_array_equal(l, np.array(k)) | |
def invert_edge_values_laurent_h(l): | |
l = copy.deepcopy(l) | |
tempList = l[:] | |
# Iterate from the beginning (and invert) until the element differs from the first one | |
for i,v in enumerate(tempList): | |
if v == tempList[0]: l[i] = not v | |
else: break | |
# Iterate from the end (and invert) until the element differs from the last one | |
for i,v in reversed(list(enumerate(tempList))): | |
if v == tempList[-1]: l[i] = not v | |
else: break | |
np.testing.assert_array_equal(np.array(l), np.array(k)) | |
def invert_edge_values_kevin(l): | |
for i in range(len(l)): | |
if l[i]: | |
break | |
l[i] = True | |
for i in range(len(l)-1, -1, -1): | |
if l[i]: | |
break | |
l[i] = True | |
np.testing.assert_array_equal(np.array(l), np.array(k)) | |
def invert_edge_values_davidak(l): | |
l = np.array(l) | |
ind = np.where(l)[0] | |
first, last = ind[0], ind[-1] | |
l[:first] = True | |
l[last + 1:] = True | |
np.testing.assert_array_equal(l, np.array(k)) | |
if __name__ == '__main__': | |
print("jpp:", timeit.timeit("invert_edge_values_jpp(l)", number=number, globals=globals())) | |
print("Laurent H.:", timeit.timeit("invert_edge_values_laurent_h(l)", number=number, globals=globals())) | |
print("Kevin:", timeit.timeit("invert_edge_values_kevin(l)", number=number, globals=globals())) | |
print("davidak:", timeit.timeit("invert_edge_values_davidak(l)", number=number, globals=globals())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment