How do you reverse a string in-place?

As long as you’re dealing with simple ASCII characters, and you’re happy to use built-in functions, this will work: function reverse(s){ return s.split(“”).reverse().join(“”); } If you need a solution that supports UTF-16 or other multi-byte characters, be aware that this function will give invalid unicode strings, or valid strings that look funny. You might want … Read more

Reverse A Binary Tree (Left to Right) [closed]

You can use recursion. We swap the left and right child of a node, in-place, and then do the same for its children: static void reverseTree(final TreeNode root) { final TreeNode temp = root.right; root.right = root.left; root.left = temp; if (root.left != null) { reverseTree(root.left); } if (root.right != null) { reverseTree(root.right); } } … Read more

Reverse each individual word of “Hello World” string with Java

This should do the trick. This will iterate through each word in the source string, reverse it using StringBuilder‘s built-in reverse() method, and output the reversed word. String source = “Hello World”; for (String part : source.split(” “)) { System.out.print(new StringBuilder(part).reverse().toString()); System.out.print(” “); } Output: olleH dlroW Notes: Commenters have correctly pointed out a few … Read more

Sort a list of tuples by second value, reverse=True and then by key, reverse=False

The following works with your input: d = [(‘B’, 3), (‘A’, 2), (‘A’, 1), (‘I’, 1), (‘J’, 1)] sorted(d,key=lambda x:(-x[1],x[0])) Since your “values” are numeric, you can easily reverse the sort order by changing the sign. In other words, this sort puts things in order by value (-x[1]) (the negative sign puts big numbers first) … Read more

How do I construct a Django reverse/url using query args?

Building an url with query string by string concatenation as suggested by some answers is as bad idea as building SQL queries by string concatenation. It is complicated, unelegant and especially dangerous with a user provided (untrusted) input. Unfortunately Django does not offer an easy possibility to pass query parameters to the reverse function. Python … Read more

Reverse iterator returns garbage when optimized

Looking at std::reverse_iterator‘s libstdc++ implementation reveals something interesting: /** * @return A reference to the value at @c –current * * This requires that @c –current is dereferenceable. * * @warning This implementation requires that for an iterator of the * underlying iterator type, @c x, a reference obtained by * @c *x remains valid … Read more

Python: How exactly can you take a string, split it, reverse it and join it back together again? [duplicate]

>>> tmp = “a,b,cde” >>> tmp2 = tmp.split(‘,’) >>> tmp2.reverse() >>> “”.join(tmp2) ‘cdeba’ or simpler: >>> tmp = “a,b,cde” >>> ”.join(tmp.split(‘,’)[::-1]) ‘cdeba’ The important parts here are the split function and the join function. To reverse the list you can use reverse(), which reverses the list in place or the slicing syntax [::-1] which returns … Read more

How do I reverse a String in Dart?

The question is not well defined. Reversing arbitrary strings does not make sense and will lead to broken output. The first (surmountable) obstacle is Utf-16. Dart strings are encoded as Utf-16 and reversing just the code-units leads to invalid strings: var input = “Music \u{1d11e} for the win”; // Music 𝄞 for the win print(input.split(”).reversed.join()); … Read more