List<int> twinSumSolutions(List<int> array) { }
// Code goes here- List<int> twinSumSolutions(List<int> array) {
- }
// See https://pub.dartlang.org/packages/test import "package:test/test.dart"; import "package:solution/solution.dart"; void main() { group('Example', () { for (var i = 0; i < 100; i++) { test('returns the sum of each pair', () { expect(twinSumSolutions([2, 2, 4, 4]), equals([4, 8])); }); test('only adds the pairs', () { expect(twinSumSolutions([3, 2, 6, 6]), equals([12])); }); test('returns an empty array when passed no pairs', () { expect(twinSumSolutions([1, 2, 3, 4]), equals([])); }); } }); }
// 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");- // See https://pub.dartlang.org/packages/test
- import "package:test/test.dart";
- import "package:solution/solution.dart";
describe("Solution", function() {it("should test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);- void main() {
- group('Example', () {
- for (var i = 0; i < 100; i++) {
- test('returns the sum of each pair', () {
- expect(twinSumSolutions([2, 2, 4, 4]), equals([4, 8]));
- });
- test('only adds the pairs', () {
- expect(twinSumSolutions([3, 2, 6, 6]), equals([12]));
- });
- test('returns an empty array when passed no pairs', () {
- expect(twinSumSolutions([1, 2, 3, 4]), equals([]));
- });
- }
- });
});- }
// 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('Example', () => { for (let i = 0; i < 100; i++) { it('returns the sum of each pair', () => { assert.strictEqual(twinSumSolutions([2, 2, 4, 4]), [4, 16]) }); it('only adds the pairs', () => { assert.strictEqual(twinSumSolutions([3, 2, 6, 6]), [12]) }); it('returns an empty array when passed no pairs', () => { assert.strictEqual(twinSumSolutions([1, 2, 3, 4]), []) }); } });
- // 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 test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);});- describe('Example', () => {
- for (let i = 0; i < 100; i++) {
- it('returns the sum of each pair', () => {
- assert.strictEqual(twinSumSolutions([2, 2, 4, 4]), [4, 16])
- });
- it('only adds the pairs', () => {
- assert.strictEqual(twinSumSolutions([3, 2, 6, 6]), [12])
- });
- it('returns an empty array when passed no pairs', () => {
- assert.strictEqual(twinSumSolutions([1, 2, 3, 4]), [])
- });
- }
- });
# From Ruby 3.0, RSpec is used under the hood. # See https://rspec.info/ # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well. describe "Example" do 100.times do it 'returns the sum of each pair' do expect(twin_sum_solutions([2, 2, 4, 4])).to eq([4, 8]) end it 'only adds the pairs' do expect(twin_sum_solutions([3, 2, 6, 6])).to eq([12]) end it 'returns an empty array when passed no pairs' do expect(twin_sum_solutions([1, 2, 3, 4])).to eq([]) end end end
// 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 test for something", function() {// Test.assertEquals(1 + 1, 2);// assert.strictEqual(1 + 1, 2);});});- # From Ruby 3.0, RSpec is used under the hood.
- # See https://rspec.info/
- # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
- describe "Example" do
- 100.times do
- it 'returns the sum of each pair' do
- expect(twin_sum_solutions([2, 2, 4, 4])).to eq([4, 8])
- end
- it 'only adds the pairs' do
- expect(twin_sum_solutions([3, 2, 6, 6])).to eq([12])
- end
- it 'returns an empty array when passed no pairs' do
- expect(twin_sum_solutions([1, 2, 3, 4])).to eq([])
- end
- end
- end
Your goal is to write a function that takes in an array of numbers. This function then returns another array which contains the sum of every pair in the original array.
twinSumSolutions([2,2,4,4]) // [4, 16]
twinSumSolutions([1,3,3,5]) // [6]
twinSumSolutions([1,2,3,4]) // []
Assume that all of of the elements in the array will always be integers.
// Code goes here
// 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 test for something", function() {
// Test.assertEquals(1 + 1, 2);
// assert.strictEqual(1 + 1, 2);
});
});
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);
});
});