Skip to content

Instantly share code, notes, and snippets.

@kamoljan
Last active August 29, 2015 14:03
Show Gist options
  • Save kamoljan/e4fc31c40661eb3bc3ea to your computer and use it in GitHub Desktop.
Save kamoljan/e4fc31c40661eb3bc3ea to your computer and use it in GitHub Desktop.
def solution(A):
l = len(A)
if not (0 < l <= 100000):
return -1
s_l = s_r = 0
s = sum(A)
for i in range(l):
if not (-2147483648 < A[i] < 2147483647):
return -1
s_r = s - s_l - A[i]
if s_l == s_r:
return i
s_l = s_l + A[i]
pass
# credit to http://blog.codility.com/2011/03/solutions-for-task-equi.html
def equi(A):
if A:
n = len(A)
if n > 1:
sl = 0
s2 = sum(A)
for i, e in enumerate(A):
if s1 == (s2-e):
return i
s1 += e
s2 -= e
return -1
else:
return 0
return -1
package solution
// you can also use imports, for example:
// import "fmt"
// you can use fmt.Println for debugging purposes, e.g.
// fmt.Println("this is a debug message")
// write your code in Go 1.4
func Solution(X int, Y int, D int) int {
if X == Y {
return 0
}
var s int = 0
if X < Y && D != 0 {
s = ((Y-X) / D)
// fmt.Println("s => ", s)
// fmt.Println("Y => ", Y)
if ((Y-X) - D * s > 0) {
// fmt.Println("D * s => ", D * s)
// fmt.Println("(Y-X) => ", (Y-X))
s = s + 1
}
}
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment