swap

Swap slices of indexes using a function

允我心安 提交于 2019-11-29 17:17:50
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? 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] An interesting (but silly one, the one by B. M. is clearly better) solution would be to create an object that supports slicing: class _Swapper(object): def __init

What's the point of iter_swap?

元气小坏坏 提交于 2019-11-29 16:41:13
问题 I was just wondering, why would anybody write this: std::iter_swap(i, k); instead of this? std::swap(*i, *k); // saved a few keystrokes! Then I looked into the implementation of iter_swap , and of course it only uses swap instead of std::swap since we're already in namespace std , anyway. That leads me to the next question: Why would anybody write this: using std::swap; swap(a, b); instead of this? std::iter_swap(&a, &b); // saved an entire line of code! Are there any important differences

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

China☆狼群 提交于 2019-11-29 16:32:16
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 any complexity better than O(n^2). Give me any hint if possible. Alright, let's think about this. We

Why do these swap functions behave differently?

社会主义新天地 提交于 2019-11-29 15:58:36
#include <stdio.h> void swap1(int a, int b) { int temp = a; a = b; b = temp; } void swap2(int *a, int *b) { int *temp = a; a = b; b = temp; } void swap3(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } main() { int a = 9, b = 4; printf("%d , %d\n", a, b); swap1(a, b); printf("%d , %d\n", a, b); swap2(&a, &b); printf("%d , %d\n", a, b); swap3(&a, &b); printf("%d , %d\n", a, b); } C has value semantics for function parameters. This means the a and b for all your three swap variants are local variables of the respective functions. They are copies of the values you pass as arguments. In

Android: ArrayList Move Item to Position 0

心不动则不痛 提交于 2019-11-29 14:58:55
问题 I have an ArrayList and I need to make sure a specific item is at the 0 position and if it is not, I need to move it there. The item has an isStartItem boolean on it, so I can easily find the specific item I need to be in position 0 but then how do I go about moving it to the right position? I am assuming I need to use something like this: for(int i=0; i<myArray.size(); i++){ if(myArray.get(i).isStartItem()){ Collection.swap(myArray, i, 0); } } But this does not seem to work... 回答1: You need

Swap elements in LinkedList

五迷三道 提交于 2019-11-29 13:57:56
I want to maintain order of the elements being added in a list. So, I used a LinkedList in Java. Now I want to be able to swap two elements in the linked list. First of all, I cannot find an elementAt() for LinkedList . Also, there is no way to add element at a specified position. There is a Collections.swap(List<?> list, int i, int j) that you can use to swap two elements of a List<?> . There's also LinkedList.get(int index) and LinkedList.add(int index, E element) (both are methods specified by interface List ). All of these operations will be O(N) since a LinkedList does not implements

Undefined behaviour of operators in XOR swap algorithm?

隐身守侯 提交于 2019-11-29 10:59:04
void swap(int* a, int* b) { if (a != b) *a ^= *b ^= *a ^= *b; } As the above *a ^= *b ^= *a ^= *b is just a shortcut for *a = *a ^ (*b = *b ^ (*a = *a ^ *b)) , could (e.g.) the 2nd *a be evaluated (for the XOR) just before the 3rd *a is modified (by the =)? Does it matter whether I write it in C99/C11/C++98/C++11? The C++11 standard says: 5.17/1: The assignment operator (=) and the compound assignment operators all group right-to-left . (...) the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. 1

如何合理设置Linux的swap分区

不问归期 提交于 2019-11-29 09:39:54
Swap,即交换区,除了安装Linux的时候,有多少人关心过它呢?其实,Swap的调整对Linux服务器,特别是Web服务器的性能至关重要。通过调整Swap,有时可以越过系统性能瓶颈,节省系统升级费用。 Swap的原理是一个较复杂的问题,需要大量的篇幅来说明。在这里只作简单的介绍,在以后的文章中将和大家详细讨论Swap实现的细节。 众所周知,现代操作系统都实现了“虚拟内存”这一技术,不但在功能上突破了物理内存的限制,使程序可以操纵大于实际物理内存的空间,更重要的是,“虚拟内存”是隔离每个进程的安全保护网,使每个进程都不受其它程序的干扰。 Swap空间的作用可简单描述为:当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用。那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放的空间被临时保存到Swap空间中,等到那些程序要运行时,再从Swap中恢复保存的数据到内存中。这样,系统总是在物理内存不够时,才进行Swap交换。 计算机用户会经常遇这种现象。例如,在使用Windows系统时,可以同时运行多个程序,当你切换到一个很长时间没有理会的程序时,会听到硬盘“哗哗”直响。这是因为这个程序的内存被那些频繁运行的程序给“偷走”了,放到了Swap区中。因此,一旦此程序被放置到前端,它就会从Swap区取回自己的数据,将其放进内存,然后接着运行。

How to swap keys with values in array?

China☆狼群 提交于 2019-11-29 09:21:21
I have array like: array( 0 => 'a', 1 => 'b', 2 => 'c' ); I need to convert it to: array( 'a', 'b', 'c' ); What's the fastest way to swap keys with values? Haim Evgi PHP has the array_flip function which exchanges all keys with their corresponding values, but you do not need it in your case because the arrays are the same. array( 'a', 'b', 'c' ); This array has the keys 0, 1, and 2. Use array_flip() . That will do to swap keys with values. However, your array is OK the way it is. That is, you don't need to swap them, because then your array will become: array( 'a' => 0, 'b' => 1, 'c' => 2 );

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

那年仲夏 提交于 2019-11-29 08:40:56
问题 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??? 回答1: 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