Skip to content

Commit 8cf1325

Browse files
authored
Merge pull request #4 from marcode24/challenges
feat: add number in expanded form solution
2 parents 8cc9d8e + 3f0c0fd commit 8cf1325

File tree

18 files changed

+495
-0
lines changed

18 files changed

+495
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Write Number in Expanded Form
2+
3+
## Description
4+
5+
You will be given a number and you will need to return it as a string in Expanded Form. For example:
6+
7+
```js
8+
expandedForm(12); // should return '10 + 2'
9+
expandedForm(42); // should return '40 + 2'
10+
expandedForm(70304); // should return '70000 + 300 + 4'
11+
```
12+
13+
**NOTE:** All numbers will be whole numbers greater than 0.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const expandedForm = (num) => {
2+
num = num.toString().split('').reverse();
3+
const results = [];
4+
num.forEach((item, index) => {
5+
if (item !== '0') {
6+
results.push(`${item}${'0'.repeat(index)}`);
7+
}
8+
});
9+
return results.reverse().join(' + ');
10+
};
11+
12+
module.exports = expandedForm;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const expandedForm = require('./solution');
2+
3+
describe('Number in expanded form', () => {
4+
const testCases = [
5+
{
6+
input: 12,
7+
output: '10 + 2',
8+
},
9+
{
10+
input: 42,
11+
output: '40 + 2',
12+
},
13+
{
14+
input: 70304,
15+
output: '70000 + 300 + 4',
16+
},
17+
{
18+
input: 10345354,
19+
output: '10000000 + 300000 + 40000 + 5000 + 300 + 50 + 4',
20+
},
21+
];
22+
23+
it('should return a string type', () => {
24+
expect(typeof expandedForm(12)).toBe('string');
25+
});
26+
27+
it.each(testCases)('should return the correct output', ({ input, output }) => {
28+
expect(expandedForm(input)).toBe(output);
29+
});
30+
});

codewars/range-extraction/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Range Extraction
2+
3+
## Description
4+
5+
A format for expressing an ordered list of integers is to use a comma separated list of either
6+
7+
- individual integers
8+
- or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example "12,13,15-17"
9+
10+
Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.
11+
12+
Example:
13+
14+
```js
15+
solution([
16+
-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18,
17+
19, 20,
18+
]);
19+
// returns "-10--8,-6,-3-1,3-5,7-11,14,15,17-20"
20+
```

codewars/range-extraction/solution.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const solution = (list) => {
2+
let result = '';
3+
let start = list[0];
4+
let end = list[0];
5+
let i = 1;
6+
7+
while (i < list.length) {
8+
if (list[i] - list[i - 1] === 1) {
9+
end = list[i];
10+
} else {
11+
if (start === end) {
12+
result += `${start},`;
13+
} else if (end - start === 1) {
14+
result += `${start},${end},`;
15+
} else {
16+
result += `${start}-${end},`;
17+
}
18+
start = list[i];
19+
end = list[i];
20+
}
21+
i += 1;
22+
}
23+
if (start === end) {
24+
result += `${start}`;
25+
} else if (end - start === 1) {
26+
result += `${start},${end}`;
27+
} else {
28+
result += `${start}-${end}`;
29+
}
30+
31+
return result;
32+
};
33+
34+
module.exports = solution;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const solution = require('./solution');
2+
3+
describe('Range extraction', () => {
4+
const testCases = [
5+
{
6+
input: [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20],
7+
output: '-6,-3-1,3-5,7-11,14,15,17-20',
8+
},
9+
{
10+
input: [-3, -2, -1, 2, 10, 15, 16, 18, 19, 20],
11+
output: '-3--1,2,10,15,16,18-20',
12+
},
13+
{
14+
input: [-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17,
15+
18, 19, 20],
16+
output: '-10--8,-6,-3-1,3-5,7-11,14,15,17-20',
17+
},
18+
{
19+
input: [-3, -2, -1, 2, 10, 15, 16, 18, 19, 20, 22, 23],
20+
output: '-3--1,2,10,15,16,18-20,22,23',
21+
},
22+
];
23+
24+
it('should return a string', () => {
25+
expect(typeof solution([-1, 0, 1, 2, 6, 7, 9])).toBe('string');
26+
});
27+
28+
it.each(testCases)('should return the correct output', (testCase) => {
29+
expect(solution(testCase.input)).toBe(testCase.output);
30+
});
31+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Roman Numerals Encoder
2+
3+
## Description
4+
5+
Create a function taking a positive integer between 1 and 3999 (both included) as its parameter and returning a string containing the Roman Numeral representation of that integer.
6+
7+
Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.
8+
9+
Example:
10+
11+
```js
12+
solution(1000); // should return 'M'
13+
```
14+
15+
Help:
16+
17+
```txt
18+
Symbol Value
19+
I 1
20+
V 5
21+
X 10
22+
L 50
23+
C 100
24+
D 500
25+
M 1,000
26+
```
27+
28+
Remember that there can't be more than 3 identical symbols in a row.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const solution = (number) => {
2+
const romanNumerals = {
3+
1: 'I',
4+
4: 'IV',
5+
5: 'V',
6+
9: 'IX',
7+
10: 'X',
8+
40: 'XL',
9+
50: 'L',
10+
90: 'XC',
11+
100: 'C',
12+
400: 'CD',
13+
500: 'D',
14+
900: 'CM',
15+
1000: 'M',
16+
};
17+
18+
const romanNumeralsKeys = Object.keys(romanNumerals).reverse();
19+
let result = '';
20+
21+
Array.from({ length: romanNumeralsKeys.length }).forEach((_, i) => {
22+
const key = romanNumeralsKeys[i];
23+
const value = romanNumerals[key];
24+
while (number >= key) {
25+
result += value;
26+
number -= key;
27+
}
28+
});
29+
30+
return result;
31+
};
32+
33+
module.exports = solution;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const solution = require('./solution');
2+
3+
describe('Roman Numerals Encoder', () => {
4+
const testCases = [
5+
{
6+
input: 1,
7+
output: 'I',
8+
},
9+
{
10+
input: 2,
11+
output: 'II',
12+
},
13+
{
14+
input: 3,
15+
output: 'III',
16+
},
17+
{
18+
input: 4,
19+
output: 'IV',
20+
},
21+
{
22+
input: 5,
23+
output: 'V',
24+
},
25+
{
26+
input: 9,
27+
output: 'IX',
28+
},
29+
{
30+
input: 10,
31+
output: 'X',
32+
},
33+
{
34+
input: 11,
35+
output: 'XI',
36+
},
37+
{
38+
input: 19,
39+
output: 'XIX',
40+
},
41+
{
42+
input: 22,
43+
output: 'XXII',
44+
},
45+
{
46+
input: 15,
47+
output: 'XV',
48+
},
49+
{
50+
input: 1000,
51+
output: 'M',
52+
},
53+
{
54+
input: 1001,
55+
output: 'MI',
56+
},
57+
{
58+
input: 1990,
59+
output: 'MCMXC',
60+
},
61+
{
62+
input: 2007,
63+
output: 'MMVII',
64+
},
65+
{
66+
input: 2008,
67+
output: 'MMVIII',
68+
},
69+
];
70+
71+
it('should return a string type', () => {
72+
expect(solution(1)).toBe('I');
73+
});
74+
75+
it.each(testCases)('should return $output', (testCase) => {
76+
expect(solution(testCase.input)).toBe(testCase.output);
77+
});
78+
});

codewars/what-century-is-it/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# What Century Is It?
2+
3+
## Description
4+
5+
Return the century of the input year. The input will always be a 4 digit string, so there is no need for validation.
6+
7+
Examples
8+
9+
```txt
10+
"1999" --> "20th"
11+
"2011" --> "21st"
12+
"2154" --> "22nd"
13+
"2259" --> "23rd"
14+
"1124" --> "12th"
15+
"2000" --> "20th"
16+
```

0 commit comments

Comments
 (0)