My first question was not really clear, sorry about that. So, I know about "split" method in JavaScript and now I want to add number to random array on a position that I can choose inside array, but without "split". Question is for understanding how algorithm "inside" "split" method is working. I wrote random array, so my idea is to use loop second times for a random array, create empty array add that number to position, then number that was before the position of our new number add to empty array and then got confused a bit.  var array1 = []; for (var k= 0, t=100; k&lt;t; k++){     array1.push(Math.round(Math.random() * 300)) };  console.log(array1)
6261
I am looking for a JavaScript array insert method, in the style of:  arr.insert(index, item)   Preferably in jQuery, but any JavaScript implementation will do at this point.
23352
I am writing a few rather large pseudocode functions using algorithm and algorithmic, i.e. my code looks something like:  \begin{algorithm}                      \caption{Coolest Algorithm ever}           \label{findme}                           \begin{algorithmic} [1]                   % enter the algorithmic environment \REQUIRE    Here are a few variables \\             variable 2 \\             variable 3  \ENSURE     this is the output \\             output 2  \STATE $some cool code here$ \STATE $some cool code here$ \STATE $some cool code here$ \STATE $some cool code here$ \end{algorithmic} \end{algorithm}   Unfortunately this code does not fit on one page and I am definitely not able to split it into some subfunctions or so (I already did that as far as possible).  So now I need to split up the algorithm into two pages, but I have no idea how.  The easiest way I can imagine is just to start a new algorithm on the new page, but this time letting the line counter start at line 35 or so instead of 1. But how can I do that?  Please not that there is already a similar question here on this site, but it deals with the package algorithmicx.
768
I'm someone who writes code just for fun and haven't really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me.  I was reading an article about JavaScript, which apparently supports bitwise operations. I keep seeing this operation mentioned in places, and I've tried reading about to figure out what exactly it is, but I just don't seem to get it at all. So what are they? Clear examples would be great! :D  Just a few more questions - what are some practical applications of bitwise operations? When might you use them?
77935
How do you split a string?  Lets say i have a string "dog, cat, mouse,bird"  My actual goal is to insert each of those animals into a listBox, so they would become items in a list box.  but i think i get the idea on how to insert those items if i know how to split the string. or does anyone know a better way to do this?  im using asp c#
93369
I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
59216
There are two different ways to create an empty object in JavaScript:  var objectA = {} var objectB = new Object()   Is there any difference in how the script engine handles them? Is there any reason to use one over the other?  Similarly it is also possible to create an empty array using different syntax:  var arrayA = [] var arrayB = new Array()
4926
The JavaScript  function returns a random value between 0 and 1, automatically seeded based on the current time (similar to Java I believe).  However, I don't think there's any way to set you own seed for it.  How can I make a random number generator that I can provide my own seed value for, so that I can have it produce a repeatable sequence of (pseudo)random numbers?
5654
What is the difference between using  on the array element as opposed to using ?   For example:  myArray = ['a', 'b', 'c', 'd'];  delete myArray[1]; //  or myArray.splice (1, 1);   Why even have the splice method if I can delete array elements like I can with objects?
42769
I read this in a math textbook, stated as obvious fact. I cannot wrap my head around why this works. I understood the division lemma, and also got the algorithm to work: but the proof of why the algorithm works, just said "because of the equation" that I put in the question.  Could you please elaborate on why it works?
95854
I saw this question on Reddit, and there were no positive solutions presented, and I thought it would be a perfect question to ask here. This was in a thread about interview questions:  Write a method that takes an int array of size m, and returns (True/False) if the array consists of the numbers n...n+m-1, all numbers in that range and only numbers in that range. The array is not guaranteed to be sorted. (For instance, {2,3,4} would return true. {1,3,1} would return false, {1,2,4} would return false. The problem I had with this one is that my interviewer kept asking me to optimize (faster O(n), less memory, etc), to the point where he claimed you could do it in one pass of the array using a constant amount of memory. Never figured that one out.  Along with your solutions please indicate if they assume that the array contains unique items. Also indicate if your solution assumes the sequence starts at 1. (I've modified the question slightly to allow cases where it goes 2, 3, 4...) edit: I am now of the opinion that there does not exist a linear in time and constant in space algorithm that handles duplicates. Can anyone verify this? The duplicate problem boils down to testing to see if the array contains duplicates in O(n) time, O(1) space. If this can be done you can simply test first and if there are no duplicates run the algorithms posted. So can you test for dupes in O(n) time O(1) space?
12129
I needed an algorithm to generate all possible partitions of a positive number, and I came up with one (posted as an answer), but it's exponential time.  The algorithm should return all the possible ways a number can be expressed as the sum of positive numbers less than or equal to itself. So for example for the number 5, the result would be:   5 4+1 3+2 3+1+1 2+2+1 2+1+1+1 1+1+1+1+1   So my question is: is there a more efficient algorithm for this?  EDIT: Question was titled "Sum decomposition of a number", since I didn't really know what this was called.  that they were called "partitions," so I edited the question title accordingly.
108260
So I have been tasked to make an array without duplicated values from another existing array. So I did it, but I want to know is there some other better way to do that. Example input/output: Input: 10, 15, 10, 5, 1, 3 Output: 10, 15, 5, 1, 3 So here is my code. #include &lt;stdio.h&gt;  int main(void) {   const int MAX_ARRAY_SIZE = 5;    int m[MAX_ARRAY_SIZE], p[MAX_ARRAY_SIZE];     for(int i = 0; i &lt; MAX_ARRAY_SIZE; i++) {     printf(&quot;Enter number: &quot;);     scanf(&quot;%d&quot;,&amp;m[i]);   }   int k = 0;   int dup = 0;   for(int i =0; i &lt; MAX_ARRAY_SIZE; i++) {     for(int j = i +1; j &lt;MAX_ARRAY_SIZE; j++) {         if(m[i] == m[j]) {             dup = 1;         }     }     if(dup != 1) {       p[k++] = m[i];     }     dup = 0;   }    printf(&quot;The new array without repeated values\n&quot;);   for(int i = 0; i &lt; k; i++) {     printf(&quot;%d\n&quot;,p[i]);   }    return 0; }  I'm not sure if this is the right and simple way I do that. I want some suggestions. Thanks in advance. :)
95963
Let's say that I have two arrays (in Java),  int[] numbers; and int[] colors;  Each ith element of numbers corresponds to its ith element in colors. Ex, numbers = {4,2,1}      colors = {0x11, 0x24, 0x01}; Means that number 4 is color 0x11, number 2 is 0x24, etc.  I want to sort the numbers array, but then still have it so each element matches up with its pair in colors.  Ex. numbers = {1,2,4};     colors = {0x01,0x24,0x11};  What's the cleanest, simplest way to do this? The arrays have a few thousand items, so being in place would be best, but not required. Would it make sense to do an Arrays.sort() and a custom comparator? Using library functions as much as possible is preferable.  Note: I know the "best" solution is to make a class for the two elements and use a custom comparator. This question is meant to ask people for the quickest way to code this. Imagine being at a programming competition, you wouldn't want to be making all these extra classes, anonymous classes for the comparator, etc. Better yet, forget Java; how would you code it in C?
110046
Even though this is a 3rd-grade math problem, people found it extremely hard. Any people have a solution, or algorithm is welcome. I'll try make a program base on the algorithm and see if it's correct. And, people are welcome to provide the best solution (least loop, recursion)  Enough said, here's the original: Fill in the blank the number from 1-9 to complete the equation. (probably distinct numbers each blank)    PS: Help me with tags. I'm not sure which tags should I use.  EDIT 1: After a few shortened, here's what I've got:  a + d - f + 12*e + 13*b/c + g*h/j = 87.   So, as you see, the priority does matter.  EDIT 2: Maybe my description is not clear enough, but I think it require to use every number   Like this: an array from [1:9], each fill in the blank remove one component. As Martigan point out, it's like: fill in the blanks using once and only once each number from 1 to 9. Simple math here: There are 9*8*7*6*5*4*3*2 = 362880 possibilitities. (You may correct this simple math if I'm wrong).  And b mod c should = 0 (as only integers appear.)  EDIT 3: I just asked my mom (who's an elementary teacher). She's confirmed that 3rd grade has studied about mathematic priority, so sorry Hagen von Eitzen. Your answer is half right, she'd give you an 7/10.  PS2: People, please share your wisdom and provide the math algorithm.  Sorry for my bad English.
36042
Duplicate:    I want an pseudo random number generator that can generate numbers with no repeats in a random order. For example: random(10) might return 5, 9, 1, 4, 2, 8, 3, 7, 6, 10 Is there a better way to do it other than making the range of numbers and shuffling them about, or checking the generated list for repeats?  Edit: Also I want it to be efficient in generating big numbers without the entire range.  Edit: I see everyone suggesting shuffle algorithms. But if I want to generate large random number (1024 byte+) then that method would take alot more memory than if I just used a regular RNG and inserted into a Set until it was a specified length, right? Is there no better mathematical algorithm for this.
12489
What is the best or most concise method for returning a string repeated an arbitrary amount of times?  The following is my best shot so far:  function repeat(s, n){     var a = [];     while(a.length &lt; n){         a.push(s);     }     return a.join(''); }
25054
Problem Statement: We are given a $2-1$ function $f:\{0,1\}^{n}\to\{0,1\}^{n}$ such that: there is a secret string $s\in\{0,1\}^{n}$ such that: $f(x)=f(x\oplus s)$. Challenge: find $s$.     Simon's algorithm says:         Set up a random superposition $$\frac{1}{\sqrt{2}}|r\rangle + \frac{1}{\sqrt{2}}|r\oplus s\rangle$$   Fourier sample to get a random $y$: $$y.s=0\ (\text{mod 2})$$   Repeat steps $n-1$ times to generate $n-1$ linear equations in $s$.         Solve for $s$.   I don't understand steps (1) and (2). Why and how exactly do we set up the random superposition? How does it help? Also, in step (2), what does the dot operator (in $y.s$) stand for? Bit-wise multiplication?