DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #142 - Parts of a Whole

Given a long number, write a function to return all possible pairs of addends and the sum of each pair.

For example, 12,345: all of the addends from that number are:
[ 1 + 2, 1 + 3, 1 + 4, 1 + 5, 2 + 3, 2 + 4, 2 + 5, 3 + 4, 3 + 5, 4 + 5 ]

Therefore the result must be:
[ 3, 4, 5, 6, 5, 6, 7, 7, 8, 9 ]

Test Cases
156
81596
3852
3264128
999999

Good luck!


This challenge comes from Zorianff9 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Oldest comments (8)

Collapse
 
martinpham profile image
Martin Pham • Edited

My first daily challenge :)

Javascript

const input = 999999

const output = []

Array.prototype.hasPair = function([a, b])
{
    return this.findIndex(([_a, _b]) => (a === _a && b === _b) || (a === _b && b === _a)) > -1
}

const inputString = input + ''
for(let i = 0; i < inputString.length; i++)
{
    for(let j = i + 1; j < inputString.length; j++)
    {
        const pair = [+inputString[i], +inputString[j]]

        if(!output.hasPair(pair))
        {
            output.push(pair)
        }
    }
}

const outputSum = output.map(([a, b]) => a + b)

console.log(outputSum)

Live demo codesandbox.io/s/festive-sun-8eno0

Collapse
 
vaibhavyadav1998 profile image
Vaibhav Yadav

In Python.

def sum(num):
  nums = list(str(num))
  result = []

  for i in range(len(nums)):
    for j in range(len(nums)):
      if i < j:
        result.append(int(nums[i]) + int(nums[j]))


  return result
Collapse
 
martinpham profile image
Martin Pham

you will have duplicated pairs

Collapse
 
bamartindev profile image
Brett Martin

Small JavaScript answer - trying to think how I would do this with a reduce for fun:

const digits = num => {
    const result = [];
    const parts = String(num).split('').map(n => n | 0);
    for(let i = 0; i < parts.length; i++) {
        for (let j = i + 1; j < parts.length; j++) {
            result.push(parts[i] + parts[j]);
        }
    }
    return result;
};
Collapse
 
sabbin profile image
Sabin Pandelovitch

something like this maybe

const prepareArray = nr => String(nr).split('').map(Number);
const printPairs = arr => arr.reduce((acc, val, index) => {
  for(let i = index+1; i < arr.length; i++){
    acc.push(val+arr[i]);
  }
  return acc;
}, []);

console.log(printPairs(prepareArray(12345)))
Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell! Uses the TupleSections extension to the compiler to make the pairs function nicer.

{-# LANGUAGE TupleSections #-} 

import Data.Char (digitToInt) 

digits :: Int -> [Int] 
digits = map digitToInt . show

pairs :: [a] -> [(a, a)] 
pairs (x:[]) = [] 
pairs (x:xs) = map (x,) xs ++ pairs xs

allSums :: Int -> [Int] 
allSums = map (uncurry (+)) . pairs . digits
Collapse
 
avalander profile image
Avalander

Scala

object whole extends App {
  def solve (x: Int): List[Int] = {
    @tailrec
    def rec (xs: Seq[Int], result: List[Int]): List[Int] =
      xs match {
        case Nil       => result
        case x :: rest => {
          val next = rest map (_ + x)
          rec(rest, result ++ next)
        }
      }
    rec(toDigits(x), List())
  }

  private def toDigits (x: Int): List[Int] =
    x.toString.map(_.asDigit).toList

  println(solve(156))     // List(6, 7, 11)
  println(solve(81596))   // List(9, 13, 17, 14, 6, 10, 7, 14, 11, 15)
  println(solve(3852))    // List(11, 8, 5, 13, 10, 7)
  println(solve(3264128)) // List(5, 9, 7, 4, 5, 11, 8, 6, 3, 4, 10, 10, 7, 8, 14, 5, 6, 12, 3, 9, 10)
  println(solve(999999))  // List(18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18)
}
Collapse
 
tylerwel profile image
tyler-wel

Sorry if this is off topic, but does everyone usually code answers for small things like this? Local? Codepen? Other sites?