codewars 3문제 풀이

samuel Jo·2023년 6월 28일
0

codewars

목록 보기
26/47

1. VowelsCount

Description
Return the number (count) of vowels in the given string.
We will consider a, e, i, o, u as vowels for this Kata (but not y).
The input string will only consist of lower case letters and/or spaces.

/**직관적으로 푼 방법 */
function getCount1(str) {
  str = str.toLowerCase();
  const vowels = ["a", "e", "i", "o", "u"];
  let count = 0;

  for (let i = 0; i < str.length; i++) {
    if (vowels.includes(str[i])) {
      count++;
    }
  }
  return count;
}
/**다른 풀이방법 */
function getCount(str) {
  str = str.toLowerCase();
  return str.split("").filter((c) => "aeiou".includes(c)).length;
}

2.CategorizeNewMembers

Description

The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed.
To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.

Input

Input will consist of a list of pairs. Each pair contains information for a single potential member. Information consists of an integer for the person's age and an integer for the person's handicap.

Output

Output will consist of a list of string values (in Haskell and C: Open or Senior) stating whether the respective member is to be placed in the senior or open category.

Example

input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]
output = ["Open", "Open", "Senior", "Open", "Open", "Senior"]

function openOrSenior(data){
    return data.map(([age, handicap]) => (age > 54 && handicap > 7) ? 'Senior' : 'Open');
  }

문제 요구에 비해 코드는 간결하다.


3.Playing_with_digits

Description

Some numbers have funny properties. For example:
89 --> 8¹ + 9² = 89 1
695 --> 6² + 9³ + 5⁴= 1390 = 695
2
46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p
we want to find a positive integer k, if it exists, such that the sum of the digits of n taken to the successive powers of p is equal to k * n.

In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k
If it is the case we will return k, if not return -1.

Note: n and p will always be given as strictly positive integers.

result

digPow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 1
digPow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92
k
digPow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 2
digPow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288
51

function digPow(n, p) {
    //1.첫번째 n을 toString() 써서 length로 접근 가능하게 만들기.
  let Changes = n.toString();
  let res = 0;
  for (let i = 0; i < Changes.length; i++) {
    res += Changes[i] ** (i + p);
  }
  //2. res가 n으로 딱 나눠지면 몫을 반환하게 한다.
  if (res % n === 0) {
    return res / n;
  } else {
    return -1;
  }
}
console.log(digPow(695,2)); // 2 
console.log(digPow(4333214,4)); // -1

6kyu 문제라 살짝 어려울줄 알았는데, 키보드를 치기 전 노트에 문제 요구사항을 써내려가다 보니 방법을 찾을 수 있었다.

사실 Math.pow();를 써도 되지만 **연산자를 써서 좀 더 직관적인 코드를 짜봤다.

마지막 문제는 꽤 재밌는 축에 속한 느낌.


profile
step by step

0개의 댓글