Rewriting this function from python to julia

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Rewriting this function from python to julia

Jacob Yates
return "".join(chr(ord(a)^ord(b)) for a,b in zip(s,t))


Now I know that chr = Char, ord = Int, and zip = tuple in julia. But while using this in that context I get a load error saying the I'm missing a ", or )"


function xorStrings(s,t)
        global xorStr = join(Char(Int(a)^Int(b)) for a,b in tuple(s,t)
exit
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriting this function from python to julia

Stefan Karpinski
^ isn't xor in Julia, it's exponentiation. You can use infix $ instead.

On Mon, Nov 7, 2016 at 3:02 PM, Jacob Yates <[hidden email]> wrote:
return "".join(chr(ord(a)^ord(b)) for a,b in zip(s,t))


Now I know that chr = Char, ord = Int, and zip = tuple in julia. But while using this in that context I get a load error saying the I'm missing a ", or )"


function xorStrings(s,t)
        global xorStr = join(Char(Int(a)^Int(b)) for a,b in tuple(s,t)
exit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriting this function from python to julia

Jacob Yates

I know ^ is an exponent. The whole function is meant to xor strings the strings end up being the variables s and t


On Nov 7, 2016 15:12, "Stefan Karpinski" <[hidden email]> wrote:
^ isn't xor in Julia, it's exponentiation. You can use infix $ instead.

On Mon, Nov 7, 2016 at 3:02 PM, Jacob Yates <[hidden email]> wrote:
return "".join(chr(ord(a)^ord(b)) for a,b in zip(s,t))


Now I know that chr = Char, ord = Int, and zip = tuple in julia. But while using this in that context I get a load error saying the I'm missing a ", or )"


function xorStrings(s,t)
        global xorStr = join(Char(Int(a)^Int(b)) for a,b in tuple(s,t)
exit

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriting this function from python to julia

Stefan Karpinski
You also need parens around (a,b):

julia> xorStrings(s,t) = join(Char(Int(a) $ Int(b)) for (a,b) in zip(s,t))
xorStrings (generic function with 1 method)

julia> xorStrings("foo","bar")
"\x04\x0e\x1d"

julia> xorStrings("foo",ans)
"bar"

On Mon, Nov 7, 2016 at 3:17 PM, blm22 <[hidden email]> wrote:

I know ^ is an exponent. The whole function is meant to xor strings the strings end up being the variables s and t


On Nov 7, 2016 15:12, "Stefan Karpinski" <[hidden email]> wrote:
^ isn't xor in Julia, it's exponentiation. You can use infix $ instead.

On Mon, Nov 7, 2016 at 3:02 PM, Jacob Yates <[hidden email]> wrote:
return "".join(chr(ord(a)^ord(b)) for a,b in zip(s,t))


Now I know that chr = Char, ord = Int, and zip = tuple in julia. But while using this in that context I get a load error saying the I'm missing a ", or )"


function xorStrings(s,t)
        global xorStr = join(Char(Int(a)^Int(b)) for a,b in tuple(s,t)
exit


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriting this function from python to julia

Steven G. Johnson
In reply to this post by Jacob Yates
On Monday, November 7, 2016 at 3:17:54 PM UTC-5, Jacob Yates wrote:

I know ^ is an exponent. The whole function is meant to xor strings the strings end up being the variables s and t

 
By "xor the strings", I think you mean "xor the bytes"?   Note that chr and ord in Python only work for ASCII characters, so I guess you are only interested in ASCII data?   In that case, I would do:

xorstrings(s::String, t::String) = String(s.data $ t.data)

(requires Julia 0.5).   You will have to think carefully about whether you want to handle non-ASCII Unicode strings and what you want to do in that case.  (The above code will hande non-ASCII data by xor-ing their UTF-8 representations.  It's not clear whether this is what you want.)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Rewriting this function from python to julia

Stefan Karpinski
Yeah, that version is considerably more concise as well. My version is the "how you would fix the thing you were trying to do" version :)

On Mon, Nov 7, 2016 at 3:27 PM, Steven G. Johnson <[hidden email]> wrote:
On Monday, November 7, 2016 at 3:17:54 PM UTC-5, Jacob Yates wrote:

I know ^ is an exponent. The whole function is meant to xor strings the strings end up being the variables s and t

 
By "xor the strings", I think you mean "xor the bytes"?   Note that chr and ord in Python only work for ASCII characters, so I guess you are only interested in ASCII data?   In that case, I would do:

xorstrings(s::String, t::String) = String(s.data $ t.data)

(requires Julia 0.5).   You will have to think carefully about whether you want to handle non-ASCII Unicode strings and what you want to do in that case.  (The above code will hande non-ASCII data by xor-ing their UTF-8 representations.  It's not clear whether this is what you want.)

Loading...