Created
July 21, 2015 22:21
-
-
Save oskar-j/babf966aa486a6fa23bb to your computer and use it in GitHub Desktop.
Find lowest index of array's slices having lowest average of elements (in Python)
This file contains hidden or 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
def solution(A): | |
min_avg_value = (A[0] + A[1])/2.0 # The mininal average | |
min_avg_pos = 0 # The begin position of the first | |
# slice with mininal average | |
for index in xrange(0, len(A)-2): | |
# Try the next 2-element slice | |
if (A[index] + A[index+1]) / 2.0 < min_avg_value: | |
min_avg_value = (A[index] + A[index+1]) / 2.0 | |
min_avg_pos = index | |
# Try the next 3-element slice | |
if (A[index] + A[index+1] + A[index+2]) / 3.0 < min_avg_value: | |
min_avg_value = (A[index] + A[index+1] + A[index+2]) / 3.0 | |
min_avg_pos = index | |
# Try the last 2-element slice | |
if (A[-1]+A[-2])/2.0 < min_avg_value: | |
min_avg_value = (A[-1]+A[-2])/2.0 | |
min_avg_pos = len(A)-2 | |
return min_avg_pos |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source: http://codesays.com/2014/solution-to-min-avg-two-slice-by-codility/