0

I'm trying to make a selection sort using python, I just recently learned python following the book "Python Crash Course" then now I'm reading the book "grokking algorithms"

I pretty much understood the selection sort algorithm but I just can't figure out why I'm getting a TypeError: 'NoneType' object cannot be interpreted as an integer

So the way I test it is to populate my array using random.randint(1, 101) and range(0,11), what I can't figure out is why python can't interpret it as an integer? since both function returns an integer. The error seems to happen when I do the new_array.append(arr.pop(smallest)).

see my full code below. (I'm using Python 3)

import random

def find_smallest(arr):
    """Return the index of the smallest value in an array"""
    smallest = arr[0]
    smallest_index = 0

    for i in range(1, len(arr)):
        if arr[i] < smallest:
            smallest = arr[i]
            smallest_index = i
        return smallest_index

def selection_sort(arr):
    """sort an array by storing smallest value to new array 1 by 1"""
    new_array = []
    for i in range(len(arr)):
        smallest = find_smallest(arr)
        new_array.append(arr.pop(smallest))
    return new_array


my_array = []
for i in range(0, 11):
    my_array.append(random.randint(1, 101))
    print(my_array[i])

sorted_array = selection_sort(my_array)
for i in range(len(sorted_array)):
    print(sorted_array[i])

1 Answer 1

1

Looks like an indentation problem in your find_smallest() function

Consider

def find_smallest(arr):
    """Return the index of the smallest value in an array"""
    smallest = arr[0]
    smallest_index = 0

    for i in range(1, len(arr)):
        if arr[i] < smallest:
            smallest = arr[i]
            smallest_index = i
    return smallest_index  # <--- this is the line

Note that the return statement is indented differently than in your code.

To debug such problems you should notice that arr.pop() requires an integer or a list of integers as an argument, so you could insert

 assert(smallest is not None)

above the failing line new_array.append(arr.pop(smallest)) and confirm that assertion is triggered, then check at which iteration this happens replacing assert statement with

 assert(smallest is not None), "Array length is %d" % len(arr)

This would trigger an exception

 AssertionError: Array length is 1

Then you would know that you need to check what does your find_smallest function return when its argument is an array of length 1.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much! I understand it now since the return statement is incorrectly indented the way the script works is that it will only return when the "if statement" has been met. thanks, I appreciate it how you include debugging steps for such problem. you're awesome!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.