OmkarPathak/pygorithm

Data Structures and Searching UnitTests Failing

Closed this issue · 9 comments

test_data_structures and test_searching.py are failing their UnitTests

test_data_structures

Error
Traceback (most recent call last):
  File "C:\Users\doarni\AppData\Local\Continuum\Anaconda3\lib\unittest\case.py", line 58, in testPartExecutor
    yield
  File "C:\Users\doarni\AppData\Local\Continuum\Anaconda3\lib\unittest\case.py", line 600, in run
    testMethod()
  File "C:\Users\doarni\Dev\pygorithm\tests\test_data_structure.py", line 39, in test_infix_to_postfix
    resultString = result.infix_to_postfix()
  File "C:\Users\doarni\Dev\pygorithm\pygorithm\data_structures\stack.py", line 131, in infix_to_postfix
    and self.__precedence(self.expression[i] <= self.__precedence(self.my_stack.peek())):
TypeError: unorderable types: str() <= int()

.....
======================================================================
ERROR: test_infix_to_postfix (tests.test_data_structure.TestInfixToPostfix)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\doarni\Dev\pygorithm\tests\test_data_structure.py", line 39, in test_infix_to_postfix
    resultString = result.infix_to_postfix()
  File "C:\Users\doarni\Dev\pygorithm\pygorithm\data_structures\stack.py", line 131, in infix_to_postfix
    and self.__precedence(self.expression[i] <= self.__precedence(self.my_stack.peek())):
TypeError: unorderable types: str() <= int()

----------------------------------------------------------------------
Ran 14 tests in 0.031s

FAILED (errors=1)

test_searching

EE.
======================================================================
ERROR: test_binary_search (tests.test_searching.TestBinarySearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\doarni\Dev\pygorithm\tests\test_searching.py", line 40, in test_binary_search
    alpha_result = binary_search.search(self.array, 'n')
  File "C:\Users\doarni\Dev\pygorithm\pygorithm\searching\binary_search.py", line 37, in search
    elif target < _list[mid]:
TypeError: unorderable types: str() < int()

======================================================================
ERROR: test_dfs (tests.test_searching.TestDFSSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\doarni\Dev\pygorithm\tests\test_searching.py", line 72, in test_dfs
    result = depth_first_search.search(self.graph, 'A')
  File "C:\Users\doarni\Dev\pygorithm\pygorithm\searching\depth_first_search.py", line 21, in search
    _path = path + [start]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'

----------------------------------------------------------------------
Ran 4 tests in 0.016s

FAILED (errors=2)

They both work fine for me. I'm using Python 3.5.3 and I installed pygorithm from pip3 install pygorithm + have cloned the repository. I am running python3 -m unittest from the pygorithm directory.

Does this occur with the changes you made in #37? Maybe the issue is there.

@MrDupin I think the changes I made caused this

@MrDupin I've reverted changes in #37 to and test_searching works now. How ever my knowledge of Stacks is essentially none, and test_data_structures is still failing.
The only major change besides removing unnecessary parenthesis in conditionals and modifiying this method __isOperand(char)

  
# OLD VERSION
return ord(char) >= ord('a') and ord(char) <= ord('z') or ord(char) >= ord('A') and ord(char) <= ord('Z')

# NEW VERISON
return True if ord(char) in [ord(c) for c in list(ascii_letters)] else False

The above works for me. This is my _isOperand function:

def _isOperand(self, char):
        from string import ascii_letters
        return True if ord(char) in [ord(c) for c in ascii_letters] else False

I removed the conversion of ascii_letters to a list, since it's not necessary.

What is your error message?

@MrDupin The same as the error above

Error
Traceback (most recent call last):
  File "C:\Users\doarni\AppData\Local\Continuum\Anaconda3\lib\unittest\case.py", line 58, in testPartExecutor
    yield
  File "C:\Users\doarni\AppData\Local\Continuum\Anaconda3\lib\unittest\case.py", line 600, in run
    testMethod()
  File "C:\Users\doarni\Dev\pygorithm\tests\test_data_structure.py", line 39, in test_infix_to_postfix
    resultString = result.infix_to_postfix()
  File "C:\Users\doarni\Dev\pygorithm\pygorithm\data_structures\stack.py", line 131, in infix_to_postfix
    and self.__precedence(self.expression[i] <= self.__precedence(self.my_stack.peek())):
TypeError: unorderable types: str() <= int()

The error occurs in the infix_to_postfix. Can you comment your infix_to_postfix function? Maybe you converted something that shouldn't have been converted, or vice-versa.

@MrDupin

    def infix_to_postfix(self):
        """
        function to generate postfix expression from infix expression
        """
        postfix = []
        for i in range(len(self.expression)):
            if self.__is_operand(self.expression[i]):
                postfix.append(self.expression[i])
            elif self.expression[i] == '(':
                self.my_stack.push(self.expression[i])
            elif self.expression[i] == ')':
                top_operator = self.my_stack.pop()
                while not self.my_stack.is_empty() and top_operator != '(':
                    postfix.append(top_operator)
                    top_operator = self.my_stack.pop()
            else:
                while not self.my_stack.is_empty() \
                        and self.__precedence(self.expression[i] <= self.__precedence(self.my_stack.peek())):
                    postfix.append(self.my_stack.pop())
                self.my_stack.push(self.expression[i])

        while not self.my_stack.is_empty():
            postfix.append(self.my_stack.pop())
        return ' '.join(postfix)

First of all, you accidentally wrote __is_operand and __precedence instead of _isOperand and _precedence.

Second, you removed a parentheses in self.__precedence(self.expression[i] <= .... There should have been a parenthesis before the "<=".

The tests pass now: https://pastebin.com/vH8KdUEu

@MrDupin I changed _isOperand to _is_operand since camel case is not common in python syntaxing. After fixing the removed paranthesis all UnitTests now pass!

Thank you!!

Launching Nosetest with arguments C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2016.3.2\helpers\pycharm\_jb_nosetest_runner.py tests.test_data_structure:TestStack.test_stack in C:\Users\doarni\pygorithm
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK