[21/11/01 KATA NINJA] 방금 그곡

36922 단어 codekatacodekata

문제

기존 코드

const getPlayTime = (start, finish) => {
  const [sHour, sMin] = start.split(":");
  const [fHour, fMin] = finish.split(":");
  const pHour = +fHour - +sHour;
  const pMin = +fMin - +sMin;

  return pHour * 60 + pMin;
};
const getMelody = (time, info) => {
  let result = "";
  let index = 0;
  for (let check = 0; check < time; check++) {
    if (index === info.length) {
      index = 0;
    }
    result += info[index];
    index++;
  }
  return result;
};
function solution(m, musicinfos) {
  var result = [];
  let musics = [];
  musicinfos.forEach((music) => {
    let [start, finish, title, info] = music.split(",");
    const playTime = getPlayTime(start, finish);
    info = info.replace(/C#/gi, "H");
    info = info.replace(/D#/gi, "I");
    info = info.replace(/F#/gi, "J");
    info = info.replace(/G#/gi, "K");
    info = info.replace(/A#/gi, "L");
    const melody = getMelody(playTime, info);
    musics.push({ start, finish, title, info, melody, playTime });
  });
  m = m.replace(/C#/gi, "H");
  m = m.replace(/D#/gi, "I");
  m = m.replace(/F#/gi, "J");
  m = m.replace(/G#/gi, "K");
  m = m.replace(/A#/gi, "L");
  musics.forEach((music) => {
    if (music.melody.includes(m)) {
      result.push(music);
    }
  });
  if (result.length > 1) {
    result.sort((a, b) => {
      if (a.playTime > b.playTime) {
        return -1;
      }
      if (a.playTime < b.playTime) {
        return 1;
      }
    });
  }
  return result.length > 0 ? result[0].title : "(None)";
}

풀이

  • 소트 역순인지 확인할 것. (내림차순인지 오름차순인지)

  • #음들은 특수하게 바꾸자

function solution(m, musicinfos) {
    const answer = [];
    
    const memorize = getMusicNote(m);
    
    musicinfos.forEach((musicInfo)=>{
        const [s,f,title,note] = musicInfo.split(',');
        const min = getMin(s,f);
        const musicNote = getMusicNote(note)
        const musicNoteByTime = getPlayNoteByTime(musicNote,min,musicNote.length);
        
        if(isWantMusic(musicNoteByTime,memorize)){
            answer.push([title,min]);   
        }
        
    })
    answer.sort(([_,aTime],[__,bTime]) => +bTime - +aTime);
    
    return answer.length === 0 ? '(None)' : answer[0][0];
    
    function getMin(s,f){
        const [sH,sM] = s.split(":");
        const [fH,fM] = f.split(":");
        return (+fH - +sH) * 60 + (+fM - +sM);
    }
    function getMusicNote(note){
        const array = note.split("");
        
        array.forEach((i,idx)=>{
            if(i === '#'){
                array[idx-1] = array[idx-1].toLowerCase();
                array[idx-1] += '#'
                array.splice(idx,1);
            }
        })
        return array;
    }
    function getPlayNoteByTime(note,time,length){
        const array = [];
        for(let i=0;i<time;i++){
            array.push(note[i%length]);
        }
        return array;
    }
    function isWantMusic(note,memo){
        const noteStr = note.join('')
        const memoStr = memo.join('')
        

        return noteStr.includes(memoStr);
    }
}

다른 풀이

정규표현식을 더 이용한 코드

function solution(m, musicinfos) {
    const answer = [];
    
    const memorize = getMusicNote(m);
    
    musicinfos.forEach((musicInfo)=>{
        const [s,f,title,note] = musicInfo.split(',');
        const min = getMin(s,f);
        const musicNote = getMusicNote(note)
        const musicNoteByTime = getPlayNoteByTime(musicNote,min,musicNote.length);
        
        
        if(isWantMusic(musicNoteByTime,memorize)){
            answer.push([title,min]);   
        }
        
    })
    answer.sort(([_,aTime],[__,bTime]) => +bTime - +aTime);
    
    return answer.length === 0 ? '(None)' : answer[0][0];
    
    function getMin(s,f){
        const [sH,sM] = s.split(":");
        const [fH,fM] = f.split(":");
        return (+fH - +sH) * 60 + (+fM - +sM);
    }
    function getMusicNote(note){
      // #이 들어가는 음을 #을 지우고, 문자를 소문자로 바꾼 문자로 대치
        return note.replace(/[CDFGA]#/gi,(match)=>match.toLowerCase()[0]);
    }
    function getPlayNoteByTime(note,time,length){
        let str = '';
        for(let i=0;i<time;i++){
            str += note[i % length];
        }
        return str;
    }
    function isWantMusic(note,memo){
        console.log(note,memo);
        return note.includes(memo);
    }
}

좋은 웹페이지 즐겨찾기