In case of numbers that exceeds Number.MAX_SAFE_INTEGER (2^53 - 1), those numbers in js MUST be passed as Big integers to avoid precision loss
const maxNumber = n => { return BigInt([...n+''].reduce((a,c) => {a[9-c]=(a[9-c]||'')+c;return a},[]).join('')); }
import java.util.Arrays;public class MaxNumber {public static long print(long number) {return number}- const maxNumber = n => {
- return BigInt([...n+''].reduce((a,c) => {a[9-c]=(a[9-c]||'')+c;return a},[]).join(''));
- }
// 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_maximum_format_of_number", function() { assert.strictEqual(maxNumber(4n), 4n); assert.strictEqual(maxNumber(12n), 21n); assert.strictEqual(maxNumber(101n), 110n); assert.strictEqual(maxNumber(400000005000007000n), 754000000000000000n); assert.strictEqual(maxNumber(307778062924466824n), 988777666444322200n); }); });
import static org.junit.Assert.assertEquals;import org.junit.Test;import java.util.Random;- // 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");
public class MaxNumberTest {@Testpublic void testFour() {assertEquals(4, MaxNumber.print(4));}@Testpublic void testTwelve() {assertEquals(21, MaxNumber.print(12));}@Testpublic void testOneHundred() {assertEquals(110, MaxNumber.print(101));}@Testpublic void testHuge1() {assertEquals(754000000000000000L, MaxNumber.print(400000005000007000L));}@Testpublic void testHuge2() {assertEquals(988777666444322200L, MaxNumber.print(307778062924466824L));}}- describe("Solution", function() {
- it("should_return_maximum_format_of_number", function() {
- assert.strictEqual(maxNumber(4n), 4n);
- assert.strictEqual(maxNumber(12n), 21n);
- assert.strictEqual(maxNumber(101n), 110n);
- assert.strictEqual(maxNumber(400000005000007000n), 754000000000000000n);
- assert.strictEqual(maxNumber(307778062924466824n), 988777666444322200n);
- });
- });
function mean(x){ return x.length? x.reduce((a,c) => a+c,0)/x.length : 0; }
#include <iostream>double Mean(double x[], int n){double sum = 0;for(int i = 0; i < n; i++){sum += x[i];}return sum / n;- function mean(x){
- return x.length? x.reduce((a,c) => a+c,0)/x.length : 0;
- }
// 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 calculate the mean of a list", function() { assert.strictEqual(mean([4,8,4,8]), 6); assert.strictEqual(mean([1,2,3,4,5]), 3); assert.strictEqual(mean([]), 0); }); });
// TODO: Replace examples and use TDD by writing your own tests- // 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(any_group_name_you_want){It(should_do_something){int n = 4;double x[] = {4,8,4,8};double mean;mean = Mean(x ,n);Assert::That(6, Equals(mean));}};- describe("Solution", function() {
- it("should calculate the mean of a list", function() {
- assert.strictEqual(mean([4,8,4,8]), 6);
- assert.strictEqual(mean([1,2,3,4,5]), 3);
- assert.strictEqual(mean([]), 0);
- });
- });
function missingNumber(arr){ const max = Math.max(...arr); return (max*(max+1)/2)-arr.reduce((a,c) => a+c,0); }
class Solution {public:int missingNumber(vector<int>& nums) {}};- function missingNumber(arr){
- const max = Math.max(...arr);
- return (max*(max+1)/2)-arr.reduce((a,c) => a+c,0);
- }
// 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_5_for_given_array", function() { assert.strictEqual(missingNumber([1, 2, 3, 4, 6, 7, 8, 9, 10]), 5); }); it("should_return_2_for_given_array", function() { assert.strictEqual(missingNumber([1, 3, 4, 5]), 2); }); });
#include <criterion/criterion.h>- // 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");
// Function to find the missing number in an array of integersint findMissingNumber(int arr[], int n) {// Code to find the missing number}Test(findMissingNumber, should_return_5_for_given_array) {int arr[] = {1, 2, 3, 4, 6, 7, 8, 9, 10};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 5);}Test(findMissingNumber, should_return_2_for_given_array) {int arr[] = {1, 3, 4, 5};int n = sizeof(arr) / sizeof(arr[0]);cr_assert_eq(findMissingNumber(arr, n), 2);}- describe("Solution", function() {
- it("should_return_5_for_given_array", function() {
- assert.strictEqual(missingNumber([1, 2, 3, 4, 6, 7, 8, 9, 10]), 5);
- });
- it("should_return_2_for_given_array", function() {
- assert.strictEqual(missingNumber([1, 3, 4, 5]), 2);
- });
- });
js version with fixed TestCases.
function multiplicationTable(){ let arr = []; for(let i=1; i<6; i++){ arr.push([]); for(let j=1; j<13; j++){ arr[arr.length-1].push(`${i} * ${j} = ${i*j}`); } } return arr.map(e => e.join('\n')).join('\n\n'); }
#include<stdio.h>int main(){int i=1;while(i<=5){int j=1;while(j<=12){printf("%d * %d =%d \n", i,j, i*j);j++;- function multiplicationTable(){
- let arr = [];
- for(let i=1; i<6; i++){
- arr.push([]);
- for(let j=1; j<13; j++){
- arr[arr.length-1].push(`${i} * ${j} = ${i*j}`);
- }
i++;printf("\n");}return(0);- } return arr.map(e => e.join('\n')).join('\n\n');
- }
// 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"); function multiplicationTable(){ let arr = []; for(let i=1; i<6; i++){ arr.push([]); for(let j=1; j<13; j++){ arr[arr.length-1].push(`${i} * ${j} = ${i*j}`); } } return arr.map(e => e.join('\n')).join('\n\n'); } describe("Solution", function() { it("should give multiplication table from 1 to 5", function() { assert.strictEqual(multiplicationTable(), `1 * 1 = 1 1 * 2 = 2 1 * 3 = 3 1 * 4 = 4 1 * 5 = 5 1 * 6 = 6 1 * 7 = 7 1 * 8 = 8 1 * 9 = 9 1 * 10 = 10 1 * 11 = 11 1 * 12 = 12 2 * 1 = 2 2 * 2 = 4 2 * 3 = 6 2 * 4 = 8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18 2 * 10 = 20 2 * 11 = 22 2 * 12 = 24 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27 3 * 10 = 30 3 * 11 = 33 3 * 12 = 36 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36 4 * 10 = 40 4 * 11 = 44 4 * 12 = 48 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 5 * 11 = 55 5 * 12 = 60`); }); });
- // 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");
- function multiplicationTable(){
- let arr = [];
- for(let i=1; i<6; i++){
- arr.push([]);
- for(let j=1; j<13; j++){
- arr[arr.length-1].push(`${i} * ${j} = ${i*j}`);
- }
- } return arr.map(e => e.join('\n')).join('\n\n');
- }
- describe("Solution", function() {
- it("should give multiplication table from 1 to 5", function() {
- assert.strictEqual(multiplicationTable(),
- `1 * 1 = 1
- 1 * 2 = 2
- 1 * 3 = 3
- 1 * 4 = 4
- 1 * 5 = 5
- 1 * 6 = 6
- 1 * 7 = 7
- 1 * 8 = 8
- 1 * 9 = 9
- 1 * 10 = 10
- 1 * 11 = 11
- 1 * 12 = 12
- 2 * 1 = 2
- 2 * 2 = 4
- 2 * 3 = 6
- 2 * 4 = 8
- 2 * 5 = 10
- 2 * 6 = 12
- 2 * 7 = 14
- 2 * 8 = 16
- 2 * 9 = 18
- 2 * 10 = 20
- 2 * 11 = 22
- 2 * 12 = 24
- 3 * 1 = 3
- 3 * 2 = 6
- 3 * 3 = 9
- 3 * 4 = 12
- 3 * 5 = 15
- 3 * 6 = 18
- 3 * 7 = 21
- 3 * 8 = 24
- 3 * 9 = 27
- 3 * 10 = 30
- 3 * 11 = 33
- 3 * 12 = 36
- 4 * 1 = 4
- 4 * 2 = 8
- 4 * 3 = 12
- 4 * 4 = 16
- 4 * 5 = 20
- 4 * 6 = 24
- 4 * 7 = 28
- 4 * 8 = 32
- 4 * 9 = 36
- 4 * 10 = 40
- 4 * 11 = 44
- 4 * 12 = 48
- 5 * 1 = 5
- 5 * 2 = 10
- 5 * 3 = 15
- 5 * 4 = 20
- 5 * 5 = 25
- 5 * 6 = 30
- 5 * 7 = 35
- 5 * 8 = 40
- 5 * 9 = 45
- 5 * 10 = 50
- 5 * 11 = 55
- 5 * 12 = 60`);
- });
- });
~ Fixed TestCases ~
function findSenior(list) { const maxAge = Math.max(...list.map(e => e.age)); return list.filter(e => e.age===maxAge); }
// Solution- function findSenior(list) {
let maxAge = 0;list.forEach((dev) => { // ho usato forEach per trovare l'età massima tra gli sviluppatoriif (dev.age > maxAge) {maxAge = dev.age;}});return list.filter((dev) => dev.age === maxAge); // ho usato filter per filtrare gli sviluppatori con l'età massima- const maxAge = Math.max(...list.map(e => e.age));
- return list.filter(e => e.age===maxAge);
- }
// 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"); function findSenior(list) { const maxAge = Math.max(...list.map(e => e.age)); return list.filter(e => e.age===maxAge); } const list1 = [ { firstName: "Gabriel", lastName: "X.", country: "Monaco", continent: "Europe", age: 49, language: "PHP", }, { firstName: "Odval", lastName: "Fabio.", country: "Mongolia", continent: "Asia", age: 38, language: "Python", }, { firstName: "Guea", lastName: "Serge.", country: "Italy", continent: "Europe", age: 24, language: "Java", }, { firstName: "Sou", lastName: "Bocc.", country: "Japan", continent: "Asia", age: 49, language: "PHP", }, ]; const list2 = [ { firstName: "Gabriel", lastName: "X.", country: "Monaco", continent: "Europe", age: 49, language: "PHP", }, { firstName: "Odval", lastName: "F.", country: "Mongolia", continent: "Asia", age: 38, language: "Python", }, { firstName: "Emilija", lastName: "S.", country: "Lithuania", continent: "Europe", age: 19, language: "Python", }, { firstName: "Sou", lastName: "B.", country: "Japan", continent: "Asia", age: 49, language: "PHP", }, { firstName: "Sou", lastName: "B.", country: "Japan", continent: "Asia", age: 49, language: "PHP", }, ]; describe("Solution", function() { it("should filter the most senior developer", function() { assert.strictEqual(JSON.stringify(findSenior(list1)), JSON.stringify([{ firstName: "Gabriel", lastName: "X.", country: "Monaco", continent: "Europe", age: 49, language: "PHP"},{ firstName: "Sou", lastName: "Bocc.", country: "Japan", continent: "Asia", age: 49, language: "PHP"}])); assert.strictEqual(JSON.stringify(findSenior(list2)),JSON.stringify([{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }])); })});
- // 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");
- function findSenior(list) {
- const maxAge = Math.max(...list.map(e => e.age));
- return list.filter(e => e.age===maxAge);
- }
- const list1 = [
- {
- firstName: "Gabriel",
- lastName: "X.",
- country: "Monaco",
- continent: "Europe",
- age: 49,
- language: "PHP",
- },
- {
- firstName: "Odval",
- lastName: "Fabio.",
- country: "Mongolia",
- continent: "Asia",
- age: 38,
- language: "Python",
- },
- {
- firstName: "Guea",
- lastName: "Serge.",
- country: "Italy",
- continent: "Europe",
- age: 24,
- language: "Java",
- },
- {
- firstName: "Sou",
- lastName: "Bocc.",
- country: "Japan",
- continent: "Asia",
- age: 49,
- language: "PHP",
- },
- ];
- const list2 = [
- {
- firstName: "Gabriel",
- lastName: "X.",
- country: "Monaco",
- continent: "Europe",
- age: 49,
- language: "PHP",
- },
- {
- firstName: "Odval",
- lastName: "F.",
- country: "Mongolia",
- continent: "Asia",
- age: 38,
- language: "Python",
- },
- {
- firstName: "Emilija",
- lastName: "S.",
- country: "Lithuania",
- continent: "Europe",
- age: 19,
- language: "Python",
- },
- {
- firstName: "Sou",
- lastName: "B.",
- country: "Japan",
- continent: "Asia",
- age: 49,
- language: "PHP",
- },
- {
- firstName: "Sou",
- lastName: "B.",
- country: "Japan",
- continent: "Asia",
- age: 49,
- language: "PHP",
- },
- ];
console.log(findSenior(list1)); // [{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]console.log(findSenior(list2)); // [{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' },// { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]function findSenior(list) {let maxAge = 0;list.forEach((dev) => { // ho usato forEach per trovare l'età massima tra gli sviluppatoriif (dev.age > maxAge) {maxAge = dev.age;}});return list.filter((dev) => dev.age === maxAge); // ho usato filter per filtrare gli sviluppatori con l'età massima}- describe("Solution", function() {
- it("should filter the most senior developer", function() {
- assert.strictEqual(JSON.stringify(findSenior(list1)), JSON.stringify([{ firstName: "Gabriel", lastName: "X.", country: "Monaco", continent: "Europe", age: 49, language: "PHP"},{ firstName: "Sou", lastName: "Bocc.", country: "Japan", continent: "Asia", age: 49, language: "PHP"}]));
- assert.strictEqual(JSON.stringify(findSenior(list2)),JSON.stringify([{ firstName: 'Gabriel', lastName: 'X.', country: 'Monaco', continent: 'Europe', age: 49, language:'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }, { firstName: 'Sou', lastName: 'B.', country: 'Japan', continent: 'Asia', age: 49, language: 'PHP' }]));
- })});
function addArr(arr){ return arr.reduce((a,c) => a+c,0)||null; }
- function addArr(arr){
if(arr.length === 0) return nulllet final = 0arr.forEach(num => {final += num})return final- return arr.reduce((a,c) => a+c,0)||null;
- }
const chai = require("chai"); const assert = chai.assert; describe("Solution", function() { it("should test for something", function() { assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15); assert.strictEqual(addArr([1, 100]), 101) assert.strictEqual(addArr([]), null) }); });
- const chai = require("chai");
- const assert = chai.assert;
- describe("Solution", function() {
- it("should test for something", function() {
- assert.strictEqual(addArr([1, 2, 3, 4, 5]), 15);
- assert.strictEqual(addArr([1, 100]), 101)
- assert.strictEqual(addArr([]), null)
- });
- });
Date Time
function ShowDate() { const months = {'Jan':'January','Feb':'February','Mar':'March','Apr':'April','May':'May', 'Jun':'June','Jul':'July','Aug':'Augest','Sep':'September','Oct':'October', 'Nov':'November','Dec':'December'}; const days = {'1':'st','2':'nd','3':'rd'}; return new Date().toDateString().slice(4).split(' ').map((e,i) => i?i==1?+e+(days[e.slice(-1)]||'th'):e:months[e]).join(' '); }
- function ShowDate() {
const currentDate = new Date();const monthsOfYear = ["January","February","March","April","May","June","July","August","September","October","November","December",];const currentMonth = monthsOfYear[currentDate.getMonth()];const currentDay = currentDate.getDate();let daySuffix;switch (currentDay) {case 1:case 21:case 31:daySuffix = "st";break;case 2:case 22:daySuffix = "nd";break;case 3:case 23:daySuffix = "rd";break;default:daySuffix = "th";}const currentYear = currentDate.getFullYear();return `${currentMonth} ${currentDay}${daySuffix} ${currentYear}`;- const months = {'Jan':'January','Feb':'February','Mar':'March','Apr':'April','May':'May',
- 'Jun':'June','Jul':'July','Aug':'Augest','Sep':'September','Oct':'October',
- 'Nov':'November','Dec':'December'};
- const days = {'1':'st','2':'nd','3':'rd'};
- return new Date().toDateString().slice(4).split(' ').map((e,i) => i?i==1?+e+(days[e.slice(-1)]||'th'):e:months[e]).join(' ');
- }
Using replace method
const firstNonRepeatingCharacter = str => { const obj = [...str].reduce((a,c) => {a[c]?a[c]++:a[c]=1;return a},{}); return Object.keys(obj).find(e => obj[e]===1)||null; };
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- const firstNonRepeatingCharacter = str => {
- const obj = [...str].reduce((a,c) => {a[c]?a[c]++:a[c]=1;return a},{});
- return Object.keys(obj).find(e => obj[e]===1)||null;
- };
shorter code chars..
n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join`` d=s=>[...s].map(e=>(+e&&e<6?"aeiou"[e-1]:e)).join``
- n=s=>[...s].map(e=>("aeiou".includes(e)?"aeiou".indexOf(e)+1:e)).join``
d=s=>[...s].map(e=>("12345".includes(e)?"aeiou"[e-1]:e)).join``- d=s=>[...s].map(e=>(+e&&e<6?"aeiou"[e-1]:e)).join``