Hướng dẫn moving median coderbyte solution python - con trăn giải pháp coderbyte di chuyển trung bình
Show Các giải pháp cho {{thử thách.title}}{{ challenge.title }}
{{Votelist [user.username]? Votelist [user.username] ['phiếu']: 0}} {{user.username}} đã nhận {{user.score}} điểm | Mã vận hành{{ user.score }} points | Run code {{idx + 1}} {{user.username}} {{chọn Language.replace ('CPP', 'C ++'). Điểm: {{user.score}}{{ user.score }} Xem giải pháp Không có giải pháp nào cho ngôn ngữ này. Buổi sáng tốt lành! Đây là vấn đề phỏng vấn mã hóa của bạn cho ngày hôm nay. Vấn đề này đã được Microsoft hỏi. Tính trung bình chạy của một chuỗi các số. Đó là, được đưa ra một luồng số, in ra trung bình của danh sách cho đến nay trên mỗi yếu tố mới. Hãy nhớ lại rằng trung bình của một danh sách số chẵn là trung bình của hai số trung bình. Ví dụ: với chuỗi
Quá trình suy nghĩ:
Logic tính toán:
Logic riêng biệt cho các mảng chiều dài chẵn và lẻMảng có độ dài
Khi chúng ta nhận được các chỉ mục (____10, 1) cho Evens, trung bình là 2.Mảng có độ dài lẻ
Khi chúng tôi nhận được các chỉ mục ( const ary = [2, 1, 5, 7, 2, 0, 5] function calculateMedian(ary) { const sorted = ary.sort((a, b) => a - b) if (sorted.length == 0) return 0 if (sorted.length == 1) return sorted[0] if (sorted.length % 2 == 0) { const firstIndex = sorted.length / 2 const secondIndex = firstIndex - 1 return ((sorted[firstIndex] + sorted[secondIndex]) / 2) } else { const middleIndex = Math.floor(sorted.length / 2) return (sorted[middleIndex]) } } function iterateArray(ary) { ary.forEach((el, index, ar) => { const currentStreamedArray = ar.slice(0, index + 1) console.log(calculateMedian(currentStreamedArray)) //2, 1.5, 2, 3.5, 2, 2, 2 }) } iterateArray(ary) //2, 1.5, 2, 3.5, 2, 2, 22) cho tỷ lệ cược, trung bình chỉ là const ary = [2, 1, 5, 7, 2, 0, 5] function calculateMedian(ary) { const sorted = ary.sort((a, b) => a - b) if (sorted.length == 0) return 0 if (sorted.length == 1) return sorted[0] if (sorted.length % 2 == 0) { const firstIndex = sorted.length / 2 const secondIndex = firstIndex - 1 return ((sorted[firstIndex] + sorted[secondIndex]) / 2) } else { const middleIndex = Math.floor(sorted.length / 2) return (sorted[middleIndex]) } } function iterateArray(ary) { ary.forEach((el, index, ar) => { const currentStreamedArray = ar.slice(0, index + 1) console.log(calculateMedian(currentStreamedArray)) //2, 1.5, 2, 3.5, 2, 2, 2 }) } iterateArray(ary) //2, 1.5, 2, 3.5, 2, 2, 23. Logic vòng lặpLặp lại thông qua mảng. Trong mỗi lần lặp, chúng tôi xuất ra một mảng mới từ đầu đến chỉ mục lặp hiện tại. Sau đó gửi mảng mới đó qua const ary = [2, 1, 5, 7, 2, 0, 5] function calculateMedian(ary) { const sorted = ary.sort((a, b) => a - b) if (sorted.length == 0) return 0 if (sorted.length == 1) return sorted[0] if (sorted.length % 2 == 0) { const firstIndex = sorted.length / 2 const secondIndex = firstIndex - 1 return ((sorted[firstIndex] + sorted[secondIndex]) / 2) } else { const middleIndex = Math.floor(sorted.length / 2) return (sorted[middleIndex]) } } function iterateArray(ary) { ary.forEach((el, index, ar) => { const currentStreamedArray = ar.slice(0, index + 1) console.log(calculateMedian(currentStreamedArray)) //2, 1.5, 2, 3.5, 2, 2, 2 }) } iterateArray(ary) //2, 1.5, 2, 3.5, 2, 2, 24. Mã sốconst ary = [2, 1, 5, 7, 2, 0, 5] function calculateMedian(ary) { const sorted = ary.sort((a, b) => a - b) if (sorted.length == 0) return 0 if (sorted.length == 1) return sorted[0] if (sorted.length % 2 == 0) { const firstIndex = sorted.length / 2 const secondIndex = firstIndex - 1 return ((sorted[firstIndex] + sorted[secondIndex]) / 2) } else { const middleIndex = Math.floor(sorted.length / 2) return (sorted[middleIndex]) } } function iterateArray(ary) { ary.forEach((el, index, ar) => { const currentStreamedArray = ar.slice(0, index + 1) console.log(calculateMedian(currentStreamedArray)) //2, 1.5, 2, 3.5, 2, 2, 2 }) } iterateArray(ary) //2, 1.5, 2, 3.5, 2, 2, 2 |