I suspect you're making this rather more work for yourself than you really need to, but here goes.
If you're going to go for a line-by-line port, then do that.
Don't declare result as an array of strings. That'll just give you a headache. Make it either a
StringBuilderor a plainStringas I do here (StringBuilderwill be more efficient, admittedly, but this is probably easier to understand). This is also more similar to the python code you already have.Understand what your python code is doing. It's taking a string in hex format, parsing it to an integer, adding a value (
theShift), converting back into hex, and then getting just the numeric part of the string (without the leading0x). So in Java, that loop goes like this. (NOTE: in JavaInteger.toString(x, 16)does not print the leading0x, so we don't need to chop it off).String result = ""; for (String thisHex : theHexArray) { result += Integer.toString(Integer.parseInt(thisHex, 16) + theShift, 16) + " "; }Lose the
toTextmethod. At this point you have the string you wanted, so that method doesn't really do anything anymore.
Transitioning from Python to Java
Python to Java code conversion - Stack Overflow
how can I translate efficiently a Java code to python? - Stack Overflow
Java to Python
Honestly I wouldn't even bother preparing beforehand. If you have a strong grasp of programming fundamentals, you'll breeze through a starting course anyway. At least if you don't know python you can focus on learning that in the lessons, otherwise you'll be sat there knowing everything anyway trying to find a way to make use of your time.
Carrying on with Java is what I'd do in the meantime. You'll be pushing yourself harder than learning the basics again twice (now and in class)
More on reddit.comCan I convert Python to Java automatically?
Can I also convert Java back to Python?
Can I also convert Python back to Java?
Videos
Hi.
Basically I am not that interested in Java.
However, I need to understand algorithms, not just solve them.I read books, watched some things online, and even if i can explain some algorithm I can't code it.
I even had difficulties making a linked list in C.
I also lack the OOP understanding. I can read about it, solve quizzes but that is the thing i get most tripped about in Python.
Maybe it just my impatience as I found out I haven't exactly watched tutorials as I cant wait to get into it myself.
Maybe I should just bite the bullet and learn it exclusively for Sedgewicks algorithms.
To be honest i really like Python and see a future with it. Other languages aside from C have mostly been a derail and then I crawl back to Python and spend the next few weeks getting pythonic.
That's the thing, most other languages are idiomatic, but Python is pythonic. Maybe I've spoiled myself, trying to code in english?
I really want to understand classes and i heard Java is great for it too.
The only thing that puts me away is the syntax and the extreme opinion of the JVM, I don't want to spend a month relearning Python again.
To be honest at this point I would be really happy if someone said they followed Sedgewick just with python code from github or that the knowledge is transferable when watching lectures.
I tried with MIT and Standfords Roughgarden but it is very theoretical and at times very mathematical and far removed from code.
I get it i really do, that is what algorithms are. But for now I don't have the luxury of learning mathematics and relearning induction.
https://cs50.harvard.edu/ai/2023/
My goal for now is to be able to this and do it well and actually understand how it works.
I suspect you're making this rather more work for yourself than you really need to, but here goes.
If you're going to go for a line-by-line port, then do that.
Don't declare result as an array of strings. That'll just give you a headache. Make it either a
StringBuilderor a plainStringas I do here (StringBuilderwill be more efficient, admittedly, but this is probably easier to understand). This is also more similar to the python code you already have.Understand what your python code is doing. It's taking a string in hex format, parsing it to an integer, adding a value (
theShift), converting back into hex, and then getting just the numeric part of the string (without the leading0x). So in Java, that loop goes like this. (NOTE: in JavaInteger.toString(x, 16)does not print the leading0x, so we don't need to chop it off).String result = ""; for (String thisHex : theHexArray) { result += Integer.toString(Integer.parseInt(thisHex, 16) + theShift, 16) + " "; }Lose the
toTextmethod. At this point you have the string you wanted, so that method doesn't really do anything anymore.
Regardless of whether you decide to translate line by line, or work on understanding the code and then write a Java based solution to the problem, you need to break down that last line to understand it. Try looking at it like this:
result += (hex((int(theHex[i],16) + theShift))).split('x')[1] + " "
is the same as -
val1 = int(theHex[i],16)
val2 = (val1 + theShift)
val3 = hex(val2)
val4 = (val3).split('x')
result += val4[1] + " "
Now you can see more clearly what is being called. Your next step is to look up what the int, hex and split calls are doing.