(1)音符の長さをduration(四分音符)の倍率で求める。
(2)文字列の種類は基数と修飾の大区分。
(3)基数はタン、タ、ターの3種類で独立。
修飾は.、..、数字の3種類で
.または..の後ろに数字が付く場合もあるし、修飾がない場合もある。
(4)基数タンは四分音符で1を返す。
基数タは八分音符で0.5を返す。
基数ターは二分音符で2を返す。
(5).はタン、タ、ターの後ろに付いて基数の1.5倍、
..はタン、タ、ターの後ろに付いて基数の1.25倍、
(6)数字の場合は基数、または基数と.または..で求めたものをその数値で割る。
(7)入力文字列を分割して、それぞれの音符の長さをリストで返す。
【Copilotの答えの始め】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
function getDurations(notes) { // 基数の正規表現 const basePattern = /^(タン|タ|ター)/; // 修飾の正規表現 const modifierPattern = /(\.\.|\.|\d+)?$/; // 基数の対応するduration const baseDurations = { 'タン': 1, 'タ': 0.5, 'ター': 2 }; // 入力文字列を音符ごとに分割 const noteArray = notes.split(/(?=タン|タ|ター)/); let durations = []; // 各音符のdurationを計算 noteArray.forEach(note => { const baseMatch = note.match(basePattern); if (!baseMatch) { throw new Error('Invalid base note.'); } const base = baseMatch[0]; let duration = baseDurations[base]; const modifierMatch = note.match(modifierPattern); if (modifierMatch && modifierMatch[0]) { const modifier = modifierMatch[0]; if (modifier.includes('.')) { if (modifier === '.') { duration *= 1.5; } else if (modifier === '..') { duration *= 1.25; } const digitMatch = modifier.match(/\d+$/); if (digitMatch) { const digit = parseInt(digitMatch[0]); if (digit === 0) { throw new Error('Division by zero error.'); } duration /= digit; } } else if (!isNaN(modifier)) { const digit = parseInt(modifier); if (digit === 0) { throw new Error('Division by zero error.'); } duration /= digit; } } durations.push(duration); }); return durations; } // テストケース console.log(getDurations('タンタン')); // [1, 1] console.log(getDurations('タタ.タン..ター4')); // [0.5, 0.75, 1.25, 0.5] console.log(getDurations('タ..10')); // [0.0625] console.log(getDurations('タ.24')); // [0.0208333...] |
【Copilotの答えの終わり】
真つ青なモンローウォークサングラス
