import random
import time

'''
In a given list List, find a pair of subsequent values
both of which are divisible by 10


Note.
The presented problem is slightly more complex
than the task of just finding a particular value.
Elementary tasks, like finding a value, are often directly supported
by a corresponding python function. However,
when the task becomes more complex by even a small margin,
which is the case of this one, typically neither python
nor other languages provide  any ready-made support
and the task has to be approached directly
"with bare hands", so to speak.


'''

def FindPairClassic( List ):
    i = 0
    while True:
        if i+1 >= len(List):   # checked in each loop
            break
        if List[i] % 10 == 0 and List[i+1] % 10 == 0:
            return i, List[i], List[i+1]
        i += 1
    return -1, -1, -1 #  not found


def FindPairWithSentinel( List ):
    i = 0
    List.extend( [10,10] ) # sentinel
    while True:
        if List[i] % 10 == 0 and List[i+1] % 10 == 0:
            return i, List[i], List[i+1]     # will surely happen sometime
        i += 1

# -----------------------------------------------------
# Demo of use

#simple example
aList = [11, 12, 13, 30, 20, 15, 40, 23 ]

# bigger example which demonstrates the effectivity
# of the sentinel technique

# List of random selection  of values in the given range.
# The second parameter (k) is the number of selected values.
# The values may occur more times in the list.
# (Useful for various tests of a function/module etc... )
aList = random.choices( range(11, 19), k = 10000000 )

print( aList[:10], '... len =', len(aList) )
t1 = time.time()
a,b,c = FindPairClassic(aList)
t2 = time.time()
print( a, b, c )

t2 = time.time()
a, b, c = FindPairWithSentinel(aList)
t3 = time.time()
print( a, b, c  )

print( 'time classic  ', t2-t1 )
print( 'time sentinel ', t3-t2 )


