Ad
Algorithms
Logic
Algebra
Mathematics
Code
Diff
  • #include <math.h>
    
    int sumMultiples (int N, int k){
      int  countMultiples = floor(N/k);
      int  sumMultiples = 0.5*k*countMultiples*(countMultiples+1);
      return sumMultiples;
    }
    
    int solution (int N, int a, int b){
      return sumMultiples(N, a)+sumMultiples(N, b)-sumMultiples(N, a*b);
    }
    
    
    • using System;
    • #include <math.h>
    • namespace Kumite
    • {
    • public class Problem
    • {
    • public static long Sum(int N)
    • {
    • // Add your code here.
    • return 0;
    • }
    • }
    • int sumMultiples (int N, int k){
    • int countMultiples = floor(N/k);
    • int sumMultiples = 0.5*k*countMultiples*(countMultiples+1);
    • return sumMultiples;
    • }
    • int solution (int N, int a, int b){
    • return sumMultiples(N, a)+sumMultiples(N, b)-sumMultiples(N, a*b);
    • }
Algorithms
Logic
Algebra
Mathematics
Code
Diff
  • const countMultiplesOfKUpToN = (N, k) => Math.floor(N/k),
          sumLMultiplesOfN = (l, n) => 0.5*n*l*(l+1),
          sumMultiplesOfAOrBUpToN = (a, b, N) => {
            const lA = countMultiplesOfKUpToN(N, a),
                  lB = countMultiplesOfKUpToN(N, b),
                  lAB = countMultiplesOfKUpToN(N, a*b),
                  sA = sumLMultiplesOfN(lA, a),
                  sB = sumLMultiplesOfN(lB, b),
                  sAB = sumLMultiplesOfN(lAB, a*b)
            return sA+sB-sAB
          }
    
    console.log(sumMultiplesOfAOrBUpToN(3,7,30))
    console.log(sumMultiplesOfAOrBUpToN(3,7,500000000))
    
    • using System;
    • const countMultiplesOfKUpToN = (N, k) => Math.floor(N/k),
    • sumLMultiplesOfN = (l, n) => 0.5*n*l*(l+1),
    • sumMultiplesOfAOrBUpToN = (a, b, N) => {
    • const lA = countMultiplesOfKUpToN(N, a),
    • lB = countMultiplesOfKUpToN(N, b),
    • lAB = countMultiplesOfKUpToN(N, a*b),
    • sA = sumLMultiplesOfN(lA, a),
    • sB = sumLMultiplesOfN(lB, b),
    • sAB = sumLMultiplesOfN(lAB, a*b)
    • return sA+sB-sAB
    • }
    • namespace Kumite
    • {
    • public class Problem
    • {
    • public static long Sum(int N)
    • {
    • // Add your code here.
    • return 0;
    • }
    • }
    • }
    • console.log(sumMultiplesOfAOrBUpToN(3,7,30))
    • console.log(sumMultiplesOfAOrBUpToN(3,7,500000000))
Code
Diff
  • const AbbreviateTwoWords = s => {
      const [first, middle, ...rest] = s.split(' ')
      return `${first.slice(0,1)}.${middle.slice(0,1)}. ${rest}`
    }
    • public class AbbreviateTwoWords {
    • public static String abbrevName(String name) {
    • String[] names = name.split(" ");
    • return (names[0].charAt(0) + "." + names[1].charAt(0)).toUpperCase();
    • }
    • const AbbreviateTwoWords = s => {
    • const [first, middle, ...rest] = s.split(' ')
    • return `${first.slice(0,1)}.${middle.slice(0,1)}. ${rest}`
    • }
Code
Diff
  • const rot13 = s => s.replace(/[a-z]/g, m => String.fromCharCode(97+(m.charCodeAt(0)-97+13)%26))
    • def rot13(s):
    • return rot.encode(s,"rot_13")
    • const rot13 = s => s.replace(/[a-z]/g, m => String.fromCharCode(97+(m.charCodeAt(0)-97+13)%26))
Code
Diff
  • const decrypt = (s,d,a='ABCDEFGHIJKLMNOPQRSTUVWXYZ') => s.replace(/[A-Z]/g, m => a[(a.indexOf(m)+26-d)%26])
    • def decrypt(code,amount):
    • #place code here
    • let="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    • word = ""
    • x=0
    • for c in range(0,len(code)):
    • for i in range (0,len(let)):
    • if code[c] == " ":
    • word+=" "
    • break
    • elif code[c] == let[i]:
    • x=i
    • x-=amount
    • if x < 0:
    • x+=26
    • word+=let[x]
    • break
    • return word
    • const decrypt = (s,d,a='ABCDEFGHIJKLMNOPQRSTUVWXYZ') => s.replace(/[A-Z]/g, m => a[(a.indexOf(m)+26-d)%26])
Code
Diff
  • const rotateLeft = arr => arr.map((row,i,s) => row.map((_,j) => s[j][s.length-i-1]))
    const rotateRight = arr => arr.map((row,i,s) => row.map((_,j) => s[s.length-j-1][i]))
    • const rotateLeft = (arr) => {
    • let retArr = [[],[],[]]
    • for (let i = 0; i < arr.length; i++){
    • for (let j = 0; j < arr[i].length; j++){
    • retArr[i][j] = arr[j][arr.length - (i + 1)]
    • }
    • }
    • return retArr
    • }
    • const rotateRight = (arr) => {
    • let retArr = [[],[],[]]
    • for (let i = 0; i < arr.length; i++){
    • for (let j = 0; j < arr[i].length; j++){
    • retArr[i][j] = arr[arr.length - (j + 1)][i]
    • }
    • }
    • return retArr
    • }
    • const rotateLeft = arr => arr.map((row,i,s) => row.map((_,j) => s[j][s.length-i-1]))
    • const rotateRight = arr => arr.map((row,i,s) => row.map((_,j) => s[s.length-j-1][i]))
Code
Diff
  • with(Math)highAndLow=s=>max(...s=s.match(/-?\d+/g))+` `+min(...s)
    • highAndLow=a=>Math.max(...a=a.split` `)+` `+Math.min(...a)
    • with(Math)highAndLow=s=>max(...s=s.match(/-?\d+/g))+` `+min(...s)
Code
Diff
  • const getIDS = n => [...`${n}`].reduce((r,n) => r+Number(n),0)
    • function getIDS($number) {
    • return array_sum(str_split($number));
    • }
    • const getIDS = n => [...`${n}`].reduce((r,n) => r+Number(n),0)

When asked about closest to N integer divisible by n, you might think of a neat and simple n*Math.round(N/n).

But, is there anything even more concise and fast?

closestMultiple=(N,n)=>n*(N/n+0.5>>0)
Mathematics
Algorithms
Logic
Numbers
Data Types
Code
Diff
  • const average = grades => !grades.length ? undefined : grades.reduce((avg,score,_,self) => avg+score/self.length, 0)
    • function average(grades) {
    • if (grades && grades.length) {
    • return grades.reduce((total, grade) => total + grade, 0) / grades.length
    • }
    • }
    • const average = grades => !grades.length ? undefined : grades.reduce((avg,score,_,self) => avg+score/self.length, 0)

In JavaScript the most popular way of inverting strings would be something, similar to [...str].reverse().join('').

And what would be your unusual way?

const invert = str => str.replace(/./g, (_,o,s) => s.charAt(s.length-1-o));
Code
Diff
  • const multiples = (a,b,max) => Array.from({length:~~(max/(a*b))}, (_,i) => (i+1)*a*b)
    • #!/bin/bash
    • for i in $(eval echo {1..$3});
    • do
    • if [ `expr $i % $1` -eq 0 -a `expr $i % $2` -eq 0 ]
    • then
    • echo $i
    • fi
    • done
    • const multiples = (a,b,max) => Array.from({length:~~(max/(a*b))}, (_,i) => (i+1)*a*b)
Code
Diff
  • const det = m => 
      m.length == 1 ?
      m[0][0] :
      m.length == 2 ?
      m[0][0]*m[1][1]-m[0][1]*m[1][0] :
      m[0].reduce((r,e,i) => r+(-1)**(i+2)*e*det(m.slice(1).map(c => c.filter((_,j) => i != j))),0);
    
    
    
    • double Matrix::GetValueOfDeterminant(){
    • if((2 == MaxRow)&&(2 == MaxCol)){
    • return GetElement(1,1)* GetElement(2,2) - GetElement(1,2) * GetElement(2,1);}
    • else{double ResultValue = 0;for(int c = 1; c <= MaxCol; c++){int PowOfNegativeOne = std::pow(-1, c);
    • ResultValue += GetCofactorMatrix(1,c).GetValueOfDeterminant() * GetElement(1,c) * PowOfNegativeOne;}return ResultValue;}}
    • const det = m =>
    • m.length == 1 ?
    • m[0][0] :
    • m.length == 2 ?
    • m[0][0]*m[1][1]-m[0][1]*m[1][0] :
    • m[0].reduce((r,e,i) => r+(-1)**(i+2)*e*det(m.slice(1).map(c => c.filter((_,j) => i != j))),0);
Arrays
Data Types
Algorithms
Logic
Data
Code
Diff
  • const pairs = (arr,twin) => 
      (twin = arr.find(element => arr.includes((-1)*element)), [twin, (-1)*twin].sort())
    • function pairs(arr){
    • let sorted = arr.sort();
    • let length = arr.length;
    • for (let i = 0; i < length; i++) {
    • if (sorted.includes(arr[i] * -1)) {
    • return [sorted[i], sorted[i] * -1];
    • }
    • }
    • }
    • const pairs = (arr,twin) =>
    • (twin = arr.find(element => arr.includes((-1)*element)), [twin, (-1)*twin].sort())
Code
Diff
  • const maxProductOf3 = arr =>
      (arr = arr.sort((a,b) => a-b),
      Math.max(arr[0]*arr[1]*arr[arr.length-1],arr.slice(-3).reduce((p,e) => p*e,1)))
    • def maximum_product_of_three(lst):
    • max_pr = 0
    • num_num = 0
    • for num in lst:
    • for i in range(0,len(lst)):
    • if i != lst[num_num] and num_num+2 < len(lst):
    • try:
    • if (num*lst[i+1]*lst[i+2])>max_pr:
    • max_num = num*lst[i+1]*lst[i+2]
    • except:
    • pass
    • num_num =+ 1
    • return max_num
    • const maxProductOf3 = arr =>
    • (arr = arr.sort((a,b) => a-b),
    • Math.max(arr[0]*arr[1]*arr[arr.length-1],arr.slice(-3).reduce((p,e) => p*e,1)))
Loading more items...