hellopeeps
hellopeeps

Reputation: 15

I am trying to solve the hacker rank problem. I am still a beginner and I don't know where I went wrong with Python 3

THIS IS THE QUESTION: Given an array of integers, find and print the maximum number of integers you can select from the array such that the absolute difference between any two of the chosen integers is less than or equal to 1

def pickingNumbers(a):
    maxi=0
    for i in a:
        x=a.count(i)
        y=a.count(i-1)
        x=x+y
        if x>maxi :
            maxi=x
    print(maxi)     
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())
    a = list(map(int, input().rstrip().split()))

    result = pickingNumbers(a)

    fptr.write(str(result) + '\n')

    fptr.close()

given input:

6
4 6 5 3 3 1

expected output: 3

my output: None

Upvotes: 0

Views: 104

Answers (1)

Thierry Lathuille
Thierry Lathuille

Reputation: 24233

You printed the value of maxi at the end of pickingNumbers instead of returning it.

As you don't explicitely return a value, your function returns None, which get converted to the string 'None' in str(result)

Just replace it:

def pickingNumbers(a):
    maxi = 0
    for i in a:
        x = a.count(i)
        y = a.count(i-1)
        x = x+y
        if x > maxi :
            maxi = x
    return maxi

and you should be fine...

Upvotes: 1

Related Questions