Python vs Java: String Reversal with Minimal Code

Reversing a string: Java vs Python – minimal code, maximum impact! This is exactly why Python caught my attention over Java: less code, more performance. Sometimes, simplicity wins. 🚀 #Java #Python #coding #programming

  • graphical user interface, text, application

Rust is usually considered intimidating, so here's a bit of code to disprove it: // iterate over characters, reverse the order, collect into a new string let reversed = str.chars().rev().collect::<String>(); or, if it's an ASCII string: assert!(str.is_ascii()); // get raw bytes, reverse them, convert back let mut bytes = str.into_bytes(); bytes.reverse(); let reversed = String::from_utf8(bytes).expect("should be a valid ASCII string"); second approach reverses the string in-place, with no (re)allocations at all.

System.out.print(new StringBuilder(str). reverse().toString()) Same simple as python 😉

Like
Reply

Python Part would raise a SyntaxError, because you can‘t use type ‚str‘ as variable name 🤣

Like
Reply

How do you measure performance here?

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories