Questions tagged [reverse]

Rearranging the order of a sequence such that the final order is a mirror image of the original.

Rearranging the order of a sequence such that the final order is a mirror image of the original.

3773 questions
57
votes
5 answers

Reverse the order of a legend

I use the following code to plot the bar graph and need to present a legend in reverse order. How can I do it? colorsArr = plt.cm.BuPu(np.linspace(0, 0.5, len(C2))) p = numpy.empty(len(C2), dtype=object) plt.figure(figsize=(11, 11)) prevBar = 0 for…
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
53
votes
5 answers

Reverse Sorted Dictionary in .NET

Is there any way I can iterate backwards (in reverse) through a SortedDictionary in c#? Or is there a way to define the SortedDictionary in descending order to begin with?
Gal Goldman
  • 8,641
  • 11
  • 45
  • 45
51
votes
3 answers

Python Reverse Find in String

I have a string and an arbitrary index into the string. I want find the first occurrence of a substring before the index. An example: I want to find the index of the 2nd I by using the index and str.rfind() s = "Hello, I am 12! I like plankton but I…
user156027
51
votes
15 answers

Reversing an Array in Java

If I have an array like this: 1 4 9 16 9 7 4 9 11 What is the best way to reverse the array so that it looks like this: 11 9 4 7 9 16 9 4 1 I have the code below, but I feel it is a little tedious: public int[] reverse3(int[] nums) { return…
PHZE OXIDE
  • 549
  • 3
  • 6
  • 8
49
votes
18 answers

Reversing a String with Recursion in Java

Here is some Java code to reverse a string recursively. Could someone provide an explanation of how it works? public static String reverse(String str) { if ((null == str) || (str.length() <= 1)) { return str; } return…
Bob Sanders
  • 4,317
  • 4
  • 18
  • 11
48
votes
10 answers

How to replace some characters from the end of a string?

I want to replace characters at the end of a python string. I have this string: s = "123123" I want to replace the last 2 with x. Suppose there is a method called replace_last: >>> replace_last(s, '2', 'x') '1231x3' Is there any built-in or easy…
Freewind
  • 193,756
  • 157
  • 432
  • 708
48
votes
5 answers

Can I loop through a javascript object in reverse order?

So I have a JavaScript object like this: foo = { "one": "some", "two": "thing", "three": "else" }; I can loop this like: for (var i in foo) { if (foo.hasOwnProperty(i)) { // do something } } Which will loop through the properties in…
frequent
  • 27,643
  • 59
  • 181
  • 333
47
votes
5 answers

LinearLayoutManager setReverseLayout() == true but items stack from bottom

This seems like it would be an easy solution, but it seems that setting private RecyclerView mRecyclerView; private RecyclerView.Adapter mAdapter; private LinearLayoutManager mLayoutManager; .... // More code mRecyclerView = (RecyclerView)…
AndyRoid
  • 5,062
  • 8
  • 38
  • 73
46
votes
7 answers

Rails: Is it bad to have an irreversible migration?

When is it acceptable to raise an ActiveRecord::IrreversibleMigration exception in the self.down method of a migration? When should you take the effort to actually implement the reverse of the migration?
readonly
  • 343,444
  • 107
  • 203
  • 205
46
votes
2 answers

Reverse string: string[::-1] works, but string[0::-1] and others don't

I am somewhat of a python/programming newbie, and I have just been playing around with string slicing. So the simple string reverse method of string[::-1] works just fine as we know, but there are other instances in my code below that yields…
Darren Haynes
  • 1,343
  • 4
  • 18
  • 31
45
votes
6 answers

Reverse Modulus Operator

Over 3 years after asking the question I found the solution. I have included it as an answer. I have an expression with modulus in it that needs to be put in terms of x. (a + x) mod m = b I can't figure out what to do with the modulus. Is there a…
Michael Hogenson
  • 1,292
  • 1
  • 15
  • 31
43
votes
33 answers

Java reverse an int value without using array

Can anyone explain to me how to reverse an integer without using array or String. I got this code from online, but not really understand why + input % 10 and divide again. while (input != 0) { reversedNum = reversedNum * 10 + input % 10; …
user236501
  • 8,538
  • 24
  • 85
  • 119
42
votes
14 answers

Reverse a string in Python two characters at a time (Network byte order)

Say you have this string: ABCDEFGH And you want to reverse it so that it becomes: GHEFCDAB What would be the most efficient / pythonic solution? I've tried a few different things but they all look horrible... Thanks in advance! Update: In case…
PeterM
  • 2,534
  • 6
  • 31
  • 38
42
votes
1 answer

Reverse iterator returns garbage when optimized

I have a AsIterator template class which takes a numeric-like type, in this example just an int, and converts it into an iterator (++ and -- increment and decrement the number, and operator* just returns a reference to it). This works fine unless…
Pietro Saccardi
  • 2,602
  • 34
  • 41
42
votes
5 answers

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

How exactly can you take a string, split it, reverse it and join it back together again without the brackets, commas, etc. using python?
Tstrmwarrior
  • 431
  • 1
  • 4
  • 5