1

can you help me, what is it bad on my code?

def generalized_price_sorter_expensive_cheap_assert(inputList, typeOfSort):
    #print(inputList)
    if typeOfSort == "cheap":
        inputListSorted = inputList.copy()
        inputListSorted.sort()


        if inputList == inputListSorted:  ##compare first list to second list, if is equal = good
            print("Cheap sorter is OK")

        else:
            print("Cheap sorter is NOT OK")

    if typeOfSort == "expensive":
        inputListSorted = inputList.copy()
        inputListSorted.sort(reverse=True)
        if inputList == inputListSorted:
            print("Expensive sorter is OK")
        else:
            print("Expensive sorter is NOT OK")

    print("LIST FROM WEB:")
    print(inputList)
    print("CORRECTLY SORTED LIST")
    print(inputListSorted)

    assert inputList == inputListSorted

My assertion error:

<selenium.webdriver.remote.webelement.WebElement (session="7d749d08e3af4a8efc0322859c6b3e2e", element="0D305CB722AA126A875927A62168E5F6_element_91")>
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
Expensive sorter is NOT OK
LIST FROM WEB:
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
CORRECTLY SORTED LIST
[14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938, 11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178, 11038, 10938, 10838, 10478]
Tzane
  • 2,752
  • 1
  • 10
  • 21

2 Answers2

0

You need to assign the sorted list to the variable, as sorting is not done in place.

inputListSorted = inputList.copy()
inputListSorted = sorted(inputListSorted)

# Or in a single line

inputListSorted = sorted(inputList.copy())
strojan
  • 158
  • 7
0

You are getting assertion error because the ordering of elements is different between your lists. If you want to check if both lists have the same elements you could do it with like this:

import collections

def assert_same_elements(left, right):
    assert collections.Counter(left) == collections.Counter(right)

list_from_web = [
    14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938,
    12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838,
    11318, 11858, 11458, 10938
]

correctly_sorted_list = [
    14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938,
    11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178,
    11038, 10938, 10838, 10478
]

assert_same_elements(list_from_web, correctly_sorted_list)
Tzane
  • 2,752
  • 1
  • 10
  • 21