FAQ Database Discussion Community


Can we improve on my code to swap adjacent array elements in Scala?

arrays,scala,group,swap
I'm learning Scala by working the exercises from the book "Scala for the Impatient". One exercise asks that: Write a loop that swaps adjacent elements of an array of integers. For example, Array(1, 2, 3, 4, 5) becomes Array(2, 1, 4, 3, 5) I did it in 3 different ways,...

Swapping indices with values in a Python list?

python,list,python-3.x,swap,indices
(No, this is not a homework assignment nor a contest, even though it might look like one.) I have a list A in Python that contains the numbers range(0, len(A)). The numbers are not in order, but all of them exist in the list. I'm looking for a simple way...

No swap space detected while installing WLS 12c on AIX 7.1 (Installation Crashes)

installation,weblogic,swap,aix
I'm trying to install oracle weblogic server 12c on an AIX server. The installer detects that there is no swap space on the system, but that is not true; the system has 16 GB of swap memory. You can see that from the screenshots. Launching the Weblogic installer. Checking System...

Swap Qt Widgets

c++,qt,polymorphism,swap,qobject
I am running into a problem. I want to swap two Qt widgets: void swap(QMainWindow **w1, QMainWindow **w2) { QMainWindow *w3; w3 = *w1; *w1 = *w2; *w2 = w3; } However if I have widget1 of type MainWindow1* and widget2 of type MainWindow2* and I call swap(&widget1, &widget2); I...

Change the text of several elements on a timer with onClick

javascript,element,swap
I have a button that onClick starts a timer that calls a function and passes the counter. I used an alert box to see that I was getting counter as 0. If counter is 0, it should load the table with the text (it skips it) it goes to the...

How do you swap first half of an array with second half with an unknown size [closed]

c++,arrays,string,swap
I am trying to swap the first half of an array of some string obtained by the user with the second half without knowing the size of the array. Here is my attempt. #include <iostream> #include <cstring> using namespace std; int main() { int size; char string1[size]; char *string1Ptr; cout...

Simple JavaScript Swap function

javascript,image,swap
I'm having difficulty in correcting the syntax error for image swap. I would appreciate if you can help me out as below? function swap(i, s) { var d = document.images; d ? d[i].src=s : null; } ...

Switch numbers in string

javascript,jquery,swap
I have a string with space-separated unique numbers as following: "2 4 13 14 28 33" Need a quick and efficient way to switch a pair of them in form: switchNumbers(2, 28) // result: "28 4 13 14 2 33" I could split the string and search for values, but...

swap table via select box option

javascript,jquery,twitter-bootstrap,table,swap
Hi there guys and girls, Looking to swap multiple tables with a javascript function, each one I try breaks horribly. I'd like the first table to show on page load, and then the rest to load as a different option is selected. Any help would be greatly appreciated! Here's the...

how swap lines with awk with only a single pass and limited memory use?

awk,swap,gawk
in a previous post, this answer was shown: answer user2138595, though beautiful , the problem is that you should read the input file twice. I wish to make a GNU awk script to read input only once. cat swap_line.awk you get BEGIN { if(init > end){ exit 1; } flag...

Rcpp swap function with NumericVector

r,swap,rcpp
As I was exploring Rcpp I came to realization that the following swap function // swap.cpp #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] void swap(NumericVector x) { double tmp = x[0]; x[0] = x[1]; x[1] = tmp; } does not perform the swap when passed an integer vector. For example,...

Flash AS3: Swapping Parts within Movieclips

actionscript-3,flash,swap,movieclip,graphic
I have a Movieclip (level 1, MC) with around 10 frames. There is a different movieclip (level 2, Move Jump etc animation) on each frame. Inside each of the level 2 movieclips, there are about 30 movieclip symbols (level 3, Head Arm etc) each spamming about 100 frames with around...

A function to swap two variables

c,function,pointers,swap
I am a newbie and learning the pointer now. I encountered this problem: function 1: void swap(int *x, int *y) { int *p; *p=*x;*x=*y;*y=*p; } function 2: void swap(int *x, int *y){ *x=*x+*y;*y=*x-*y;*x=*x-*y; } I know the function 2 can work well to swap two variables but function 1 cannot....

Why is my swap method not working?

java,swap
I have successfully written a Bubblesort class. Now I am trying to break the functions apart to make the code cleaner. Here is the working code: public class BubbleSortTest { public static void main(String[] args) { int[] array = {93, 60, 65, 45, 50}; bubbleSort(array); printArray(array); } public static int[]...

How to swap a 16bit Value with x86 Assembler?

assembly,x86,swap
Hi I want to swap a 16 bit value. In in an other question someone suggested the following solution. But this only works with immidiate values, mov eax, 0AABBCCDDh xchg ah, al ror eax, 16 xchg ah, al. But this only works with immidiate values. Is there a solution, where...

My swap program is not working using Template?

c++,templates,swap
I'm using Template keyword to run simple swap program, Kindly help me why my program is not working out? #include<iostream> using namespace std; template<typename T> void Swap(T m, T n) { T temp; temp = m; m = n; n = temp; } int main() { int i = 5,...

Safely moving objects that run a lambda in a member thread that accesses other members

c++,c++11,lambda,move,swap
I have a class that has an std::thread member variable which runs a lambda function that depends on some other member variable. Let me give an example: struct C { C() { thread_ = std::thread([this]() { running_ = true; while (running_) { ... } }); } C(C&& rv) { swap(rv);...

Memory: Stack and Swap

linux,memory,stack,heap,swap
When there isn't enough RAM, dynamically allocated variables on the heap can take advantage of swap space on the disk (albeit causing performance degradations). My question is if the stack in memory can take advantage of the swap space as well. For example, the following program places a large array...

Trouble swapping nodes in doubly linked list

c,list,data-structures,linked-list,swap
I am trying to swap two nodes in a doubly linked list using C. It is working if I pass in some values, for example the head and tail of the list, and some in between. However, on others the value of one seems to be overwritten by the other...

C++ swap/sort algorithm

sorting,swap
Given the following: Container A; //contains n data entries vector<int> B; //indexes of A that need to be swapped vector<int> C; // Where the entry needs to be moved to, a randomly sorted version of B //B.size() <= A.size() - not all may be swapped //C.size() = B.size() swap(container &X,...

Swap key and value in a map in fsharp

dictionary,f#,key,swap,value
How to create a new map that is similar to the original one, but with swapped keys and values in Fsharp? For example, I have this let map1 = [("A", "1"); ("B", "2"); ("C", "3");] |> Map.ofList and want to get this: let map2 = [("1", "A"); ("2", "B"); ("3",...

How to use the qi::hold[] Parser directive. (Issue with attribute type for boost::swap)

c++,boost,swap,boost-spirit,boost-spirit-qi
I have a parser that parses into a boost::variant<int, double, std::string> namespace qi = boost::spirit::qi; namespace ascii = boost::spirit::ascii; using namespace std; typedef map<string, boost::variant<int, double, string> > namevalue; typedef pair<string, boost::variant<int, double, string> > namevaluepair; template <typename Iterator> struct keys_and_values2 : grammar<Iterator, namevalue(), ascii::blank_type> { keys_and_values2() : keys_and_values2::base_type(start) {...

Problems ordering a linked list in C

c,list,pointers,swap
I got issues trying to order a linked list from its min to its max, the code is: struct nodo *orderList (struct nodo *p) { struct nodo *head=p; struct nodo *min=p; int *tmp=NULL; while (min != NULL) { p = min; // p = p->succ (not necessary i think) while...

How to swap the lines in a file that match a condition using Shell script

unix,match,condition,swap,lines
I have a file in which 28th character of every line is either "A" or "D". I want to swap the lines in such a way that the first line of the file should have the 28th character as "D" and the second line of the file should have 28...

I made a for loop to swap two indiv char variables in a string

java,string,random,char,swap
I am supposed to write a method that scrambles a word, switching two letters in a word that is not the first or last character. I've initialized i and j as random integers between positions 1 and str.length() -1. Is there a reason why this loop would not print the...

Javascript check if list is sorted after one swap

javascript,algorithm,sorting,swap
I want to check if a list can be sorted by performing at most one swap operation, and I want to return true if it does. Otherwise, I want to return false. For example, the input A = [1,2,3,4,5] should result in true because it is already sorted. A =...

Cannot invoke 'subscript' error

ios,swift,swap
I am trying to utilize a function borrowed from this blog. Here is that function: func shuffleArray<T>(array: Array<T>) -> Array<T> { for var index = array.count - 1; index > 0; index-- { // Random int from 0 to index-1 var j = Int(arc4random_uniform(UInt32(index-1))) // Swap two array elements //...

Notepad++ how to swap characters in a string

character,notepad++,swap
I have a computer generated text file. I need to swap positions of certain entries. These entries are always 4 characters long and separated from the rest by semicolons. The 4th character needs to become the first character. For example: ;1234; has to become: ;4123; Note: There's a lot of...

How to swap two dates in swift

arrays,swift,sorting,swap
Trying to swap two dates in Swift. It currently gives me an error saying: Cannot subscript a value of type '[Mission]' with an index of type 'Int' func sort (mission: [Mission]) -> Bool { for (var i = 0; i < mission.count; i++) { println(mission[i].createdAt) if mission[i].createdAt.timeIntervalSince1970 > mission[i+1].createdAt.timeIntervalSince1970 {...

How to swap two image views using drag and drop in Android?

android,drag-and-drop,swap
I've been trying to implement a very simple drag and drop in Android, where the dragged item swaps positions with the item it was dropped on. I've successfully implemented the onLongClick and onDrag listeners. When I drag and drop an item it replaces the item it was dropped on, but...

Java - swap in the array [closed]

java,arrays,swap
I want to swap 2 values in an array... This is my code: static void swap (Integer[] input, int i, int j) { /* * Assure indices are allright! */ if (i < 0 || i >= input.length) throw new IllegalArgumentException("Incorrect i!"); if (j < 0 || j >= input.length)...

Adapting std::swap interface to swap n bytes

c++,swap
I have a C function to swap an arbitrary number of bytes: void my_swap(void* a, void* b, long n); I've done a little benckmark with several types and I've seen that std::swap outperform my_swap. So, I'd like to wrap std::swap with my_swap interface, but I can't figure out how to...

Python list of objects, get all 'orientations' of attributes

python,list,object,attributes,swap
I have a list of objects(rectangles). Each object has 2 attributes(height and width). I would like to get all 'orientations'(not sure how to call it exactly) of this list, so all the 2^n(for a list of n rectangles) orientations in which height and width of the rectangles are (possibly) swapped....

Memory usage issues with VPS (ubuntu): MySQL process dies

mysql,linux,memory-leaks,swap,digital-ocean
I'm running a VPS, with specs: Ubuntu 12.04.5 LTS (GNU/Linux 3.13.0-32-generic x86_64) 512mb RAM 1 CPU 20gb SSD If you're wondering it's a DigitalOcean droplet. It's running TS3, LAMP (with wordpress), OpenVPN, BYOBU, and OwnCloud. Now my problem is with mySQL dying on me after like 30m to 1hour. Usually...

Swap and Return Char Array from the Function

c++,arrays,reference,char,swap
I know there are many questions related to my this question but still facing problem. I'm using the function to swap array mean when I input ABCD it shoudl return DCBA. Code is below check where is problem. Is there a way to pass second empty array as reference? Swap...

Swap values in a array

java,arrays,multidimensional-array,swap
I have an array like this: item[0][0] = 1; item[0][1] = 20; item[1][0] = 3; item[1][1] = 40; item[2][0] = 9; item[2][1] = 21; (...) I want to swap these "values" like: int[] aux = item[0]; item[0] = item[1]; item[1] = aux; But this isn't working since i think that's...

AWK: Need to swap values between two columns if the left one is smaller than the right one

table,awk,sed,swap
So what I've got so far is a Bed-Table file and the problem with this file is, that in some rows the value in column 2 is bigger than the value in column 3. Is there any one liner in awk, which would swap these two values, so that the...

Why does Python swaps variable ids when swaps variables

python,swap,identify
I thought every object take an id based on key. >>> a = 10 >>> b = 20 >>> id(a) 1876869280 >>> id(b) 1876869440 >>> a,b=b,a >>> id(a) 1876869440 >>> id(b) 1876869280 When swap the variables, their ids are swapped too. If every object has a unique id, then why...

How can I know which process is using swap?

linux,memory,swap
There is lots of memory avaiable(about 4G) but swap is used(200+M) in my fedora box. I wonder which process is using swap. How can I know it. ps and top only show the memory usage. Thanks in advance....