const plus= (a,b,p) => a%1||b%1?plus(+(a+"0").replace(/(\.)(\d)/,"$2$1"),+(b+"0").replace(/(\.)(\d)/,"$2$1"),p+1):(a+b)/Math.pow(10,p) const sum = arr => arr.reduce((a,b)=>0+b===b?plus(a,b,0):a,0)
const sum = arr => {const reducer = (sum, elem) => typeof elem === 'number' ? (sum + elem) : sumreturn arr.reduce(reducer, 0);}- const plus= (a,b,p) => a%1||b%1?plus(+(a+"0").replace(/(\.)(\d)/,"$2$1"),+(b+"0").replace(/(\.)(\d)/,"$2$1"),p+1):(a+b)/Math.pow(10,p)
- const sum = arr => arr.reduce((a,b)=>0+b===b?plus(a,b,0):a,0)
describe("Sum", function(){ it("should sum correctly", function(){ Test.assertEquals(sum([5,2,1]), 8, "Sum equals to 8"); }); it("should sum only numbers and skip the rest", function(){ Test.assertEquals(sum([5, 'a', 2, '$', 1]), 8, "Sum equals to 8"); Test.assertEquals(sum([5, '3', 2, '$', 1]), 8, "Sum equals to 8"); Test.assertEquals(sum([5, '3', 2, Infinity, 1]), Infinity, "Sum equals to Infinity"); }); it("should sum only numbers and calculate the correct number", function(){ Test.assertEquals(sum([5, '3', 2, '$', 1, NaN]), 8, "Sum equals to 8"); Test.assertEquals(sum(['3', 0.2, '$', 0.1]), 0.3, "Sum equals to 0.3"); Test.assertEquals(sum(['3', 0.12, '$', 3.456,7.89]), 11.466, "Sum equals to 11.466"); }); });
- describe("Sum", function(){
- it("should sum correctly", function(){
- Test.assertEquals(sum([5,2,1]), 8, "Sum equals to 8");
- });
- it("should sum only numbers and skip the rest", function(){
- Test.assertEquals(sum([5, 'a', 2, '$', 1]), 8, "Sum equals to 8");
- Test.assertEquals(sum([5, '3', 2, '$', 1]), 8, "Sum equals to 8");
- Test.assertEquals(sum([5, '3', 2, Infinity, 1]), Infinity, "Sum equals to Infinity");
- });
- it("should sum only numbers and calculate the correct number", function(){
- Test.assertEquals(sum([5, '3', 2, '$', 1, NaN]), 8, "Sum equals to 8");
- Test.assertEquals(sum(['3', 0.2, '$', 0.1]), 0.3, "Sum equals to 0.3");
- Test.assertEquals(sum(['3', 0.12, '$', 3.456,7.89]), 11.466, "Sum equals to 11.466");
- });
- });
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Your task is to find the sum of all the multiples of 3 or 5 below n
.
function sumOfMultiplesUnder(n) { return sumAll(--n,3)+sumAll(n,5)-sumAll(n,15) } function sumAll(n,m){ return m*(Math.floor(n/m)+1)*Math.floor(n/m)/2 }
function calculate_multiples {$SUM = 01..999 | ForEach-Object {if (!( $_ % 3) -OR !($_ % 5)) {$Sum += $_}}return $Sum- function sumOfMultiplesUnder(n) {
- return sumAll(--n,3)+sumAll(n,5)-sumAll(n,15)
- }
- function sumAll(n,m){
- return m*(Math.floor(n/m)+1)*Math.floor(n/m)/2
- }
describe("Basic Tests", function(){ it("It should works for basic tests.", function(){ Test.assertEquals(sumOfMultiplesUnder(10),23) Test.assertEquals(sumOfMultiplesUnder(100),2318) Test.assertEquals(sumOfMultiplesUnder(1000),233168) Test.assertEquals(sumOfMultiplesUnder(10000),23331668) Test.assertEquals(sumOfMultiplesUnder(100000),2333316668) Test.assertEquals(sumOfMultiplesUnder(1000000),233333166668) Test.assertEquals(sumOfMultiplesUnder(10000000),23333331666668) })})
# You can test with Pester (https://github.com/pester/Pester)# TODO: replace with your own tests (TDD), these are just here to demonstrate usage.$Results = calculate_multiplesDescribe 'My Solution' {It 'Should return 233168' {$Results | Should be 233168}}- describe("Basic Tests", function(){
- it("It should works for basic tests.", function(){
- Test.assertEquals(sumOfMultiplesUnder(10),23)
- Test.assertEquals(sumOfMultiplesUnder(100),2318)
- Test.assertEquals(sumOfMultiplesUnder(1000),233168)
- Test.assertEquals(sumOfMultiplesUnder(10000),23331668)
- Test.assertEquals(sumOfMultiplesUnder(100000),2333316668)
- Test.assertEquals(sumOfMultiplesUnder(1000000),233333166668)
- Test.assertEquals(sumOfMultiplesUnder(10000000),23333331666668)
- })})
Arrays
Data Types
function solution (nums){ if (nums.length<2) return nums.join("") var lens=nums.map(x=>(x+"").length).sort((a,b)=>b-a),maxlen=lens[0]+lens[1],zeros="0".repeat(maxlen), firsttwo=nums.reduce((a,b,i)=>a.concat(nums.map((c,j)=>i==j?[0,0,"0"]:[b,c,(""+b+c+zeros).slice(0,maxlen)])),[]).sort((a,b)=>b[2]-a[2]).filter((x,i,ar)=>x[2]==ar[0][2]) return firsttwo.map(x=>""+x[0]+x[1]+solution(nums.filter((y,i)=>i!=nums.indexOf(x[0])&&i!=nums.indexOf(x[1])))).sort((a,b)=>b-a)[0] }
def solution (nums)# slower but simple ;-)nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0]end- function solution (nums){
- if (nums.length<2) return nums.join("")
- var lens=nums.map(x=>(x+"").length).sort((a,b)=>b-a),maxlen=lens[0]+lens[1],zeros="0".repeat(maxlen),
- firsttwo=nums.reduce((a,b,i)=>a.concat(nums.map((c,j)=>i==j?[0,0,"0"]:[b,c,(""+b+c+zeros).slice(0,maxlen)])),[]).sort((a,b)=>b[2]-a[2]).filter((x,i,ar)=>x[2]==ar[0][2])
- return firsttwo.map(x=>""+x[0]+x[1]+solution(nums.filter((y,i)=>i!=nums.indexOf(x[0])&&i!=nums.indexOf(x[1])))).sort((a,b)=>b-a)[0]
- }
Test.assertSimilar(solution([5, 50, 53,53]), "5535350") Test.assertSimilar(solution([1, 3, 5, 4, 7, 6, 8]), "8765431")
Test.describe "Basic tests..." do- Test.assertSimilar(solution([5, 50, 53,53]), "5535350")
- Test.assertSimilar(solution([1, 3, 5, 4, 7, 6, 8]), "8765431")
Test.assert_equals(solution([5, 50, 56]), "56550")Test.assert_equals(solution([1, 3, 5, 4, 7, 6, 8]), "8765431")end
def solution (nums) # slower but simple ;-) nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0] end
import java.util.*;class Solution {public static String largestNumber(Integer[] nums) {Arrays.sort( nums, new Comparator<Integer>() {@Overridepublic int compare(Integer a, Integer b) {String aStr = a.toString();String bStr = b.toString();return (aStr + bStr).compareTo(bStr + aStr) * -1;}} );String result = "";for(Integer num : nums) {result += num.toString();}return result;}}- def solution (nums)
- # slower but simple ;-)
- nums.permutation.to_a.map{|x| x.join("")}.sort{|a,b| b.to_i-a.to_i}[0]
- end
Test.describe "Basic tests..." do Test.assert_equals(solution([5, 50, 56]), "56550") Test.assert_equals(solution([1, 3, 5, 4, 7, 6, 8]), "8765431") end
import org.junit.Test;import static org.junit.Assert.assertEquals;import org.junit.runners.JUnit4;public class SolutionTest {@Testpublic void test1() {assertEquals( "56550", Solution.largestNumber(new Integer[] {5, 50, 56}) );}@Testpublic void test2() {assertEquals( "8765431", Solution.largestNumber(new Integer[] {1, 3, 5, 4, 7, 6, 8}) );}}- Test.describe "Basic tests..." do
- Test.assert_equals(solution([5, 50, 56]), "56550")
- Test.assert_equals(solution([1, 3, 5, 4, 7, 6, 8]), "8765431")
- end