알고리즘 70 - Categorize New Member

jabae·2021년 11월 1일
0

알고리즘

목록 보기
70/97

Q.

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 lists containing two items each. Each list 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.

Note for F#: The input will be of (int list list) which is a List<List>

Example Input

[[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]
Output

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

Example Output

["Open", "Open", "Senior", "Open", "Open", "Senior"]

A)

function openOrSenior(data){
  let result = [];
  for (let el of data) {
    if (el[0] >= 55 && el[1] > 7)
      result.push("Senior");
    else
      result.push("Open");
  }
  return result;
}

other

.map()을 다양한 방식으로 쓴 것을 알 수 있다! 아래처럼 아예 배열을 주고 각 해당 값의 범위에 따라 반환하거나 (이렇게 쓴 건 처음봐서 너무 신기하다! 나중에 나도 써먹어봐야지 꽤 유용할 것 같다!😮)

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

아니면 엘리먼트에 인덱스로 접근하여 해당 값의 조건에 따른 값을 구할 수 있다! 🤭👏

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

0개의 댓글