Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions L-A/0012 SVG Path Data Parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# SVG Path Data Parser
[Edabit Problem](https://edabit.com/challenge/ysMrKPGby3FXiYtQn)

A `<path>` element can usually be found inside an `<svg>` 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)***
40 changes: 40 additions & 0 deletions L-A/0012 SVG Path Data Parser/SVGDataParser.js
Original file line number Diff line number Diff line change
@@ -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"));
31 changes: 31 additions & 0 deletions L-A/0013 Caesar's Cipher/CaesarsCipher.js
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions L-A/0013 Caesar's Cipher/README.md
Original file line number Diff line number Diff line change
@@ -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)***