코플릿/알고리즘

07_[조합] 블랙잭은 지겨워

테오구 2021. 12. 7. 17:56
728x90

문제

평범한 블랙잭 게임에서 수시로 패배하자 흥미가 떨어진 김코딩은 박타짜에게 게임룰을 변형하여 새로운 카드 놀이를 해 볼 것을 제안합니다. 새로운 룰은 다음과 같습니다.

1. 숫자로 이루어진 카드를 여러 장 받습니다.
2. 3장씩 카드를 고르고, 3장에 적힌 숫자들의 합이 소수인지 확인합니다.
3. 받아든 카드로 만들 수 있는 소수의 개수가 많은 사람이 이기게 됩니다.

예로, [1, 2, 3, 4]라는 카드를 받았을 때 만들 수 있는 숫자는 6, 7, 8, 9이고, 소수는 7 하나이기 때문에 가지고 있는 소수의 개수는 1개입니다. [2, 3, 4, 8, 13]라는 카드를 받았을 때 만들 수 있는 숫자는 9, 13, 18, 14, 19, 23, 15, 20, 24, 25이고, 소수는 13, 19, 23 총 3개이기 때문에 가지고 있는 소수의 개수는 3개입니다.

게임을 진행하기 전에 소수에 대해 아무런 지식이 없는 박타짜는 게임을 며칠 미룬 뒤, 게임의 룰을 따르는 함수를 만들기로 했습니다. 소수에 약한 박타짜를 도와 여러 장의 카드 중 세 장씩 조합해 소수가 되는 경우의 수를 리턴하는 함수를 완성해 주세요.

 

입출력 예시

let output = boringBlackjack([1, 2, 3, 4]);
console.log(output); // 1

let output = boringBlackjack([2, 3, 4, 8, 13]);
console.log(output); // 3

 

더보기
// function combination(arr, selectNum) {
//   const result = [];
//   if (selectNum === 1) return arr.map((v) => [v]);
//   arr.forEach((v, idx, arr) => {
//     const fixed = v;
//     const restArr = arr.slice(idx + 1);
//     const combinationArr = combination(restArr, selectNum - 1);
//     const combineFix = combinationArr.map((v) => [fixed, ...v]);
//     result.push(...combineFix);
//   });
//   return result;
// }

 

// function isOdd(num) {
//   if(num < 0){
//     return isOdd(-num)
//   }
//   else if(num === 0){
//     return false
//   }
//   else if(num === 1){
//     return true
//   }
//   return isOdd(num - 2)
// }

 

// function boringBlackjack(cards) {
//   // TODO: 여기에 코드를 작성합니다.
//   const arr = []
//   const count = 0
//   const semiResult = combination(cards, 3)
//   // elem
//   // > Array [1, 2, 3]
//   // > Array [1, 2, 4]
//   // > Array [1, 3, 4]
//   // > Array [2, 3, 4]
//   semiResult.map((elem) => {
//     console.log(elem)
//     // arr = [6, 7, 8, 9] 이 됨
//     arr.push(elem.reduce((prev, curr) => prev + curr, 0))
//   })

 

//   for(let i = 0; i < arr.length; i++){
//     if(isOdd(arr[i]) === true){
//       count++
//     }
//   }
//   return count
// }
function boringBlackjack(cards) {
  // TODO: 여기에 코드를 작성합니다.
  const getCombinations = (arr, choiceNum) => {
    const results = [];
    if (choiceNum === 1) {
      return arr.map((element) => [element]);
    }
    arr.forEach((fixed, index, origin) => {
      const rest = origin.slice(index + 1);
      const combinations = getCombinations(rest, choiceNum - 1);
      const attached = combinations.map((combination) => [fixed, ...combination]);
      results.push(...attached);
    });
    return results;
  }
  
  let count = 0
  let sumArr = getCombinations(cards, 3).map(el => el.reduce((a, b) => a + b))
  for (let el of sumArr) {
    if (isPrime(el) === true) {
      count++
    }
  }
  return count
}
function isPrime(num) {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) {
      return false;
    }
  }
  return num > 1;
}

 

728x90

'코플릿 > 알고리즘' 카테고리의 다른 글

08_[GCD] 빼빼로 데이  (0) 2021.12.07
06_[순열] 새로운 치킨 소스 레시피  (0) 2021.12.07
05_rockPaperScissors  (0) 2021.12.07
03_[구현] 보드 게임  (0) 2021.12.07
02_[Greedy] 편의점 알바  (0) 2021.12.07