From 07daf5ff1ffba766ad5ad91d166827a128c766a7 Mon Sep 17 00:00:00 2001 From: Manish Bisht Date: Wed, 5 Jul 2023 08:24:20 +0530 Subject: [PATCH] feat: add problems --- BFE.dev/10. first bad version.js | 0 BFE.dev/4. implement basic throttle().js | 27 +++++++++++++++++++ ...rottle() with leading & trailing option.js | 0 BFE.dev/6. implement basic debounce().js | 14 ++++++++++ ...bounce() with leading & trailing option.js | 24 +++++++++++++++++ BFE.dev/8. can you shuffle() an array?.js | 9 +++++++ BFE.dev/9. decode message.js | 22 +++++++++++++++ .../Google/929. Unique Email Addresses.js | 13 +++++++++ 8 files changed, 109 insertions(+) create mode 100644 BFE.dev/10. first bad version.js create mode 100644 BFE.dev/4. implement basic throttle().js create mode 100644 BFE.dev/5. implement throttle() with leading & trailing option.js create mode 100644 BFE.dev/6. implement basic debounce().js create mode 100644 BFE.dev/7. implement debounce() with leading & trailing option.js create mode 100644 BFE.dev/8. can you shuffle() an array?.js create mode 100644 BFE.dev/9. decode message.js create mode 100644 Leetcode/Google/929. Unique Email Addresses.js diff --git a/BFE.dev/10. first bad version.js b/BFE.dev/10. first bad version.js new file mode 100644 index 00000000..e69de29b diff --git a/BFE.dev/4. implement basic throttle().js b/BFE.dev/4. implement basic throttle().js new file mode 100644 index 00000000..ebb0b029 --- /dev/null +++ b/BFE.dev/4. implement basic throttle().js @@ -0,0 +1,27 @@ +/** + * @param {Function} func + * @param {number} wait + */ +function throttle(func, wait) { + let waiting; + let params; + + function callback() { + if (params) { + func(...params); + params = null; + } + } + + return function (...args) { + params = args; + if (!waiting) { + callback(); + } + + waiting = setTimeout(() => { + waiting = null; + callback(); + }, wait); + }; +} diff --git a/BFE.dev/5. implement throttle() with leading & trailing option.js b/BFE.dev/5. implement throttle() with leading & trailing option.js new file mode 100644 index 00000000..e69de29b diff --git a/BFE.dev/6. implement basic debounce().js b/BFE.dev/6. implement basic debounce().js new file mode 100644 index 00000000..51e39f9f --- /dev/null +++ b/BFE.dev/6. implement basic debounce().js @@ -0,0 +1,14 @@ +/** + * @param {Function} func + * @param {number} wait + */ +function debounce(func, wait) { + let timer; + return function (...args) { + clearTimeout(timer); + timer = setTimeout(() => { + func(...args); + timer = null; + }, wait); + }; +} diff --git a/BFE.dev/7. implement debounce() with leading & trailing option.js b/BFE.dev/7. implement debounce() with leading & trailing option.js new file mode 100644 index 00000000..a9cf8cce --- /dev/null +++ b/BFE.dev/7. implement debounce() with leading & trailing option.js @@ -0,0 +1,24 @@ +/** + * @param {Function} func + * @param {number} wait + * @param {boolean} option.leading + * @param {boolean} option.trailing + */ +function debounce(func, wait, option = { leading: false, trailing: true }) { + let timer; + let lastArgs; + return function betterFunction(...args) { + lastArgs = args; + clearTimeout(timer); + if (option.leading && !timer && lastArgs) { + func(...lastArgs); + lastArgs = null; + } + timer = setTimeout(() => { + if (option.trailing && lastArgs) { + func(...lastArgs); + } + timer = null; + }, wait); + }; +} diff --git a/BFE.dev/8. can you shuffle() an array?.js b/BFE.dev/8. can you shuffle() an array?.js new file mode 100644 index 00000000..9ccc9ff8 --- /dev/null +++ b/BFE.dev/8. can you shuffle() an array?.js @@ -0,0 +1,9 @@ +/** + * @param {any[]} arr + */ +function shuffle(arr) { + for (let i = 0; i < arr.length; i++) { + const j = i + Math.floor(Math.random() * (arr.length - i)); + [arr[i], arr[j]] = [arr[j], arr[i]]; + } +} diff --git a/BFE.dev/9. decode message.js b/BFE.dev/9. decode message.js new file mode 100644 index 00000000..95c8ce78 --- /dev/null +++ b/BFE.dev/9. decode message.js @@ -0,0 +1,22 @@ +/** + * @param {string[][]} message + * @return {string} + */ +function decode(message) { + let ans = ""; + let i = 0; + let j = 0; + let direction = 1; + while (j < message.length && i < message[0].length) { + ans += message[j][i]; + i++; + if (j === 0) { + direction = 1; + } + if (j + 1 === message.length) { + direction = -1; + } + j += direction; + } + return ans; +} diff --git a/Leetcode/Google/929. Unique Email Addresses.js b/Leetcode/Google/929. Unique Email Addresses.js new file mode 100644 index 00000000..f9369fd3 --- /dev/null +++ b/Leetcode/Google/929. Unique Email Addresses.js @@ -0,0 +1,13 @@ +/** + * @param {string[]} emails + * @return {number} + */ +var numUniqueEmails = function (emails) { + const uniqueEmails = {}; + emails.forEach(email => { + const [username, domainName] = email.split("@"); + uniqueEmails[`${username.split("+")[0].replaceAll(".", "")}@${domainName}`] = 1; + }); + + return Object.keys(uniqueEmails).length; +};