From eab9fd58ac35dacec324bfa247555165a07d4d0e Mon Sep 17 00:00:00 2001 From: Bill Mei Date: Mon, 25 Dec 2017 18:43:55 -0800 Subject: [PATCH 1/5] User Fisher-Yates shuffle implementation --- snippets/shuffle.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/snippets/shuffle.md b/snippets/shuffle.md index 27d7fd783..c23d5296a 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -1,10 +1,20 @@ ### shuffle -Randomizes the order of the values of an array. +Randomizes the order of the values of an array, in place. -Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. +Users the Fisher-Yates reorder the elements, based on the [Lodash implementation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js). ```js -const shuffle = arr => arr.sort(() => Math.random() - 0.5); +const shuffle = (arr = []) => { + let i = -1; + const lastIndex = arr.length - 1; + while (++i < arr.length) { + const rand = i + Math.floor(Math.random() * (lastIndex - i + 1)); + const value = arr[rand]; + arr[rand] = arr[i]; + arr[i] = value; + } + return arr +}; // shuffle([1,2,3]) -> [2,3,1] ``` From 54a3062d67285b93f6b79ae1701a273d55d95c8c Mon Sep 17 00:00:00 2001 From: Travis CI Date: Tue, 26 Dec 2017 13:17:42 +0000 Subject: [PATCH 2/5] Travis build: 305 --- README.md | 5 +++-- docs/index.html | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 25eb329d1..980acdc66 100644 --- a/README.md +++ b/README.md @@ -2093,12 +2093,13 @@ const toSnakeCase = str =>{ str && str.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) .map(x => x.toLowerCase()) .join('_'); +} // toSnakeCase("camelCase") -> 'camel_case' // toSnakeCase("some text") -> 'some_text' // toSnakeCase("some-javascript-property") -> 'some_javascript_property' // toSnakeCase("some-mixed_string With spaces_underscores-and-hyphens") -> 'some_mixed_string_with_spaces_underscores_and_hyphens' -// toKebabCase("AllThe-small Things") -> "all_the_smal_things" -// toKebabCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html" +// toSnakeCase("AllThe-small Things") -> "all_the_small_things" +// toSnakeCase('IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML') -> "i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html" ``` [⬆ back to top](#table-of-contents) diff --git a/docs/index.html b/docs/index.html index 52920974d..edd49804d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1326,12 +1326,13 @@ For more detailed explanation of this Regex,

truncateString

Truncates a string up to a specified length.

From 0b0d4392149390ece3784e728669622aa5c2c82c Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 26 Dec 2017 14:23:44 +0100 Subject: [PATCH 3/5] fix typo --- snippets/shuffle.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/shuffle.md b/snippets/shuffle.md index c23d5296a..99a0e25d6 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -2,7 +2,7 @@ Randomizes the order of the values of an array, in place. -Users the Fisher-Yates reorder the elements, based on the [Lodash implementation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js). +Uses the Fisher-Yates algoritm to reorder the elements of the array, based on the [Lodash implimentation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js) ```js const shuffle = (arr = []) => { From 5a6ba2a66b81abc86a258ed73d740ce428163623 Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 26 Dec 2017 14:24:04 +0100 Subject: [PATCH 4/5] Shorten algorithm --- snippets/shuffle.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/snippets/shuffle.md b/snippets/shuffle.md index 99a0e25d6..54687f67f 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -5,16 +5,13 @@ Randomizes the order of the values of an array, in place. Uses the Fisher-Yates algoritm to reorder the elements of the array, based on the [Lodash implimentation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js) ```js -const shuffle = (arr = []) => { - let i = -1; - const lastIndex = arr.length - 1; - while (++i < arr.length) { - const rand = i + Math.floor(Math.random() * (lastIndex - i + 1)); - const value = arr[rand]; - arr[rand] = arr[i]; - arr[i] = value; +const shuffle = ([...arr]) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; } - return arr + return arr; }; // shuffle([1,2,3]) -> [2,3,1] ``` From 34fa2b7c7768639516833e98558b51925976991e Mon Sep 17 00:00:00 2001 From: Travis CI Date: Tue, 26 Dec 2017 13:27:18 +0000 Subject: [PATCH 5/5] Travis build: 313 --- README.md | 13 ++++++++++--- docs/index.html | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 980acdc66..5b770f8c1 100644 --- a/README.md +++ b/README.md @@ -795,12 +795,19 @@ const sample = arr => arr[Math.floor(Math.random() * arr.length)]; ### shuffle -Randomizes the order of the values of an array. +Randomizes the order of the values of an array, in place. -Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator. +Uses the Fisher-Yates algoritm to reorder the elements of the array, based on the [Lodash implimentation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js) ```js -const shuffle = arr => arr.sort(() => Math.random() - 0.5); +const shuffle = ([...arr]) => { + let m = arr.length; + while (m) { + const i = Math.floor(Math.random() * m--); + [arr[m], arr[i]] = [arr[i], arr[m]]; + } + return arr; +}; // shuffle([1,2,3]) -> [2,3,1] ``` diff --git a/docs/index.html b/docs/index.html index edd49804d..aa9f6c31e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -586,9 +586,16 @@ This method also works with strings.

// sample([3, 7, 9, 11]) -> 9


similarity