The main idea is to concentrate on numbers in the range -10 < i < 10
I propose a different solution, somewhat simpler than yours:
$ cat /tmp/tmp.py
from __future__ import print_function
import random
def max_single_digit(N):
cur_max = -10
for i in N:
if (i > cur_max) and (-10 < i < 10):
cur_max = i
if cur_max > -10:
return cur_max
else:
return "No single digit numbers found"
_range = 20
negatives = tuple([random.randint(-100,0) for x in range(_range)])
positives = tuple([random.randint(0,100) for x in range(_range)])
numbers = tuple([random.randint(-100,100) for x in range(_range)])
for nums in (negatives, positives, numbers):
print("For", nums, "the result is:", max_single_digit(nums),"\n")
Which gives:
$ python /tmp/tmp.py
For (0, -31, -87, -80, -47, -21, -21, -14, -37, -43, -71, -61, -47, -4, -36, -72, -78, -83, -14, -70) the result is: 0
For (17, 83, 80, 50, 35, 43, 9, 75, 23, 38, 45, 55, 46, 99, 80, 93, 36, 97, 88, 30) the result is: 9
For (-1, 30, -20, -68, 13, -66, -71, 92, 77, 85, -100, 8, 32, 92, -6, 97, 40, 21, 13, -48) the result is: 8
$ python /tmp/tmp.py
For (-94, -94, -28, -37, -50, -5, -29, -51, -6, -24, -18, -46, -32, -20, -89, -49, -55, -39, -50, -30) the result is: -5
For (91, 4, 16, 68, 6, 100, 61, 92, 81, 65, 63, 87, 67, 67, 97, 89, 98, 53, 40, 89) the result is: 6
For (11, 49, 63, -17, 71, 50, 28, 5, 31, -100, -35, -5, -8, 77, -87, 77, 3, 8, -39, -97) the result is: 8
$ python /tmp/tmp.py
For (-55, -48, -52, -75, -1, -89, -53, -66, -48, -17, -9, -96, -16, -40, -52, 0, -90, -97, -40, -85) the result is: 0
For (52, 19, 82, 45, 54, 47, 94, 54, 46, 8, 66, 22, 100, 25, 0, 81, 79, 39, 5, 20) the result is: 8
For (-43, -32, 92, -59, -91, 63, -95, 100, -85, -21, 35, -88, -38, 43, -25, 85, 76, 67, -82, 87) the result is: No single digit numbers found
if a[i]<10 and a[j]<10:- akpif a[i]<10 and a[j]<10:why is that condition necessary? - omri_saadon