I had an interview recently and was asked to solve the following problem using Python:
Write a function:
def solution(A):
such that, given an array A consisting of N integers, it returns the maximum among all one-digit integers.
For example, given array A as follows:
[-6, -91, 1011, -100, 84, -22, 0, 1, 473]
the function should return 1.
Assume that:
- N is an integer within the range [1...1,000]
- Each element of array
Ais an integer within the range [-10,000..10,000]. - There is at least one element in array
Awhich satisfies the condition in the task statement.
I came up with the following solution which I thought was elegant, but the score I got was only 55%:
def solution(A):
return max([i for i in A if len(str(i))==1])
Why is this solution incorrect?
-6is one digit. Butlen(str(-6))is2?if -9 <= i <= 9.lower_case_with_underscoresand they should be descriptive.Aisn't either.