I Need Help with a line of code

I’m confused about the first for loop in this code. Let’s say the max_exponent is 3. Putting this in the range function gives us 0, 1, and 2. Does this for loop iterate through 0, 1 , and 2 - giving us 3 exponents and 3 indices? I’m not sure.

def radix_sort(to_be_sorted):
  max_exponent = len(str(max(to_be_sorted)))
  being_sorted = to_be_sorted[:]

  for pass_through in range(max_exponent):
    exponent = pass_through + 1
    index = -exponent
    digits = [[] for digit in range(10)]
    for item in being_sorted:
      item_as_a_string = str(item)
      try:
        digit_for_item = int(item_as_a_string[index])
      except IndexError:
        digit_for_item = 0

      digits[digit_for_item].append(item)
    being_sorted = []
    for digit in digits:
      being_sorted.extend(digit)

  return being_sorted

Yes. Put in a print() statement to see for yourself:

def radix_sort(to_be_sorted):
  max_exponent = len(str(max(to_be_sorted)))
  being_sorted = to_be_sorted[:]

  for pass_through in range(max_exponent):
    exponent = pass_through + 1
    index = -exponent
    print("pass_through = {}, index = {}, exponent = {}".format(pass_through, index, exponent))
    digits = [[] for digit in range(10)]
    for item in being_sorted:
      item_as_a_string = str(item)
      try:
        digit_for_item = int(item_as_a_string[index])
      except IndexError:
        digit_for_item = 0

      digits[digit_for_item].append(item)
    being_sorted = []
    for digit in digits:
      being_sorted.extend(digit)

  return being_sorted

print(radix_sort([12, 400, 323, 31, 9, 975]))

Output:

pass_through = 0, index = -1, exponent = 1
pass_through = 1, index = -2, exponent = 2
pass_through = 2, index = -3, exponent = 3
[9, 12, 31, 323, 400, 975]
2 Likes

Does this also mean that in the list [12, 400, 323, 31, 9, 975], 975 is put in 3 different lists in digits?

Add another print() statement. This time, we’ll simplify the input:

def radix_sort(to_be_sorted):
  max_exponent = len(str(max(to_be_sorted)))
  being_sorted = to_be_sorted[:]

  for pass_through in range(max_exponent):
    exponent = pass_through + 1
    index = -exponent
    print("pass_through = {}, index = {}, exponent = {}".format(pass_through, index, exponent))
    digits = [[] for digit in range(10)]
    for item in being_sorted:
      item_as_a_string = str(item)
      try:
        digit_for_item = int(item_as_a_string[index])
      except IndexError:
        digit_for_item = 0

      digits[digit_for_item].append(item)
      print("item = {}, digit_for_item = {}, digits = {}".format(item, digit_for_item, digits))
    being_sorted = []
    for digit in digits:
      being_sorted.extend(digit)

  return being_sorted

print(radix_sort([975, 965]))

Output:

pass_through = 0, index = -1, exponent = 1
item = 975, digit_for_item = 5, digits = [[], [], [], [], [], [975], [], [], [], []]   # index position 5
item = 965, digit_for_item = 5, digits = [[], [], [], [], [], [975, 965], [], [], [], []]
pass_through = 1, index = -2, exponent = 2
item = 975, digit_for_item = 7, digits = [[], [], [], [], [], [], [], [975], [], []]   # # index position 7
item = 965, digit_for_item = 6, digits = [[], [], [], [], [], [], [965], [975], [], []]  # # index positions 6 & 7
pass_through = 2, index = -3, exponent = 3 
item = 965, digit_for_item = 9, digits = [[], [], [], [], [], [], [], [], [], [965]]
item = 975, digit_for_item = 9, digits = [[], [], [], [], [], [], [], [], [], [965, 975]]   # index position 9
[965, 975]

So, yes, one time for each digit.

Wouldn’t this add a digit more than once to the being_sorted list? For example, 975 is in the 5 list, the 7 list, and the 9 list. Won’t it be added to being_sorted that many times?

Or is there just one list of lists?

Sorry, this is confusing

Note that being_sorted is reset to empty list each time around. Because 6 comes before 7, 965 winds up in the bin before 975 in step 2.

Use print() to see how being_sorted evolves.

1 Like

I know this is silly, but where can I go to print this out?

Put print(being_sorted) after.
´being_sorted.extend(digit)´

Go back and look again at the examples I showed above … print() can be very helpful in unraveling the flow of your code.


The whole idea is that you do the sort by matching list indices to digits: there is no “greater than” or “less than”.

The first time around the final digit is evaluated, both numbers get put in the same bucket, so being_sorted is [975, 965].

The second time around, they are put into two buckets, one for 6 and one for 7, and being_sorted is re-created by doing being_sorted.extend(), first with the 6 bucket, [965], then by the 7 bucket, [975], to become [965, 975]

The third time around, they both go into the same bucked (9), but the order is preserved, so your final round returns [965, 975].

Thank you, but I literally meant where can I print this code out. Like what platform

Copy and paste into a text editor, and print from there?

… Or did you mean run the code? You can do that right on the CodeAcademy page for this project.