Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ccfe490

Browse files
Merge pull request #57 from arindal1/main
Added: 2 JS Codes
2 parents 1ed1a90 + 64b510c commit ccfe490

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SVG Path Data Parser
2+
[Edabit Problem](https://edabit.com/challenge/ysMrKPGby3FXiYtQn)
3+
4+
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.
5+
6+
A brief summary about this attribute:
7+
8+
It contains commands (letters) and coordinates (numbers)
9+
All instructions are expressed as one character (e.g., a moveto is expressed as an **M**).
10+
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`).
11+
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).
12+
13+
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`.
14+
15+
This summary is incomplete but should get you started, for more information please refer to the W3C specification found in the resources tab.
16+
17+
## Examples
18+
19+
```javascript
20+
pathDataParser("") ➞ []
21+
22+
pathDataParser("M 0 0") ➞ [{command: 'M', parameters: [0, 0]}]
23+
24+
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" ➞ [
25+
{command: "M", parameters: [1, 1.5]},
26+
{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]}
27+
]
28+
29+
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" ➞ [
30+
{command: "M", parameters: [0, 1]},
31+
{command: "h", parameters: [1]},
32+
{command: "v", parameters: [-1]},
33+
{command: "h", parameters: [1]},
34+
{command: "v", parameters: [1]},
35+
{command: "h", parameters: [1]},
36+
{command: "C", parameters: [2, 1, 3, 3, 1.5, 3]},
37+
{command: "C", parameters: [0, 3, 1, 1, 0, 1]},
38+
{command: "z", parameters: []}
39+
]
40+
```
41+
42+
## Notes:
43+
44+
- Return an empty array if no commands are found (example #1)
45+
- The z (or Z) command is the only one without parameters, in this case return an empty array (see last command of example #4)
46+
- The parameters array contains numbers, not strings, so you'll have to convert them
47+
- Sometimes numbers can be very compressed to save space, let's look at some examples that might trip you up:
48+
- Decimal numbers can start with a dot: .4 is the same as 0.4
49+
- If a negative number comes after another number, the space is optional: 0-4 is equal to 0 -4
50+
- 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
51+
- 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.
52+
53+
## By: arindal1
54+
***[GitHub](https://github.com/arindal1)***
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function pathDataParser(data) {
2+
const commands = [];
3+
let currentCommand = null;
4+
let currentParams = [];
5+
let currentParam = '';
6+
7+
for (let i = 0; i < data.length; i++) {
8+
const char = data[i];
9+
10+
if (char === ' ' || char === ',') {
11+
if (currentParam !== '') {
12+
currentParams.push(parseFloat(currentParam));
13+
currentParam = '';
14+
}
15+
} else if (/[a-zA-Z]/.test(char)) {
16+
if (currentCommand !== null) {
17+
commands.push({ command: currentCommand, parameters: currentParams });
18+
currentParams = [];
19+
}
20+
currentCommand = char;
21+
} else {
22+
currentParam += char;
23+
}
24+
}
25+
26+
if (currentCommand !== null) {
27+
if (currentParam !== '') {
28+
currentParams.push(parseFloat(currentParam));
29+
}
30+
commands.push({ command: currentCommand, parameters: currentParams });
31+
}
32+
33+
return commands;
34+
}
35+
36+
// Examples
37+
console.log(pathDataParser(""));
38+
console.log(pathDataParser("M 0 0"));
39+
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"));
40+
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"));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function caesarCipher(str, k) {
2+
// Helper function to shift a single character by k positions
3+
function shiftChar(char, k) {
4+
const isUpperCase = /[A-Z]/.test(char);
5+
const alphabetSize = 26;
6+
7+
const baseCharCode = isUpperCase ? 'A'.charCodeAt(0) : 'a'.charCodeAt(0);
8+
const shiftedCharCode = ((char.charCodeAt(0) - baseCharCode + k) % alphabetSize + alphabetSize) % alphabetSize + baseCharCode;
9+
10+
return String.fromCharCode(shiftedCharCode);
11+
}
12+
13+
let result = '';
14+
15+
for (let i = 0; i < str.length; i++) {
16+
const char = str[i];
17+
18+
if (/[A-Za-z]/.test(char)) {
19+
result += shiftChar(char, k);
20+
} else {
21+
result += char; // Non-alphabetic characters remain unchanged
22+
}
23+
}
24+
25+
return result;
26+
}
27+
28+
// Examples
29+
console.log(caesarCipher("middle-Outz", 2)); // ➞ "okffng-Qwvb"
30+
console.log(caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5)); // ➞ "Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj"
31+
console.log(caesarCipher("A friend in need is a friend indeed", 20)); // ➞ "U zlcyhx ch hyyx cm u zlcyhx chxyyx"

‎L-A/0013 Caesar's Cipher/README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Caesar's Cipher
2+
[Edabit Problem](https://edabit.com/challenge/a33jdGXkaQRtK9ZTs)
3+
4+
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*.
5+
6+
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.
7+
8+
## Example:
9+
```javascript
10+
caesarCipher("middle-Outz", 2) ➞ "okffng-Qwvb"
11+
12+
// m -> o
13+
// i -> k
14+
// d -> f
15+
// d -> f
16+
// l -> n
17+
// e -> g
18+
// - -
19+
// O -> Q
20+
// u -> w
21+
// t -> v
22+
// z -> b
23+
24+
caesarCipher("Always-Look-on-the-Bright-Side-of-Life", 5)
25+
"Fqbfdx-Qttp-ts-ymj-Gwnlmy-Xnij-tk-Qnkj"
26+
27+
caesarCipher("A friend in need is a friend indeed", 20)
28+
"U zlcyhx ch hyyx cm u zlcyhx chxyyx"
29+
```
30+
31+
### Notes: All test input will be a valid ASCII string.
32+
33+
## By: arindal1
34+
***[GitHub](https://github.com/arindal1)***

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /