21221

вторник, 26 июля 2016 г.

на завтра

1. память
2. наушники 2–2.5 м
3. Волковка-7... мощность, амперы — на выходе адаптера
4. провод USB — USB диск
5. провод USB — фотоаппарат


провод USB — USB диск


среда, 22 июня 2016 г.

Permutation algorithm with JavaScript

http://www.pixelstech.net/article/1316620955-Permutation-algorithm-with-JavaScript

In secondary school, we have learned permutation. Basically, for n numbers, the number of permutations for these n numbers can be calculated to n! which is called 'n factorial'. But to display all the permutations on the screen, we need to use a recursive function. The problem is how to write this function. 

Next I write a program to display the permutations for n numbers with JavaScript. First, we need to understand that from these n numbers, we can first take any one number from it, and the (n-1) numbers remaining has the similar property as the n numbers, we need to find the permutations for the (n-1) numbers, and from these (n-1) numbers, we take any one of them and there are (n-2) numbers which we need to find the permutations of them. So the whole process will stop when there is only one number left, we will can display the permutations. It is a recursive process. The code can be shown below:

create all permutation of a string

http://www.thatjsdude.com/interview/js1.html#permutation

Question: How would you create all permutation of a string?
Answer: This could be a tough one based on you level of knowledge about algorithm.

function permutations(str){
var arr = str.split(''),
    len = arr.length, 
    perms = [],
    rest,
    picked,
    restPerms,
    next;

    if (len == 0)
        return [str];

    for (var i=0; i<len; i++)
    {
        rest = Object.create(arr);
        picked = rest.splice(i, 1);

        restPerms = permutations(rest.join(''));

       for (var j=0, jLen = restPerms.length; j< jLen; j++)
       {
           next = picked.concat(restPerms[j]);
           perms.push(next.join(''));
       }
    }
   return perms;
}
        
Explanation:
  • Idea: Idea is very simple. We will convert the string to an array. from the array we will pick one character and then permute rest of it. After getting the permutation of the rest of the characters, we will concatenate each of them with the character we have picked.
  • step-1 First copy original array to avoid changing it while picking elements
  • step-2 Use splice to removed element from the copied array. We copied the array because splice will remove the item from the array. We will need the picked item in the next iteration.
  • step-3 [1,2,3,4].splice(2,1) will return [3] and remaining array = [1,2,4]
  • step-4 Use recursive method to get the permutation of the rest of the elements by passing array as string
  • step-5 Finally, concat like a+permute(bc) for each