From f40115ff3beb9bb0c90ce657c3b3da3b26ad2900 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 6 Dec 2017 23:50:23 +0200 Subject: [PATCH] Swap, anagrams, occurences Fixed sorting title for strings. --- README.md | 38 ++++++++++++++++++- .../anagrams-of-string-(with-duplicates).md | 16 ++++++++ .../count-occurences-of-a-value-in-array.md | 7 ++++ ...rt-characters-in-string-(alphabetical).md} | 0 snippets/swap-values-of-two-variables.md | 7 ++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 snippets/anagrams-of-string-(with-duplicates).md create mode 100644 snippets/count-occurences-of-a-value-in-array.md rename snippets/{sort-characters-in-string-alphabetical.md => sort-characters-in-string-(alphabetical).md} (100%) create mode 100644 snippets/swap-values-of-two-variables.md diff --git a/README.md b/README.md index fe47fbade..958fc1722 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ ## 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) * [Fibonacci array generator](#fibonacci-array-generator) * [Greatest common divisor (GCD)](#greatest-common-divisor-gcd) @@ -15,10 +17,36 @@ * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) * [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) +* [Swap values of two variables](#swap-values-of-two-variables) * [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 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); ``` +### Swap values of two variables + +Use array destructuring to swap values between two variables. + +```js +[varA, varB] = [varB, varA]; +``` + ### Unique values of array Use `reduce()` to accumulate all unique values in an array. diff --git a/snippets/anagrams-of-string-(with-duplicates).md b/snippets/anagrams-of-string-(with-duplicates).md new file mode 100644 index 000000000..9c13eed58 --- /dev/null +++ b/snippets/anagrams-of-string-(with-duplicates).md @@ -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; + }, []); +} +``` diff --git a/snippets/count-occurences-of-a-value-in-array.md b/snippets/count-occurences-of-a-value-in-array.md new file mode 100644 index 000000000..6851c33bf --- /dev/null +++ b/snippets/count-occurences-of-a-value-in-array.md @@ -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; +``` diff --git a/snippets/sort-characters-in-string-alphabetical.md b/snippets/sort-characters-in-string-(alphabetical).md similarity index 100% rename from snippets/sort-characters-in-string-alphabetical.md rename to snippets/sort-characters-in-string-(alphabetical).md diff --git a/snippets/swap-values-of-two-variables.md b/snippets/swap-values-of-two-variables.md new file mode 100644 index 000000000..d1cd017b9 --- /dev/null +++ b/snippets/swap-values-of-two-variables.md @@ -0,0 +1,7 @@ +### Swap values of two variables + +Use array destructuring to swap values between two variables. + +```js +[varA, varB] = [varB, varA]; +```