2 Replies
      Latest reply on Jun 29, 2019 6:56 PM by John368
      Protium01 Level 1 Level 1 (0 points)
        1. (1, "zebra") < (2, "apple") // true because 1 is less than 2; "zebra" and "apple" are not compared
        2. (3, "apple") < (3, "bird") // true because 3 is equal to 3, and "apple" is less than "bird"
        3. (4, "dog") == (4, "dog") // true because 4 is equal to 4, and "dog" is equal to "dog"

         

        In the example above, you can see the left-to-right comparison behavior on the first line. Because 1 is less than 2, (1, "zebra") is considered less than (2, "apple"), regardless of any other values in the tuples. It doesn’t matter that "zebra" isn’t less than "apple", because the comparison is already determined by the tuples’ first elements. However, when the tuples’ first elements are the same, their second elements are compared—this is what happens on the second and third line.

         

        Why is Zebra not less than apple, and apple is less than bird ? How do you know this, do you count the letters ? or how is it done ? What is the criteria to determine ?

        • Re: Operators, Tuple comparison with logical boolean operators.I found that difficult to understand !
          Claude31 Level 8 Level 8 (6,865 points)

          You need a logic to compare t-uples.

          The first item is evaluated. If different, you get the result. If equal, compare second:

          (1, "zebra") < (1, "apple")  // false

           

          It is the same logic as in word dictionary: you compare the first letter ; if different, you have the order ; if equal, then compare the second and so on.

           

          You could redefine your own, but what would be your spec ?

          What would you want the result of (1, "zebra") < (2, "apple") to be ? false ? And why, because zebra is not less than apple ?

          But you have conflicting elements: 1 < 2 (should give true) but "zebra" > "apple" (should give false

          In addition, it must work in a consitent way with > operator.

           

          Then, one will argue that you ignore that 1 < 2

           

          Which to be considered ? The logic of tuple comparison is to consider the first to begin with.

           

          Another way would be to revert the t-uples:

          ("zebra", 1) and ("apple", 2)

           

          For the 2nd question

          Why is Zebra not less than apple, and apple is less than bird ? How do you know this, do you count the letters ? or how is it done ? What is the criteria to determine ?

           

          In fact "Zebra" < "apple" is true.

          but

          "zebra" < "apple" is false

           

          because strings are compared character by character and letters are in order of A … Z  a … z

           

          So, when comparing strings, it is safer to convert first to lowercase both

           

          print("Zebra".lowercased() < "apple".lowercased())

          Now yields false

          • Re: Operators, Tuple comparison with logical boolean operators.I found that difficult to understand !
            John368 Level 1 Level 1 (0 points)

            Once I had the same question. The fact is that the characters are not compared alphabetically, but by the code of the character in the Unicode table.