Why can't I xor strings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dataangel

    Why can't I xor strings?

    I wrote a function to compare whether two strings are "similar" because
    I'm using python to make a small text adventure engine and I want to it
    to be responsive to slight mispellings, like "inevtory" and the like. To
    save time the first thing my function does is check if I'm comparing an
    empty vs. a non-empty string, because they are to be never considered
    similar. Right now I have to write out the check like this:

    if str1 and str2:
    if not str1 or not str2:
    return 0

    Because python won't let me do str1 ^ str2. Why does this only work for
    numbers? Just treat empty strings as 0 and nonempty as 1.

    Regardless of whether this is the best implementation for detecting if
    two strings are similar, I don't see why xor for strings shouldn't be
    supported. Am I missing something? Inparticular, I think it'd be cool to
    have "xor" as opposed to "^". The carrot would return the resulting
    value, while "xor" would act like and/or do and return the one that was
    true (if any).


  • Paul Rubin

    #2
    Re: Why can't I xor strings?

    dataangel <k04jg02@kzoo.e du> writes:[color=blue]
    > To save time the first thing my function does is check if
    > I'm comparing an empty vs. a non-empty string, because they are to be
    > never considered similar.[/color]
    [color=blue]
    > Right now I have to write out the check like this:
    >
    > if str1 and str2:
    > if not str1 or not str2:
    > return 0[/color]

    How about:

    def nonempty(s):
    return (len(s) > 0)

    and then

    if nonempty(str1) != nonempty(str2):
    return 0

    Of course there's other ways to write the same thing. Python 2.3 has
    a "bool" function which when called on a string, returns true iff the
    string is nonempty.

    Comment

    • Byron

      #3
      Re: Why can't I xor strings?

      Hi DataAngel,

      Welcome to the Python community!

      From reading your post, it sounds like you are wanting to use XOR
      encryption because there are other ways to quickly and easily compare
      strings to see if they are equal.

      To compare a string, use the following:

      name = "Steven"
      name2 = "Steven"
      print name == name2

      The result will be "True." However, if my initial guess is correct that
      you are wanting to use XOR for strings, it probably because you are
      wanting a quick way of encrypting data so that basic users won't be able
      to view the information. If this is what you are wanting, I might have
      something for you...

      Let me know,

      Byron
      ---


      dataangel wrote:[color=blue]
      > I wrote a function to compare whether two strings are "similar" because
      > I'm using python to make a small text adventure engine and I want to it
      > to be responsive to slight mispellings, like "inevtory" and the like. To
      > save time the first thing my function does is check if I'm comparing an
      > empty vs. a non-empty string, because they are to be never considered
      > similar. Right now I have to write out the check like this:
      >
      > if str1 and str2:
      > if not str1 or not str2:
      > return 0
      >
      > Because python won't let me do str1 ^ str2. Why does this only work for
      > numbers? Just treat empty strings as 0 and nonempty as 1.
      >
      > Regardless of whether this is the best implementation for detecting if
      > two strings are similar, I don't see why xor for strings shouldn't be
      > supported. Am I missing something? Inparticular, I think it'd be cool to
      > have "xor" as opposed to "^". The carrot would return the resulting
      > value, while "xor" would act like and/or do and return the one that was
      > true (if any).
      >
      >[/color]

      Comment

      • Byron

        #4
        Re: Why can't I xor strings?

        Hi DataAngel,

        Welcome to the Python community!

        From reading your post, it sounds like you are wanting to use XOR for
        "encryption ." The reason why I say this is because there are many other
        ways of comparing the content of two strings to see if they are exactly
        the same. An example of how to do such a thing is listed below:

        # How to compare two strings to see if they are exact matches.
        name = "Steven"
        name2 = "Steven"
        print name == name2


        The result will be "True."

        However, the only reason that one might want to use XOR for a string is
        if he or she wanted to use XOR based encryption in order to keep data
        semi-private.

        Let me know,

        Byron
        ---

        dataangel wrote:[color=blue]
        > I wrote a function to compare whether two strings are "similar" because
        > I'm using python to make a small text adventure engine and I want to it
        > to be responsive to slight mispellings, like "inevtory" and the like. To
        > save time the first thing my function does is check if I'm comparing an
        > empty vs. a non-empty string, because they are to be never considered
        > similar. Right now I have to write out the check like this:
        >
        > if str1 and str2:
        > if not str1 or not str2:
        > return 0
        >
        > Because python won't let me do str1 ^ str2. Why does this only work for
        > numbers? Just treat empty strings as 0 and nonempty as 1.
        >
        > Regardless of whether this is the best implementation for detecting if
        > two strings are similar, I don't see why xor for strings shouldn't be
        > supported. Am I missing something? Inparticular, I think it'd be cool to
        > have "xor" as opposed to "^". The carrot would return the resulting
        > value, while "xor" would act like and/or do and return the one that was
        > true (if any).
        >
        >[/color]

        Comment

        • Grant Edwards

          #5
          Re: Why can't I xor strings?

          On 2004-10-08, Byron <DesertLinux@ne tscape.net> wrote:[color=blue]
          > Hi DataAngel,
          >
          > Welcome to the Python community!
          >
          > From reading your post, it sounds like you are wanting to use XOR
          > encryption[/color]

          No, he wants to do an exclusive or of the boolean values of the
          strings.
          [color=blue]
          > The result will be "True." However, if my initial guess is correct that
          > you are wanting to use XOR for strings, it probably because you are
          > wanting a quick way of encrypting data so that basic users won't be able
          > to view the information.[/color]

          No, he explained exactly what he was trying to do, and it had
          nothing to do with encryption. He wants to know if exactly one
          (1) of the strings is the empty string.

          --
          Grant Edwards grante Yow! I hope something GOOD
          at came in the mail today so
          visi.com I have a REASON to live!!

          Comment

          • Byron

            #6
            Re: Why can't I xor strings?

            Grant Edwards wrote:[color=blue]
            > No, he explained exactly what he was trying to do, and it had
            > nothing to do with encryption. He wants to know if exactly one
            > (1) of the strings is the empty string.[/color]

            Hi Grant,

            Opps, that's what happens when on skims through a post a light speed...
            lol :-)

            Byron
            ---

            Comment

            • Byron

              #7
              Re: Why can't I xor strings?

              Grant Edwards wrote:[color=blue]
              > No, he explained exactly what he was trying to do, and it had
              > nothing to do with encryption. He wants to know if exactly one
              > (1) of the strings is the empty string.[/color]


              Hi Grant,

              Opps, that's what happens when one skims through a post a light speed...
              lol :-)

              Byron
              ---

              Comment

              • Byron

                #8
                Re: Why can't I xor strings?

                Grant Edwards wrote:[color=blue]
                > No, he explained exactly what he was trying to do, and it had
                > nothing to do with encryption. He wants to know if exactly one
                > (1) of the strings is the empty string.[/color]


                Hi Grant,

                Opps, that's what happens when one skims through a post at light
                speed... lol :-)

                Byron
                ---

                Comment

                • Andrew Dalke

                  #9
                  Re: Why can't I xor strings?

                  Grant Edwards:[color=blue]
                  > No, he explained exactly what he was trying to do, and it had
                  > nothing to do with encryption. He wants to know if exactly one
                  > (1) of the strings is the empty string.[/color]

                  BTW, another way to get that is

                  if bool(str1) + bool(str2) == 1:
                  print "one and only one of them was empty"


                  Andrew
                  dalke@dalkescie ntific.com

                  Comment

                  • Steven Bethard

                    #10
                    Re: Why can't I xor strings?

                    Grant Edwards <grante <at> visi.com> writes:
                    [color=blue]
                    > No, he wants to do an exclusive or of the boolean values of the
                    > strings.[/color]

                    I'm guessing the OP has already guessed this solution from the variety already
                    provided, but the direct translation of this statement would be:

                    bool(x) ^ bool(y)

                    for example:
                    [color=blue][color=green][color=darkred]
                    >>> bool("") ^ bool("a")[/color][/color][/color]
                    True[color=blue][color=green][color=darkred]
                    >>> bool("") ^ bool("")[/color][/color][/color]
                    False[color=blue][color=green][color=darkred]
                    >>> bool("a") ^ bool("a")[/color][/color][/color]
                    False[color=blue][color=green][color=darkred]
                    >>> bool("a") ^ bool("")[/color][/color][/color]
                    True


                    Steve




                    Comment

                    • Grant Edwards

                      #11
                      Re: Why can't I xor strings?

                      On 2004-10-09, Steven Bethard <steven.bethard @gmail.com> wrote:[color=blue]
                      > Grant Edwards <grante <at> visi.com> writes:
                      >[color=green]
                      >> No, he wants to do an exclusive or of the boolean values of the
                      >> strings.[/color]
                      >
                      > I'm guessing the OP has already guessed this solution from the
                      > variety already provided, but the direct translation of this
                      > statement would be:
                      >
                      > bool(x) ^ bool(y)[/color]

                      :)

                      It dawned on me later that perhaps the jump from what I wrote
                      to the code you wrote wasn't as obvious as I thought.

                      --
                      Grant Edwards grante Yow!
                      at TAILFINS!!... click...
                      visi.com

                      Comment

                      • Jeremy Bowers

                        #12
                        Re: Why can't I xor strings?

                        On Fri, 08 Oct 2004 16:19:22 -0400, dataangel wrote:[color=blue]
                        > Regardless of whether this is the best implementation for detecting if
                        > two strings are similar, I don't see why xor for strings shouldn't be
                        > supported. Am I missing something?[/color]

                        The basic problem is that there is no obvious "xor on string" operation
                        *in general*, even if you stipulate "bitwise". In particular, what does it
                        mean to bitwise xor two different length strings?

                        "In general" is the key, here. You actually don't have strings,
                        conceptually, you have "tokens" or "commands" or something that happen to
                        be strings. I recommend creating a class to match this concept by
                        subclassing str... or even just write it directly without subclassing
                        string. You can then make __xor__(self, other) for that class do whatever
                        makes sense with your concept.



                        I can almost guarantee that you will later find other things to put into
                        that class. It may very well clean up a lot of code.

                        Comment

                        • Grant Edwards

                          #13
                          Re: Why can't I xor strings?

                          On 2004-10-09, Jeremy Bowers <jerf@jerf.or g> wrote:[color=blue]
                          >[color=green]
                          >> Regardless of whether this is the best implementation for
                          >> detecting if two strings are similar, I don't see why xor for
                          >> strings shouldn't be supported. Am I missing something?[/color]
                          >
                          > The basic problem is that there is no obvious "xor on string" operation[/color]

                          Sure there is. Strings have a boolean value, and the xor
                          operation on boolean values is well-defined.

                          --
                          Grant Edwards grante Yow! Are you mentally here
                          at at Pizza Hut??
                          visi.com

                          Comment

                          • David Bolen

                            #14
                            Re: Why can't I xor strings?

                            Grant Edwards <grante@visi.co m> writes:
                            [color=blue]
                            > On 2004-10-09, Jeremy Bowers <jerf@jerf.or g> wrote:[color=green]
                            > >[color=darkred]
                            > >> Regardless of whether this is the best implementation for
                            > >> detecting if two strings are similar, I don't see why xor for
                            > >> strings shouldn't be supported. Am I missing something?[/color]
                            > >
                            > > The basic problem is that there is no obvious "xor on string" operation[/color]
                            >
                            > Sure there is. Strings have a boolean value, and the xor
                            > operation on boolean values is well-defined.[/color]

                            That's an operation, but I'm not sure that's the obvious one. For my
                            part, if I saw "string1 ^ string2" I'd probably expect a byte by byte
                            xor with the result being a new string.

                            It doesn't feel natural to me to have my strings suddenly interpreted
                            as a new data type based on the operation at hand. Logical operators
                            work that way but not numerics (it would be in the same vein as string
                            + number interpreting the string as a number - that way lies Perl :-))

                            -- David

                            Comment

                            • Grant Edwards

                              #15
                              Re: Why can't I xor strings?

                              On 2004-10-09, David Bolen <db3l@fitlinxx. com> wrote:
                              [color=blue][color=green][color=darkred]
                              >>> The basic problem is that there is no obvious "xor on string"
                              >>> operation[/color]
                              >>
                              >> Sure there is. Strings have a boolean value, and the xor
                              >> operation on boolean values is well-defined.[/color]
                              >
                              > That's an operation, but I'm not sure that's the obvious one.
                              > For my part, if I saw "string1 ^ string2" I'd probably expect
                              > a byte by byte xor with the result being a new string.[/color]

                              Only because Python lacks a logical xor operator, so you're
                              used to thinking of ^ as a bitwise operator. What if you saw

                              string1 xor string2?

                              Wouldn't you expect it to be equivalent to

                              (string1 and (not string2)) or ((not string1) and string2)
                              [color=blue]
                              > It doesn't feel natural to me to have my strings suddenly
                              > interpreted as a new data type based on the operation at hand.
                              > Logical operators work that way but not numerics[/color]

                              I don't know what you mean by that. Nobody seems to have a
                              problem with "and" "or" and "not" operators using the truth
                              values of strings. What is there about "xor" that precludes it
                              from behaving similarly?
                              [color=blue]
                              > (it would be in the same vein as string + number interpreting
                              > the string as a number - that way lies Perl :-))[/color]

                              I don't see that at all.

                              --
                              Grant Edwards grante Yow! Let's go to CHURCH!
                              at
                              visi.com

                              Comment

                              Working...