[알고리즘] 프로그래머스_코딩테스트 입문_2023.2.28

신재욱·2023년 2월 28일
0
post-thumbnail

0️⃣ 모음 제거


  • 잘못된 풀이
    function solution(my_string) {
     let ar = ["a","e","i","o","u"]
     let br = my_string.split('')
     let cr = br.filter(number => ar.includes(number));
     let dr = br.replace.(cr,'')
     let er = dr.join('')
    
     return er;
    }
1. 모음을 비교하기 위해서는 배열로 분할해야만 비교할 수 있다고 생각함
2. 배열과 배열로 나눠서 비교하기가 안됨
3. includes를 사용해 포함된 것들을 정의하고 replace ''비어있는 공백으로 대체하려 했으나 배열 따로 따로가 안됨
4. 결국 실패...
  • 풀이
function solution(my_string) {
    let ar = 'aeiou'
    let br = my_string.split('')
    let cr = br.filter((number) => (!ar.includes(number)));
    let er = cr.join('')

    return er;
}

1. 비교할 문자를 굳이 배열로 나눠줄 필요가 없음을 앎
2. !ar.includes(number) [!]<----을 사용해 속하지 않은것을 표현 가능
  • 다른 풀이
function solution(my_string) {
    return my_string.replace(/[aeiou]/g, '');
}

📌 /[aeiou]/g

특정문자를 위와같은 정규식으로 가능

0️⃣ 개미 군단


  • 풀이
function solution(hp) {
    var answer = 0;
    if(hp%5===0){
        answer = hp/5
    }else if(hp%5===3){
        answer = (hp/5)+1
    }else if(hp%5===4){
        answer = (hp/5)+2
    }else if(hp%5===2){
        answer = (hp/5)+2
    }else if(hp%5===1){
        answer = (hp/5)+1
    }
   
    return Math.floor(answer);
}

📌 Math.floor
  • 다른 풀이
function solution(hp) {
    return Math.floor(hp/5)+Math.floor((hp%5)/3)+(hp%5)%3;
}

📌 나머지를 활용

0️⃣ 대문자와 소문자 ✨


  • 풀이
function solution(my_string) {
    let ar = my_string.split('')
    let br = [];
    for(let i =0; i<ar.length; i++){
        if(ar[i]===ar[i].toUpperCase()){
            br.push(ar[i].toLowerCase())
        }else{
            br.push(ar[i].toUpperCase())
        }
    }
    return br.join('')
   
    return answer;
} 

📌 toUpperCase()

대문자로 변경

📌 toLowerCase()

소문자로 변경

profile
1년차 프론트엔드 개발자

0개의 댓글

관련 채용 정보