swap

Swap slices of indexes using a function

大兔子大兔子 提交于 2019-11-30 09:53:56
问题 Follow-up question of: Python swap indexes using slices r = ['1', '2', '3', '4', '5', '6', '7', '8'] If I want to swap slices, using a function, what would be the correct method? def swap(from,to): r[a:b+1], r[c+1:d] = r[c:d], r[a:b] swap(a:b,c:d) I want to swap the numbers 3 + 4 with 5 + 6 + 7 in r: swap(2:4,4:7) Is this correct? 回答1: Without any calculation, you can do : def swap(r,a,b,c,d): assert a<=b<=c<=d r[a:d]=r[c:d]+r[b:c]+r[a:b] 回答2: An interesting (but silly one, the one by B. M.

How to find number of expected swaps in bubble sort in better than O(n^2) time

情到浓时终转凉″ 提交于 2019-11-30 09:47:30
问题 I am stuck on problem http://www.codechef.com/JULY12/problems/LEBOBBLE Here it is required to find number of expected swaps. I tried an O(n^2) solution but it is timing out. The code is like: swaps = 0 for(i = 0;i < n-1;i++) for(j = i+1;j<n;j++) { swaps += expected swap of A[i] and A[j] } Since probabilities of elements are varying, so every pair is needed to be compared. So according to me the above code snippet must be most efficient but it is timing out. Can it be done in O(nlogn) or it

Swap values in a tuple/list inside a list in python?

假装没事ソ 提交于 2019-11-30 07:11:14
I have a tuple/list inside a list like this: [('foo','bar'),('foo1','bar1'),('foofoo','barbar')] What is the fastest way in python (running on a very low cpu/ram machine) to swap values like this... [('bar','foo'),('bar1','foo1'),('barbar','foofoo')] curently using: for x in mylist: self.maynewlist.append((_(x[1]),(x[0]))) Is there a better or faster way??? iabdalkader You could use map: map (lambda t: (t[1], t[0]), mylist) Or list comprehension: [(t[1], t[0]) for t in mylist] List comprehensions are preferred and supposedly much faster than map when lambda is needed, however note that list

Using pointers to swap int array values

▼魔方 西西 提交于 2019-11-30 05:20:13
问题 I am supposed to use pointers to swap ints in an array. It compiles with no errors or warnings and runs but does not swap the ints. Any suggestions would be helpful!!! Here is the tester: #import <stdio.h> void swap( int ary[] ); int main( int argc, char*argv[] ) { int ary[] = { 25, 50 }; printf( "The array values are: %i and %i \n", ary[0], ary[1] ); swap( ary ); printf( "After swaping the values are: %i and %i \n", ary[0], ary[1] ); return 0; } Here is the swap function: void swap( int ary[

reorder byte order in hex string (python)

我的未来我决定 提交于 2019-11-30 03:55:54
问题 I want to build a small formatter in python giving me back the numeric values embedded in lines of hex strings. It is a central part of my formatter and should be reasonable fast to format more than 100 lines/sec (each line about ~100 chars). The code below should give an example where I'm currently blocked. 'data_string_in_orig' shows the given input format. It has to be byte swapped for each word. The swap from 'data_string_in_orig' to 'data_string_in_swapped' is needed. In the end I need

Why will two-phase lookup fail to choose overloaded version of 'swap'?

人走茶凉 提交于 2019-11-30 03:24:49
问题 I am studying this fascinating answer to a subtle question regarding the best practice to implement the swap function for user-defined types . (My question was initially motivated by a discussion of the illegality of adding types to namespace std.) I will not re-print the code snippet from the above-linked answer here. Instead, I would like to understand the answer. The answer I've linked above states, beneath the first code snippet, in regards to overloading swap in namespace std (rather

Is the copy and swap idiom still useful in C++11

我与影子孤独终老i 提交于 2019-11-30 01:13:05
I refer to this question: What is the copy-and-swap idiom? Effectively, the above answer leads to the following implementation: class MyClass { public: friend void swap(MyClass & lhs, MyClass & rhs) noexcept; MyClass() { /* to implement */ }; virtual ~MyClass() { /* to implement */ }; MyClass(const MyClass & rhs) { /* to implement */ } MyClass(MyClass && rhs) : MyClass() { swap(*this, rhs); } MyClass & operator=(MyClass rhs) { swap(*this, rhs); return *this; } }; void swap( MyClass & lhs, MyClass & rhs ) { using std::swap; /* to implement */ //swap(rhs.x, lhs.x); } However, notice that we

Difference Swapping and Paging

馋奶兔 提交于 2019-11-29 20:29:37
What are the differences between Swapping and Paging with reference to Process Memory Management ? Also guide me to the tutorials if any where I could get more information. Swapping refers to copying the entire process address space, or at any rate, the non-shareable-text data segment, out to the swap device, or back, in one go (typically disk). Whereas paging refers to copying in/out one or more pages of the address space. In particular, this is at a much finer grain. For example, there are ~250,000 4 KB pages in a 1 GB RAM address space. Swapping was used in the early days, e.g. DEC pdp-11

Is there a better way to reverse an array of bytes in memory?

会有一股神秘感。 提交于 2019-11-29 18:01:29
问题 typedef unsigned char Byte; ... void ReverseBytes( void *start, int size ) { Byte *buffer = (Byte *)(start); for( int i = 0; i < size / 2; i++ ) { std::swap( buffer[i], buffer[size - i - 1] ); } } What this method does right now is it reverses bytes in memory. What I would like to know is, is there a better way to get the same effect? The whole "size / 2" part seems like a bad thing, but I'm not sure. EDIT: I just realized how bad the title I put for this question was, so I [hopefully] fixed

Why swap doesn't use Xor operation in C++

☆樱花仙子☆ 提交于 2019-11-29 18:01:04
问题 I've learned that Xor operation can be used to implement effective swap function. like this: template<class T> void swap(T& a, T& b) { a = a^b; b = a^b; a = a^b; } But the implementation of swap all i can found on the internet is essentially like this: template<class T> void swap(T& a, T& b) { T temp(a); a = b; b = temp; } It seems that the compiler didn't generate the same code for the two form above because I tested it on VC++ 2010 and the first one is done the job more quickly than std: