Skip to content

Commit 8cc9d8e

Browse files
authored
Merge pull request #3 from marcode24/challenges
feat: add josephus-survivor solution
2 parents 37de37f + a36bbc4 commit 8cc9d8e

File tree

24 files changed

+621
-0
lines changed

24 files changed

+621
-0
lines changed

codewars/arrays-similar/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Arrays Similar
2+
3+
## Description
4+
5+
Write a function that determines whether the passed in sequences are similar. Similar means they contain the same elements, and the same number of ocurrences of elements.
6+
7+
```js
8+
var arr1 = [1, 2, 2, 3, 4],
9+
arr2 = [2, 1, 2, 4, 3],
10+
arr3 = [1, 2, 3, 4],
11+
arr4 = [1, 2, 3, '4']
12+
```
13+
14+
```js
15+
arraysSimilar(arr1, arr2); // should equal true
16+
arraysSimilar(arr2, arr3); // should equal false
17+
arraysSimilar(arr3, arr4); // should equal false
18+
```

codewars/arrays-similar/solution.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const arraysSimilar = (arr1, arr2) => {
2+
if (arr1.length !== arr2.length) {
3+
return false;
4+
}
5+
arr1 = arr1.sort((a, b) => a - b);
6+
arr2 = arr2.sort((a, b) => a - b);
7+
return arr1.every((el, index) => el === arr2[index]);
8+
};
9+
10+
module.exports = arraysSimilar;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const arraysSimilar = require('./solution');
2+
3+
describe('Arrays Similar', () => {
4+
const testCases = [
5+
{
6+
input: [[1, 2, 3, 4, 5], [2, 3, 4, 1, 5]],
7+
output: true,
8+
},
9+
{
10+
input: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], [5, 2, 3, 4, 7, 8, 9, 1, 0, 6]],
11+
output: true,
12+
},
13+
{
14+
input: [[1, 2, 3, '4', 5], [2, 3, 4, '1', 5]],
15+
output: false,
16+
},
17+
{
18+
input: [[1, 2, 3, '3', 5], [2, 3, 4, '1', 5]],
19+
output: false,
20+
},
21+
{
22+
input: [[2, 3], [1, 2, 4]],
23+
output: false,
24+
},
25+
];
26+
27+
it('should return a boolean type', () => {
28+
expect(typeof arraysSimilar([1, 2], [2, 1])).toBe('boolean');
29+
});
30+
31+
it.each(testCases)('should return $output', (testCase) => {
32+
expect(arraysSimilar(...testCase.input)).toBe(testCase.output);
33+
});
34+
});

codewars/chasers-schedule/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Chasers Schedule
2+
3+
## Description
4+
5+
A runner, who runs with base speed s with duration t will cover a distances d: d = s * t
6+
However, this runner can sprint for one unit of time with double speed s * 2
7+
After sprinting, base speed s will permanently reduced by 1, and for next one unit of time runner will enter recovery phase and can't sprint again.
8+
9+
Your task, given base speed s and time t, is to find the maximum possible distance d.
10+
11+
Input:
12+
1 <= s < 1000
13+
1 <= t < 1000
14+
15+
Example:
16+
Given s = 2 and t = 4.
17+
We could schedule when runner should sprint so we could get these possible sequences:
18+
19+
```txt
20+
Seq.: RRRR
21+
=> s + s + s + s
22+
=> 2 + 2 + 2 + 2 = 8
23+
Seq.: RRRS
24+
=> s + s + s + s*2
25+
=> 2 + 2 + 2 + 2*2 = 10
26+
Seq.: RRSR
27+
=> s + s + s*2 + (s-1)
28+
=> 2 + 2 + 2*2 + (2-1) = 9
29+
Seq.: RSRR
30+
=> s + s*2 + (s-1) + (s-1)
31+
=> 2 + 2*2 + (2-1) + (2-1) = 8
32+
Seq.: RSRS
33+
=> s + s*2 + (s-1) + (s-1)*2
34+
=> 2 + 2*2 + (2-1) + (2-1)*2 = 9
35+
Seq.: SRRR
36+
=> s*2 + (s-1) + (s-1) + (s-1)
37+
=> 2*2 + (2-1) + (2-1) + (2-1) = 7
38+
Seq.: SRRS
39+
=> s*2 + (s-1) + (s-1) + (s-1)*2
40+
=> 2*2 + (2-1) + (2-1) + (2-1)*2 = 8
41+
Seq.: SRSR
42+
=> s*2 + (s-1) + (s-1)*2 + (s-1-1)
43+
=> 2*2 + (2-1) + (2-1)*2 + (2-1-1) = 7
44+
```
45+
46+
Where:
47+
- R: Normal Run / Recovery
48+
- S: Sprint
49+
Based on above sequences, the maximum possible distance d is 10.
50+

codewars/chasers-schedule/solution.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function chaserSchedule(speed, time) {
2+
let distance = speed * time;
3+
const maxSprints = Math.ceil(time / 2);
4+
5+
for (let i = 0; i < maxSprints; i++) {
6+
if (speed - 3 * i > 0) {
7+
distance += speed - 3 * i;
8+
}
9+
}
10+
return distance;
11+
}
12+
13+
module.exports = chaserSchedule;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const chaserSchedule = require('./solution');
2+
3+
describe('Chasers Schedule', () => {
4+
const testCases = [
5+
{
6+
input: [2, 4],
7+
expected: 10,
8+
},
9+
{
10+
input: [1, 1],
11+
expected: 2,
12+
},
13+
{
14+
input: [829, 135],
15+
expected: 161453,
16+
},
17+
{
18+
input: [742, 775],
19+
expected: 667182,
20+
},
21+
];
22+
23+
it('should return a number type', () => {
24+
expect(typeof chaserSchedule(1, 1)).toBe('number');
25+
});
26+
27+
it.each(testCases)('should return $output', (testCase) => {
28+
const { input, expected } = testCase;
29+
expect(chaserSchedule(...input)).toBe(expected);
30+
});
31+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Consecutive Strings
2+
3+
## Description
4+
5+
You are given an array(list) strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.
6+
7+
Examples:
8+
9+
```txt
10+
strarr = ["tree", "foling", "trashy", "blue", "abcdef", "uvwxyz"], k = 2
11+
12+
Concatenate the consecutive strings of strarr by 2, we get:
13+
14+
treefoling (length 10) concatenation of strarr[0] and strarr[1]
15+
folingtrashy (" 12) concatenation of strarr[1] and strarr[2]
16+
trashyblue (" 10) concatenation of strarr[2] and strarr[3]
17+
blueabcdef (" 10) concatenation of strarr[3] and strarr[4]
18+
abcdefuvwxyz (" 12) concatenation of strarr[4] and strarr[5]
19+
20+
Two strings are the longest: "folingtrashy" and "abcdefuvwxyz".
21+
The first that came is "folingtrashy" so
22+
longest_consec(strarr, 2) should return "folingtrashy".
23+
24+
In the same way:
25+
longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta"
26+
```
27+
28+
n being the length of the string array, if n = 0 or k > n or k <= 0 return "" (return Nothing in Elm, "nothing" in Erlang).
29+
30+
Note
31+
32+
consecutive strings : follow one after another without an interruption
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const longestConsec = (strarr, k) => {
2+
if (k <= 0 || k > strarr.length) {
3+
return '';
4+
}
5+
let longestWord = '';
6+
let longestWordTemp = '';
7+
for (let i = 0; i <= strarr.length - k; i++) {
8+
longestWordTemp = strarr.slice(i, i + k).join('');
9+
if (longestWordTemp.length > longestWord.length) {
10+
longestWord = longestWordTemp;
11+
}
12+
}
13+
return longestWord;
14+
};
15+
16+
module.exports = longestConsec;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const longestConsec = require('./solution');
2+
3+
describe('Consecutive Strings', () => {
4+
const testCases = [
5+
{
6+
input: ['zone', 'abigail', 'theta', 'form', 'libe', 'zas'],
7+
k: 2,
8+
output: 'abigailtheta',
9+
},
10+
{
11+
input: ['ejjjjmmtthh', 'zxxuueeg', 'aanlljrrrxx',
12+
'dqqqaaabbb', 'oocccffuucccjjjkkkjyyyeehh'],
13+
k: 1,
14+
output: 'oocccffuucccjjjkkkjyyyeehh',
15+
},
16+
{
17+
input: [],
18+
k: 3,
19+
output: '',
20+
},
21+
{
22+
input: ['itvayloxrp', 'wkppqsztdkmvcuwvereiupccauycnjutlv',
23+
'vweqilsfytihvrzlaodfixoyxvyuyvgpck'],
24+
k: 2,
25+
output: 'wkppqsztdkmvcuwvereiupccauycnjutlvvweqilsfytihvrzlaodfixoyxvyuyvgpck',
26+
},
27+
{
28+
input: ['it', 'wkppv', 'ixoyx', '3452', 'zzzzzzzzzzzz'],
29+
k: 3,
30+
output: 'ixoyx3452zzzzzzzzzzzz',
31+
},
32+
{
33+
input: ['it', 'wkppv', 'ixoyx', '3452', 'zzzzzzzzzzzz'],
34+
k: 0,
35+
output: '',
36+
},
37+
];
38+
39+
it('should return a string type', () => {
40+
const result = longestConsec(['zone', 'abigail', 'theta', 'form', 'libe', 'zas'], 2);
41+
expect(typeof result).toBe('string');
42+
});
43+
44+
it.each(testCases)('should return "$output"', (testCase) => {
45+
const result = longestConsec(testCase.input, testCase.k);
46+
expect(result).toBe(testCase.output);
47+
});
48+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# I Am Very Very Very
2+
3+
## Description
4+
5+
You are very very happy and very very very excited to solve this Kata.
6+
Show it to us by creating a function iam such as:
7+
8+
```js
9+
iam('happy') // returns the string "I am happy"
10+
iam('excited') // returns the string "I am excited"
11+
iam()('scared') // returns the string "I am very scared"
12+
iam()()('interested') // returns the string "I am very very interested"
13+
```
14+
and so on...
15+
16+
As you understood, the function iam accept 1 optional parameter.
17+
If provided, the function returns a string.
18+
If NOT provided, it must returns a function allowing to continue the sentence.
19+
20+
I am very very very curious to see you approach the problem.
21+
22+
Enjoy!

0 commit comments

Comments
 (0)