[알고리즘] 2024-02-28(수)

dev-riverkim·2024년 3월 13일
0

7 kyu - Exes and Ohs

https://deeplify.dev/front-end/js/count-characters-in-string

function XO(str) {
    // 문자열을 소문자로 변환
    str = str.toLowerCase();
    
    // 'x'와 'o'의 개수를 저장할 변수 초기화
    let countX = 0;
    let countO = 0;
    
    // 문자열을 반복하며 'x'와 'o'의 개수 세기
    for (let char of str) {
        if (char === 'x') countX++;
        if (char === 'o') countO++;
    }
    
    // 'x'와 'o'의 개수가 같으면 true, 다르면 false 반환
    return countX === countO;
}
function XO(str) {
    // 문자열을 소문자로 변환
    str = str.toLowerCase();
    
    // 'x'와 'o'로 문자열을 분리하여 배열 생성
    const xCount = str.split('x').length - 1;
    const oCount = str.split('o').length - 1;
    
    // 'x'와 'o'의 개수가 같은지 비교
    return xCount === oCount;
}
function XO(str) {
    let xCount = 0;
    let oCount = 0;
    str = str.toLowerCase();
    
    for (let char of str) {
        if (char === 'x') xCount++;
        else if (char === 'o') oCount++;
    }
    
    return xCount === oCount;
}
function XO(str) {
    const xMatches = str.match(/x/gi);
    const oMatches = str.match(/o/gi);
    const xCount = xMatches ? xMatches.length : 0;
    const oCount = oMatches ? oMatches.length : 0;
    
    return xCount === oCount;
}

8 kyu - Remove First and Last Character

It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry about strings with less than two characters.

Solution

function removeChar(str){
 //You got this!
 // 매우 간단합니다. 문자열의 첫 번째 문자와 마지막 문자를 제거하는 함수를 만드는 것이 목표입니다. 
 // 하나의 매개변수, 즉 원본 문자열이 주어집니다. 두 글자 미만의 문자열은 걱정할 필요가 없습니다.

  return str.substring(1, str.length - 1)
  
};
function removeChar(str) {
  return str.slice(1, -1);
}
const removeChar = str => str.slice(1,-1)

8 kyu - Is it even?

In this Kata we are passing a number (n) into a function.

Your code will determine if the number passed is even (or not).

The function needs to return either a true or false.

Numbers may be positive or negative, integers or floats.

Floats with decimal part non equal to zero are considered UNeven for this kata.

Solution

function testEven(n) {
  return n%2 === 0 ? true : false
}
function testEven(n) {
    return n%2===0;
}

7 kyu - Categorize New Member

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

Solution

function openOrSenior(data){
  const result = data.map((item)=>{
    return  item[0] >= 55 && item[1] > 7 ? "Senior" : "Open"
  })
  return result;
}
// Destructuring: [age, handicap] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
// Arrow Functions: () => {} https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

function openOrSenior(data){
  return data.map(([age, handicap]) => (age > 54 && handicap > 7) ? 'Senior' : 'Open');
}
function openOrSenior(data){
  function determineMembership(member){
    return (member[0] >= 55 && member[1] > 7) ? 'Senior' : 'Open';
  }
  return data.map(determineMembership);
}
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(([age, handicap]) => age >= 55 && handicap > 7 ? 'Senior' : 'Open');
}
function openOrSenior(data){
  return data.map(function(d){
    return d[0] >= 55 && d[1] > 7 ? 'Senior' : 'Open';
  });
}
profile
dev-riverkim

0개의 댓글