[21/11/01 KATA NINJA]さっきの歌
質問する
既存のコード
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);
}
}
Reference
この問題について([21/11/01 KATA NINJA]さっきの歌), 我々は、より多くの情報をここで見つけました https://velog.io/@rat8397/211101-KATA-NINJA-방금-그곡テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol