Your goal is to write a function that takes in 2 strings and return the winner in rock paper scissors. For example:
dumbRockPaperScissors("Rock", "Paper") // "Player 2 wins"
dumbRockPaperScissors("Paper", "Rock") // "Player 1 wins"
dumbRockPaperScissors("Scissors", "Scissors") // "Draw"
The twist is that your function needs to be as SLOW as you can make it. The 2 rules:
- No setTimeouts or anyway to artificially inflate times
- All code must provide some sort of value toward solving the problem
function dumbRockPaperScissors(player1, player2) {
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe('dumbRockPaperScissors', function() {
it('returns "Draw" for Rock vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Rock'), 'Draw');
});
it('returns "Draw" for Paper vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Paper'), 'Draw');
});
it('returns "Draw" for Scissors vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Scissors'), 'Draw');
});
it('returns "Player 1 wins" for Rock vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Scissors'), 'Player 1 wins');
});
it('returns "Player 1 wins" for Scissors vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Paper'), 'Player 1 wins');
});
it('returns "Player 1 wins" for Paper vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Rock'), 'Player 1 wins');
});
it('returns "Player 2 wins" for Scissors vs Rock', function() {
assert.strictEqual(dumbRockPaperScissors('Scissors', 'Rock'), 'Player 2 wins');
});
it('returns "Player 2 wins" for Paper vs Scissors', function() {
assert.strictEqual(dumbRockPaperScissors('Paper', 'Scissors'), 'Player 2 wins');
});
it('returns "Player 2 wins" for Rock vs Paper', function() {
assert.strictEqual(dumbRockPaperScissors('Rock', 'Paper'), 'Player 2 wins');
});
});
This function has already been writte, but is very slow, operating at O(n^2) complexity. Your goal is to optimize this function to be as fast as possible.
const firstNonRepeatingCharacter = (str) => {
for (let i = 0; i < str.length; i++) {
let seenDuplicate = false;
for (let j = 0; j < str.length; j++) {
if (str[i] === str[j] && i !== j) {
seenDuplicate = true;
break;
}
}
if (!seenDuplicate) {
return str[i];
}
}
return null; // return null if no unique character is found
};
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
// const Test = require("@codewars/test-compat");
describe("Solution", function() {
it('should return the first non-repeating character', function() {
assert.strictEqual(firstNonRepeatingCharacter('abcd'), 'a');
assert.strictEqual(firstNonRepeatingCharacter('aabbcdc'), 'd');
});
it('should return the first non-repeating character when special characters are included', function() {
assert.strictEqual(firstNonRepeatingCharacter('a@b@bc'), 'a');
});
it('should return null when all characters are repeating', function() {
assert.strictEqual(firstNonRepeatingCharacter('aabbcc'), null);
});
it('should return the first character if it is the only one', function() {
assert.strictEqual(firstNonRepeatingCharacter('z'), 'z');
});
it('should handle an empty string correctly', function() {
assert.strictEqual(firstNonRepeatingCharacter(''), null);
});
it('should handle strings with numbers', function() {
assert.strictEqual(firstNonRepeatingCharacter('1122a'), 'a');
});
it('should handle very long strings with the non-repeating character at the end', function() {
const longString = 'a'.repeat(10000) + 'b'.repeat(10000) + 'c';
assert.strictEqual(firstNonRepeatingCharacter(longString), 'c');
});
it('should handle very long strings with all characters repeating', function() {
const longString = 'a'.repeat(20000) + 'b'.repeat(20000);
assert.strictEqual(firstNonRepeatingCharacter(longString), null);
});
});