From f9faeea358ceafdb0116df5fbc4d58df9b000d77 Mon Sep 17 00:00:00 2001 From: Arindal Char <110285827+arindal1@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:45:52 +0530 Subject: [PATCH 1/5] Created: README.md --- L-A/0012 SVG Path Data Parser/README.md | 54 +++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 L-A/0012 SVG Path Data Parser/README.md diff --git a/L-A/0012 SVG Path Data Parser/README.md b/L-A/0012 SVG Path Data Parser/README.md new file mode 100644 index 0000000..24c40f4 --- /dev/null +++ b/L-A/0012 SVG Path Data Parser/README.md @@ -0,0 +1,54 @@ +# SVG Path Data Parser +[Edabit Problem](https://edabit.com/challenge/ysMrKPGby3FXiYtQn) + +A `` element can usually be found inside an `` element and has an attribute **d** that represents the definition of the outline of a shape. + +A brief summary about this attribute: + +It contains commands (letters) and coordinates (numbers) +All instructions are expressed as one character (e.g., a moveto is expressed as an **M**). +Superfluous white space and separators such as commas can be eliminated (e.g., M 10 10 L 20 20 contains unnecessary spaces and could be expressed more compactly as `M10 10L20 20`). +The command letter can be eliminated on subsequent commands if the same command is used multiple times in a row (e.g., you can drop the second L in `M 10 20 L 20 10 L -10 -20` and use `M 10 20 L 20 10 -10 -20` instead). + +Your job is to build a parser that will convert this string in an array of commands, where each array element is an object with the `command` letter and an array of `parameters`. + +This summary is incomplete but should get you started, for more information please refer to the W3C specification found in the resources tab. + +## Examples + +```javascript +pathDataParser("") ➞ [] + +pathDataParser("M 0 0") ➞ [{command: 'M', parameters: [0, 0]}] + +pathDataParser("M 1 1.5 L 0 1.5 0 0.5 1 0.5 0.5 0 0 0.5 1 1.5 1 0.5 0 1.5" ➞ [ + {command: "M", parameters: [1, 1.5]}, + {command: "L", parameters: [0, 1.5, 0, 0.5, 1, 0.5, 0.5, 0, 0, 0.5, 1, 1.5, 1, 0.5, 0, 1.5]} +] + +pathDataParser("M 0,1 h 1 v -1 h 1 v 1 h 1 C 2,1 3,3 1.5,3 C 0,3 1,1 0,1 z" ➞ [ + {command: "M", parameters: [0, 1]}, + {command: "h", parameters: [1]}, + {command: "v", parameters: [-1]}, + {command: "h", parameters: [1]}, + {command: "v", parameters: [1]}, + {command: "h", parameters: [1]}, + {command: "C", parameters: [2, 1, 3, 3, 1.5, 3]}, + {command: "C", parameters: [0, 3, 1, 1, 0, 1]}, + {command: "z", parameters: []} +] +``` + +## Notes: + +- Return an empty array if no commands are found (example #1) +- The z (or Z) command is the only one without parameters, in this case return an empty array (see last command of example #4) +- The parameters array contains numbers, not strings, so you'll have to convert them +- Sometimes numbers can be very compressed to save space, let's look at some examples that might trip you up: + - Decimal numbers can start with a dot: .4 is the same as 0.4 + - If a negative number comes after another number, the space is optional: 0-4 is equal to 0 -4 + - Multiple decimal numbers in a row can be very tricky, remember that a number CAN NOT have more than 1 dot, so this: 1.2.34 is actually 2 different numbers: 1.2 and 0.34 +- Some examples have commas, some do not, some have multiline strings, some are a single line, remember to take into account all valid cases! Check out the tests tab to find out more. + +## By: arindal1 +***[GitHub](https://github.com/arindal1)*** From ea76ac441c6b4c50c01f37e287665c98da5bc4ff Mon Sep 17 00:00:00 2001 From: Arindal Char <110285827+arindal1@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:47:28 +0530 Subject: [PATCH 2/5] Created: `SVGDataParser` Solution --- .../SVGDataParser.js | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 L-A/0012 SVG Path Data Parser/SVGDataParser.js diff --git a/L-A/0012 SVG Path Data Parser/SVGDataParser.js b/L-A/0012 SVG Path Data Parser/SVGDataParser.js new file mode 100644 index 0000000..931a790 --- /dev/null +++ b/L-A/0012 SVG Path Data Parser/SVGDataParser.js @@ -0,0 +1,40 @@ +function pathDataParser(data) { + const commands = []; + let currentCommand = null; + let currentParams = []; + let currentParam = ''; + + for (let i = 0; i < data.length; i++) { + const char = data[i]; + + if (char === ' ' || char === ',') { + if (currentParam !== '') { + currentParams.push(parseFloat(currentParam)); + currentParam = ''; + } + } else if (/[a-zA-Z]/.test(char)) { + if (currentCommand !== null) { + commands.push({ command: currentCommand, parameters: currentParams }); + currentParams = []; + } + currentCommand = char; + } else { + currentParam += char; + } + } + + if (currentCommand !== null) { + if (currentParam !== '') { + currentParams.push(parseFloat(currentParam)); + } + commands.push({ command: currentCommand, parameters: currentParams }); + } + + return commands; +} + +// Examples +console.log(pathDataParser("")); +console.log(pathDataParser("M 0 0")); +console.log(pathDataParser("M 1 1.5 L 0 1.5 0 0.5 1 0.5 0.5 0 0 0.5 1 1.5 1 0.5 0 1.5")); +console.log(pathDataParser("M 0,1 h 1 v -1 h 1 v 1 h 1 C 2,1 3,3 1.5,3 C 0,3 1,1 0,1 z")); From 782f3d04dfc7f50f8c7e7c6aa1ca0004ea2829ab Mon Sep 17 00:00:00 2001 From: Arindal Char <110285827+arindal1@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:52:04 +0530 Subject: [PATCH 3/5] Created: **Caesar's Cipher/README.md** --- L-A/0013 Caesar's Cipher/README.md | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 L-A/0013 Caesar's Cipher/README.md diff --git a/L-A/0013 Caesar's Cipher/README.md b/L-A/0013 Caesar's Cipher/README.md new file mode 100644 index 0000000..20500cc --- /dev/null +++ b/L-A/0013 Caesar's Cipher/README.md @@ -0,0 +1,34 @@ +# Caesar's Cipher +[Edabit Problem](https://edabit.com/challenge/a33jdGXkaQRtK9ZTs) + +Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher (check ***Resources*** tab for more info) shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by *3, w, x, y* and *z* would map to *z, a, b* and *c*. + +Create a function that takes a string *s* (text to be encrypted) and an integer *k* (the rotation factor). It should return an encrypted string. + +## Example: +```javascript +caesarCipher("middle-Outz", 2) ➞ "okffng-Qwvb" + +// m -> o +// i -> k +// d -> f +// d -> f +// l -> n +// e -> g +// - - +// O -> Q +// u -> w +// t -> v +// z -> b + +caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5) +➞ "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj" + +caesarCipher("A friend in need is a friend indeed", 20) +➞ "U zlcyhx ch hyyx cm u zlcyhx chxyyx" +``` + +### Notes: All test input will be a valid ASCII string. + +## By: arindal1 +***[GitHub](https://github.com/arindal1)*** From 9fd706efe731dd3b70f25075f06ea03068b1a5cc Mon Sep 17 00:00:00 2001 From: Arindal Char <110285827+arindal1@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:52:44 +0530 Subject: [PATCH 4/5] Updated: `Caesar's Cipher/README.md` --- L-A/0013 Caesar's Cipher/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/L-A/0013 Caesar's Cipher/README.md b/L-A/0013 Caesar's Cipher/README.md index 20500cc..ef84879 100644 --- a/L-A/0013 Caesar's Cipher/README.md +++ b/L-A/0013 Caesar's Cipher/README.md @@ -1,5 +1,5 @@ # Caesar's Cipher -[Edabit Problem](https://edabit.com/challenge/a33jdGXkaQRtK9ZTs) +[Edabit Problem](https://edabit.com/challenge/a33jdGXkaQRtK9ZTs) Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar's cipher (check ***Resources*** tab for more info) shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by *3, w, x, y* and *z* would map to *z, a, b* and *c*. From 64b510c9b0d0c51dfb176c566d24569df646556f Mon Sep 17 00:00:00 2001 From: Arindal Char <110285827+arindal1@users.noreply.github.com> Date: Thu, 5 Oct 2023 12:54:23 +0530 Subject: [PATCH 5/5] Created: Solution for `Caesar's Cipher` --- L-A/0013 Caesar's Cipher/CaesarsCipher.js | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 L-A/0013 Caesar's Cipher/CaesarsCipher.js diff --git a/L-A/0013 Caesar's Cipher/CaesarsCipher.js b/L-A/0013 Caesar's Cipher/CaesarsCipher.js new file mode 100644 index 0000000..0c46e12 --- /dev/null +++ b/L-A/0013 Caesar's Cipher/CaesarsCipher.js @@ -0,0 +1,31 @@ +function caesarCipher(str, k) { + // Helper function to shift a single character by k positions + function shiftChar(char, k) { + const isUpperCase = /[A-Z]/.test(char); + const alphabetSize = 26; + + const baseCharCode = isUpperCase ? 'A'.charCodeAt(0) : 'a'.charCodeAt(0); + const shiftedCharCode = ((char.charCodeAt(0) - baseCharCode + k) % alphabetSize + alphabetSize) % alphabetSize + baseCharCode; + + return String.fromCharCode(shiftedCharCode); + } + + let result = ''; + + for (let i = 0; i < str.length; i++) { + const char = str[i]; + + if (/[A-Za-z]/.test(char)) { + result += shiftChar(char, k); + } else { + result += char; // Non-alphabetic characters remain unchanged + } + } + + return result; +} + +// Examples +console.log(caesarCipher("middle-Outz", 2)); // ➞ "okffng-Qwvb" +console.log(caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5)); // ➞ "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj" +console.log(caesarCipher("A friend in need is a friend indeed", 20)); // ➞ "U zlcyhx ch hyyx cm u zlcyhx chxyyx"