swap

(Any Language) Find all permutations of elements in a vector using swapping

╄→尐↘猪︶ㄣ 提交于 2019-12-02 18:48:33
问题 I was asked this question in a Lab session today. We can imagine a vector containing the elements 1 ... N - 1, with a length N. Is there an algorithmic (systematic) method of generating all permutations, or orders of the elements in the vector. One proposed method was to swap random elements. Obviously this would work provided all previously generated permutations were stored for future reference, however this is obviously a very inefficient method, both space wise and time wise. The reason

Javascript change div content upon a click

和自甴很熟 提交于 2019-12-02 13:53:30
问题 I'm pulling a content from PHP array and I have a situation like this: <div class="weight-display"> <span>04/25/2011</span> <span>100lbs</span> <span>Edit</span> <a href="http://foo.com">Delete</span> </div> <div class="weight-display"> <span>04/27/2011</span> <span>150lbs</span> <span>Edit</span> <a href="http://foo.com">Delete</span> </div> etc... Now when somebody clicks on Edit within, let's say, first div where weight is 100lbs, I just need that "div" to change and to have input field

How can I swap two values of an array in c#?

扶醉桌前 提交于 2019-12-02 13:43:36
I have an array of int containing some values starting from index 0. I want to swap two values for example value of index 0 should be swapped with the value of index 1. How can I do this in the c# array? You could create an extension method that would work for any array public static void SwapValues<T>(this T[] source, long index1, long index2) { T temp = source[index1]; source[index1] = source[index2]; source[index2] = temp; } If you really only want to swap, you can use this method: public static bool swap(int x, int y, ref int[] array){ // check for out of range if(array.Length <= y ||

Python Swap Function

你离开我真会死。 提交于 2019-12-02 13:40:38
I'm having a hard time expressing this in Python. This is the description of what needs to be done. swap_cards: (list of int, int) -> NoneType swap_cards([3, 2, 1, 4, 5, 6, 0], 5) [3, 2, 1, 4, 5, 0, 6] swap_cards([3, 2, 1, 4, 5, 6, 0], 6) [0, 2, 1, 4, 5, 6, 3]` I've created 2 examples, but I don't know how to start the body of the function. Sounds like some index notation is required here: >>> def swap_cards(L, n): ... if len(L) == n + 1: ... L[n], L[0] = L[0], L[n] ... return L ... L[n], L[n+1] = L[n+1], L[n] ... return L ... >>> swap_cards([3, 2, 1, 4, 5, 6, 0], 5) [3, 2, 1, 4, 5, 0, 6] >>>

(Any Language) Find all permutations of elements in a vector using swapping

旧巷老猫 提交于 2019-12-02 11:57:11
I was asked this question in a Lab session today. We can imagine a vector containing the elements 1 ... N - 1, with a length N. Is there an algorithmic (systematic) method of generating all permutations, or orders of the elements in the vector. One proposed method was to swap random elements. Obviously this would work provided all previously generated permutations were stored for future reference, however this is obviously a very inefficient method, both space wise and time wise. The reason for doing this by the way is to remove special elements (eg elements which are zero) from special

Swap Elements in a 2D Array C#

笑着哭i 提交于 2019-12-02 11:48:31
问题 I am using C#, I'm fairly new to the language but I have used similar languages before so I understand basic syntax. I have a 2D array of type Object . ( X represents what value and Y is what record) It stores two strings in columns 0 and 1 and a MessageBoxIcon in 2 and a MessageBoxButtons in 3. I would like the ability to swap two records. I populate a listBox with column 1 every time a change is made to the array. (using a loop) I am happy with this system. I have placed + and - buttons to

Assembly - Swap function - Why it will not work?

会有一股神秘感。 提交于 2019-12-02 11:17:25
I need to create a function that swaps the value of &x with the value of &y (meaning swap *(&y) and *(&x). Swap: push EBP mov EBP,ESP mov EBX, [EBP+12] ; ebx = *x mov EAX, DWORD [EBX] ;eax = ebx = *x mov DWORD [EBP-4], EAX ; [ebp-4] = eax =*x mov EDX, [EBP+8] ; edx = *y mov EAX, DWORD [EDX] ; eax = *edx = *y mov DWORD [EBX], EAX ; ebx = eax = *y mov EAX, DWORD [EBP-4] ; eax = *x mov DWORD [EDX], EAX ; edx = *x pop EBP ; ebx = *y and edx = *x ret I call it like this: // call Swap push x push y call swap I don't understand why it's not working. I added comments that explain my understanding of

Rationale behind member function swap

天大地大妈咪最大 提交于 2019-12-02 10:17:44
In the standard library, if a class type has a specialized swap algorithm, then it will have a member function swap and a free function swap that simply forwards to the member function. I don't quite get the rationale behind having both of them (and thus code duplication) but not just the free function one. Note that, when I say the free function, I take to mean the specialized free swap function, not the generic std::swap function template. Such a specialized function may have privileged access to the relevant class by being a friend of it. I have two points to support just leaving the free

Javascript: How can I swap elements of an array of objects (by reference, not index)?

点点圈 提交于 2019-12-02 06:20:32
问题 I have an array of objects a=[ {v:0}, {v:1}, {v:2}, {v:3} ] ; I do not have the index into the array, but I do have references to the 2 values I want to swap s1=a[2] ; s2 = a[3] ; How do I use these references to swap the elements of the actual array? [s1,s2] = [s2,s1] ; // only swaps s1 and s2, NOT elements of the array // a is unchanged 回答1: If you have the reference you can safely retrieve the index by Array.indexOf() : a.indexOf(myReference) // returns the index of the reference or -1

Javascript change div content upon a click

社会主义新天地 提交于 2019-12-02 06:12:59
I'm pulling a content from PHP array and I have a situation like this: <div class="weight-display"> <span>04/25/2011</span> <span>100lbs</span> <span>Edit</span> <a href="http://foo.com">Delete</span> </div> <div class="weight-display"> <span>04/27/2011</span> <span>150lbs</span> <span>Edit</span> <a href="http://foo.com">Delete</span> </div> etc... Now when somebody clicks on Edit within, let's say, first div where weight is 100lbs, I just need that "div" to change and to have input field instead of simple text where weight is (while others will remain the same) and to be like this: <div