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:
Показаны сообщения с ярлыком javascript. Показать все сообщения
Показаны сообщения с ярлыком javascript. Показать все сообщения
среда, 22 июня 2016 г.
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
четверг, 16 июня 2016 г.
Math.random
http://javascript.ru/math.random
Синтаксис
var randomNumber = Math.random(); |
Описание, примеры
Заметим, что числа в яваскрипт хранятся в формате с плавающей точкой IEEE 754 с округлением в сторону ближайшего четного. Поэтому иногда, в очень редких случаях, возможна генерация обычно исключенной верхней границы.
Пример: Cлучайное число от 0(включительно) до 1
1 | function getRandom() |
2 | { |
3 | return Math.random(); |
4 | } |
Пример: Случайное число между min и max
1 | function getRandomArbitary(min, max) |
2 | { |
3 | return Math.random() * (max - min) + min; |
4 | } |
Пример: Случайное целое между min и max
1 | // использование Math.round() даст неравномерное распределение! |
2 | function getRandomInt(min, max) |
3 | { |
4 | return Math.floor(Math.random() * (max - min + 1)) + min; |
5 | } |
среда, 20 января 2016 г.
Подписаться на:
Сообщения (Atom)

