Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lv.2] [3차] 파일명 정렬 #165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

[Lv.2] [3차] 파일명 정렬 #165

wants to merge 1 commit into from

Conversation

say-young516
Copy link
Contributor

🔗 Related Issues

🔍 문제

프로그래머스 - [3차] 파일명 정렬

✅ 풀이 방법

추후 첨부

📜 최종 소스코드

function solution(files) {
  const answer = [];

  function checkNum(startIdx, str) {
    for (let i = startIdx; i < str.length; i++) {
      if (startIdx === 0 && !isNaN(str[i]) && str[i] !== " ") return i;
      else if (startIdx !== 0 && isNaN(str[i])) return i;
    }
  }
  answer.push(files[0]);
  for (let i = 1; i < files.length; i++) {
    let cur = files[i].toLowerCase();
    let flag = false;

    for (let k = 0; k < answer.length; k++) {
      if (
        cur.slice(0, checkNum(0, cur)) <
        answer[k].slice(0, checkNum(0, answer[k])).toLowerCase()
      ) {
        answer.splice(k, 0, files[i]);
        flag = true;
        break;
      } else if (
        cur.slice(0, checkNum(0, cur)) >
        answer[k].slice(0, checkNum(0, answer[k])).toLowerCase()
      )
        continue;

      if (
        Number(
          cur
            .slice(checkNum(0, cur), checkNum(checkNum(0, cur), cur))
            .slice(0, 5)
        ) <
        Number(
          answer[k]
            .slice(
              checkNum(0, answer[k]),
              checkNum(checkNum(0, answer[k]), answer[k])
            )
            .slice(0, 5)
        )
      ) {
        answer.splice(k, 0, files[i]);
        flag = true;
        break;
      }
    }
    if (!flag) answer.push(files[i]);
  }

  return answer;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging this pull request may close these issues.

[Lv.2] [3차] 파일명 정렬
1 participant