Throwing Darts ( 6 kyu )

문제

You’ve just recently been hired to calculate scores for a Dart Board game!

Scoring specifications:

0 points - radius above 10
5 points - radius between 5 and 10 inclusive
10 points - radius less than 5
If all radiuses are less than 5, award 100 BONUS POINTS!

Write a function that accepts an array of radiuses (can be integers and/or floats), and returns a total score using the above specification.

An empty array should return 0.

Examples:

scoreThrows( [1, 5, 11] )    =>  15
scoreThrows( [15, 20, 30] )  =>  0
scoreThrows( [1, 2, 3, 4] )  =>  140

다트 점수내기, 4발 던져서 1,2,3,4에 넣으면 100점추가

풀이

function scoreThrows(radiuses){
  let point = [], result = 0;
  for ( let i of radiuses ) {
    if ( i < 5 ) {
      result += 10;
      point.push(i)
    } else if ( i >=5 && i <= 10 ) {
      result += 5;
    }
  }
  if ( [...new Set(point)].length === 4 && radiuses.length === 4 ) result += 100
  return result;
}

케이스를 많이 나눠 생각했는데 테스트케이스가 얼마 없어서 의미가 없어짐

다른 사람의 풀이

function scoreThrows(a){
 var result = a.reduce(function(p,c){
   return p + (c <5 ? 10 : c > 10 ? 0 : 5);
 },0) ;
 return result/a.length===10?result+100:result;
}

간단하게 푼 풀이


Sorting on planet Twisted-3-7 ( 6 kyu )

문제

There is a planet… in a galaxy far far away. It is exactly like our planet, but it has one difference:

The values of the digits 3 and 7 are twisted. Our 3 means 7 on the planet Twisted-3-7. And 7 means 3.

Your task is to create a method, that can sort an array the way it would be sorted on Twisted-3-7.

7 Examples from a friend from Twisted-3-7:

[1,2,3,4,5,6,7,8,9] -> [1,2,7,4,5,6,3,8,9]
[12,13,14] -> [12,14,13]
[9,2,4,7,3] -> [2,7,4,3,9]

3과 7이 바뀌었다 가정하고 정렬하는 문제

풀이

function sortTwisted37(array) {
  function change(val) {
    let result = val.toString().split('');
    for ( let i = 0; i < result.length; i++ ) {
      if ( result[i] === '3' ) result[i] = '7';
      else if ( result[i] === '7' ) result[i] = '3';
    }
    return Number(result.join(''));
  }
  let arr = array.slice().map( a => change(a) );
  arr.sort((a,b) => a-b);
  return arr.map( a => change(a) )
}

3,7을 바꿔 정렬 후 다시 바꿔줌

다른 사람의 풀이

function sortTwisted37(array) {
  const MAPPING = {7:3,3:7};

  return array.slice().sort((a,b) => twist(a) - twist(b));

  function twist(number) {
    return parseInt(number.toString().replace(/[37]/g,a=>MAPPING[a]));
  }
}

replace로 동시에 처리하려는 방법을 생각했는데 이렇게 하는 방법이 있었다.

왕꿀팁


Integer depth ( 6 kyu )

문제

The depth of an integer n is defined to be how many multiples of n it is necessary to compute before all 10 digits have appeared at least once in some multiple.

example:

let see n=42

Multiple         value         digits     comment
42*1              42            2,4
42*2              84             8         4 existed
42*3              126           1,6        2 existed
42*4              168            -         all existed
42*5              210            0         2,1 existed
42*6              252            5         2 existed
42*7              294            9         2,4 existed
42*8              336            3         6 existed
42*9              378            7         3,8 existed

Looking at the above table under digits column you can find all the digits from 0 to 9, Hence it required 9 multiples of 42 to get all the digits. So the depth of 42 is 9. Write a function named computeDepth which computes the depth of its integer argument.Only positive numbers greater than zero will be passed as an input.

몇까지 곱했을 때 0부터 9까지 나오는지 구하는 문제

풀이

function computeDepth (x){
  let result = '', count =1
  while ( [...new Set(result)].length < 10 ) {
    result += x*count;
    count++;
  }
  return count-1;
}

Set을 이용해 중복을 제거해 length로 10이 될때까지 반복문을 돌며 count

다른 사람의 풀이

별로임


+ Recent posts