0

Wing IDE keeps stating that

line 403, in <module>
    new = _insert_in_order(self, temp)
builtins.NameError: name '_insert_in_order' is not defined 

however, it is defined in my code. My teacher is not replying and I am stuck. Any advices on how I could approach it? The problem is in the add function.

class SortedFreqList(FreqList):
"""FreqList that keeps items in order, sorted by their frequencies"""

    def __init__(self):
        FreqList.__init__(self)
        self.freq_list_type = 'SortedFreqList'

    def _insert_in_order(self, freq_node):
        """ Takes a FreqNode and inserts in to the list so that
        items are sorted from largest to smallest.
        NOTE: The list must contain something for this method to work.
        In general this method should only be called from the add method,
        see the add method docstring for information on how to use this method.
        ***** DON'T change this method *****
        """
        # so we don't have to lookup each time
        freq_of_item = freq_node.frequency

        # check to see if larger than first freq in list
        if freq_of_item > self.head.frequency:
            freq_node.next_node = self.head
            self.head = freq_node
        else:
            curr_freq = self.head
            inserted = False
            while curr_freq.next_node is not None and not inserted:
                if freq_of_item > curr_freq.next_node.frequency:
                    # insert here
                    freq_node.next_node = curr_freq.next_node
                    curr_freq.next_node = freq_node
                    inserted = True
                else:
                    curr_freq = curr_freq.next_node
            # got to end and didn't find
            if not inserted:
                freq_node.next_node = None  # as now at end of list
                curr_freq.next_node = freq_node

    def add(self, new_item):
        """
        If the list is empty then make a new FreqNode and insert it at head.
        If the new_item is not already in freq list then adds the given
        item with a frequency of 1 as a FreqNode object to the end of the list.

        If the given new item is already in the list,
           the frequency is incremented by 1.
           If needed (ie, the freq is now greater than the previous node),
             the node is removed and then inserted
             in to its sorted position - using _insert_in_order.

        >>> f = SortedFreqList()
        >>> f.add('a')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'a' = 1
        >>> f.add('b')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'a' = 1
          2:  'b' = 1
        >>> f.add('b')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 1
        >>> f.add('c')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 1
          3:  'c' = 1
        >>> f.add('a')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 2
          3:  'c' = 1
        >>> f.add('c')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'b' = 2
          2:  'a' = 2
          3:  'c' = 2
        >>> f.add('c')
        >>> f.add('d')
        >>> f.add('d')
        >>> f.add('e')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'c' = 3
          2:  'b' = 2
          3:  'a' = 2
          4:  'd' = 2
          5:  'e' = 1
        >>> f.add('e')
        >>> f.add('e')
        >>> print(f)
        Sorted Frequency List
        ---------------------
          1:  'c' = 3
          2:  'e' = 3
          3:  'b' = 2
          4:  'a' = 2
          5:  'd' = 2
        """
        # make sure you read the docstring for this method!
        # ---start student section---
        if self.head is None:
            self.head = FreqNode(new_item)
        else:
            found = False
            current = self.head
            previous = None
            while current is not None:
                if current.item == new_item:
                    current.increment()
                    found = True
                    temp = current
                    previous.next_node = current.next_node
                    new = _insert_in_order(self, temp)
                previous = current
                current = current.next_node

            if not found:
                new_node = FreqNode(new_item)
                current = self.head
                while current.next_node!= None:
                    current = current.next_node
                current.next_node = new_node 
smci
  • 32,567
  • 20
  • 113
  • 146
  • 1
    self._insert_in_order is what is defined – joaquin Jan 27 '18 at 05:44
  • Your methods `__init__`,`insert_in_order` are not indented, hence they're not actually part of `SortedFreqList` class. Fix that indentation first, rerun, tell us if what error if any you get. – smci Jan 27 '18 at 05:46
  • @smci my fault, I edited the class line then the rest became unindented – joaquin Jan 27 '18 at 05:49
  • Does this answer your question? [How can I call a function within a class?](https://stackoverflow.com/questions/5615648/how-can-i-call-a-function-within-a-class) – mkrieger1 Aug 27 '22 at 21:13

2 Answers2

1

Functions defined in a class needs to be referenced with self. When calling the function though you don't need the self.

new = self._insert_in_order(temp)

r.ook
  • 13,466
  • 2
  • 22
  • 39
1

Your methods __init__ and insert_in_order are not indented, hence they're not actually part of SortedFreqList class. (EDIT: you fixed that)

Fix that indentation first, rerun, confirm the error disappears. (Inside methods, you also need to call other methods with self.insert_in_order as Idlehands said)

smci
  • 32,567
  • 20
  • 113
  • 146