Skip to content

Instantly share code, notes, and snippets.

@mfbx9da4
Last active February 18, 2016 22:43
Show Gist options
  • Save mfbx9da4/987d15a7cfe8421826e4 to your computer and use it in GitHub Desktop.
Save mfbx9da4/987d15a7cfe8421826e4 to your computer and use it in GitHub Desktop.
def update_not_x(k, array, num):
if k > len(array) - 1 or array[k] == num:
return 0
else:
return 1
def update_count_x(i, array, num):
if i == 0 or array[i - 1] != num:
return 0
else:
return 1
def solution(num, array):
i = 0
k = len(array)
count_x = 0
not_x = 0
while i != k or count_x != not_x:
if count_x > not_x:
if i == k:
count_x -= update_count_x(i, array, num)
i -= 1
k -= 1
not_x += update_not_x(k, array, num)
print 'decr k', i, k, count_x, not_x
else:
if i == k:
not_x -= update_not_x(k, array, num)
k -= 1
i += 1
count_x += update_count_x(i, array, num)
print 'incr i', i, k, count_x, not_x
return k
def solution2(x, a):
k = len(a)
i = 0
while i < k:
if a[i] == x:
k -= 1;
while a[k] == x and i < k:
k -= 1;
i += 1;
return k
solution = solution
assert solution(5, [5, 5, 1, 7, 2, 3, 5]) == 4
print ''
assert solution(5, [4, 4, 4, 4]) == 4
print ''
assert solution(4, [4, 4, 4, 4]) == 0
print ''
assert solution(1, [0, 0, 1, 1]) == 2
print ''
assert solution(0, [0, 0, 1, 1]) == 2
print ''
assert solution(1, [1, 1, 0, 0, 1, 1]) == 2
print ''
assert solution(0, [1, 1, 0, 0, 1, 1]) == 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment