본문 바로가기
알고리즘/Codewars

[Codewars] Categorize New Member (7 kyu) / JavaScript

by fluss 2022. 10. 6.

https://www.codewars.com/kata/5502c9e7b3216ec63c0001aa

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

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"]
 

설명:

웨스턴 교외 크로켓 클럽은 시니어와 오픈 두 가지 유형의 회원이 있습니다. 그들은 장래의 회원들에게 그들이 어느 유형에 배치될지를 알려주는 신청서를 만드는데 당신의 도움을 바랍니다.

시니어가 되려면 회원은 최소 55살 이상이어야 하며 7 이상의 핸디캡을 가져야 합니다. 이 크로켓 클럽에서 핸디캡의 범위는 -2에서 +26이고 더 잘하는 플레이어가 핸디캡이 더 낮습니다.

 

입력

입력은 쌍들의 리스트로 구성될 것입니다. 각 쌍은 하나의 잠재적인 회원의 정보를 포함합니다. 정보는 사람의 나이의 정수와 핸디캡의 정수로 구성되어있습니다.

 

출력

출력은 각각의 회원이 시니어 또는 오픈 유형에 배치되는지의 상태를 나타내는 문자열 값의 리스트로 구성되어 있습니다. (Haskell과 C에서는 오픈 또는 시니어)

 

예시

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

풀이

function openOrSenior(data){
  let result = [];
  for(let i = 0; i < data.length; i++){
    if(data[i][0] >= 55 && data[i][1] > 7){
      result.push('Senior');
    } else{
      result.push('Open');
    }
  }
  return result;
}

data[i]의 첫 번째 위치가 나이 두 번째 위치가 핸디캡이므로 data[i][0]이 55 이상 data[i][1]가 7 초과이면 결과 값에 Senior를 아니라면 Open을 넣어주었다.

 

다른 사람의 좋았던 풀이

function openOrSenior(data){
  var result = [];
  data.forEach(function(member) {
    if(member[0] >= 55 && member[1] > 7) {
      result.push('Senior');
    } else {
      result.push('Open');
    }
  })
  return result;
}

 

function openOrSenior(data){
  return data.map(function(d){
    return d[0] >= 55 && d[1] > 7 ? 'Senior' : 'Open';
  });
}

 

댓글