Swap, anagrams, occurences

Fixed sorting title for strings.
This commit is contained in:
Angelos Chalaris
2017-12-06 23:50:23 +02:00
parent e86bf2ffa3
commit f40115ff3b
5 changed files with 67 additions and 1 deletions

View File

@ -8,6 +8,8 @@
## Contents ## Contents
* [Anagrams of string (with duplicates)](#anagrams-of-string-with-duplicates)
* [Count occurences of a value in array](#count-occurences-of-a-value-in-array)
* [Even or odd number](#even-or-odd-number) * [Even or odd number](#even-or-odd-number)
* [Fibonacci array generator](#fibonacci-array-generator) * [Fibonacci array generator](#fibonacci-array-generator)
* [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd)
@ -15,10 +17,36 @@
* [Random number in range](#random-number-in-range) * [Random number in range](#random-number-in-range)
* [Randomize order of array](#randomize-order-of-array) * [Randomize order of array](#randomize-order-of-array)
* [RGB to hexadecimal](#rgb-to-hexadecimal) * [RGB to hexadecimal](#rgb-to-hexadecimal)
* [Sort characters in string alphabetical](#sort-characters-in-string-alphabetical) * [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical)
* [Sum of array of numbers](#sum-of-array-of-numbers) * [Sum of array of numbers](#sum-of-array-of-numbers)
* [Swap values of two variables](#swap-values-of-two-variables)
* [Unique values of array](#unique-values-of-array) * [Unique values of array](#unique-values-of-array)
### Anagrams of string (with duplicates)
Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters.
Use `map()` to combine the letter with each partial anagram, then `reduce()` to combine all anagrams in one array.
Base cases are for string `length` equal to `2` or `1`.
```js
var anagrams = s => {
if(s.length <= 2) return s.length === 2 ? [s, s[1] + s[0]] : [s];
return s.split('').reduce( (a,l,i) => {
anagrams(s.slice(0,i) + s.slice(i+1)).map( v => a.push(l+v) );
return a;
}, []);
}
```
### Count occurences of a value in array
Use `filter()` to create an array containing only the items with the specified value, count them using `length`.
```js
var countOccurences = (arr, value) => arr.filter(v => v === value).length;
```
### Even or odd number ### Even or odd number
Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator. Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator.
@ -106,6 +134,14 @@ var sum = arr =>
arr.reduce( (acc , val) => acc + val, 0); arr.reduce( (acc , val) => acc + val, 0);
``` ```
### Swap values of two variables
Use array destructuring to swap values between two variables.
```js
[varA, varB] = [varB, varA];
```
### Unique values of array ### Unique values of array
Use `reduce()` to accumulate all unique values in an array. Use `reduce()` to accumulate all unique values in an array.

View File

@ -0,0 +1,16 @@
### Anagrams of string (with duplicates)
Use recursion.
For each letter in the given string, create all the partial anagrams for the rest of its letters.
Use `map()` to combine the letter with each partial anagram, then `reduce()` to combine all anagrams in one array.
Base cases are for string `length` equal to `2` or `1`.
```js
var anagrams = s => {
if(s.length <= 2) return s.length === 2 ? [s, s[1] + s[0]] : [s];
return s.split('').reduce( (a,l,i) => {
anagrams(s.slice(0,i) + s.slice(i+1)).map( v => a.push(l+v) );
return a;
}, []);
}
```

View File

@ -0,0 +1,7 @@
### Count occurences of a value in array
Use `filter()` to create an array containing only the items with the specified value, count them using `length`.
```js
var countOccurences = (arr, value) => arr.filter(v => v === value).length;
```

View File

@ -0,0 +1,7 @@
### Swap values of two variables
Use array destructuring to swap values between two variables.
```js
[varA, varB] = [varB, varA];
```