Hướng dẫn moving median coderbyte solution python - con trăn giải pháp coderbyte di chuyển trung bình

Các giải pháp cho {{thử thách.title}}{{ challenge.title }}

// Your account does not have access to view these user solutions.

{{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 [2, 1, 5, 7, 2, 0, 5], thuật toán của bạn nên in ra:

2
1.5
2
3.5
2
2
2

Quá trình suy nghĩ:

  1. cần một chức năng để tính toán trung bình cho bất kỳ mảng nào
  2. Cần một hàm để lặp thông qua chức năng đó để có được đầu ra của mảng phát trực tuyến

Logic tính toán:

  • trường hợp cạnh
    • Khi mảng trống, hãy trả về 0
    • Khi mảng chỉ có một phần tử, hãy trả về phần tử đó
  • Chúng ta cần sắp xếp mảng trước khi có thể tính toán trung bình
  • Tạo các mảng ví dụ để xác định những yếu tố cần chụp [điều này giúp xem những mẫu nào xuất hiện]. Tôi nhận ra rằng sẽ có một logic khác nhau giữa các mảng có độ dài và các mảng có độ dài lẻ nên chúng ta cần tách logic đó.

Logic riêng biệt cho các mảng chiều dài chẵn và lẻ

Mảng có độ dài

  • [1,2,3,4] => length = 4, chúng ta cần chỉ mục [1,2].
    • Để có được các chỉ mục này, chúng tôi lấy length / 2 là 2. Chỉ số thứ hai là 2 - 1, là 1.
  • [1,2,3,4,5,6] => length = 6, chúng ta cần chỉ mục [2,3].
    • Để có được các chỉ mục này, chúng tôi lấy length / 2 là 3. Chỉ số thứ hai là 3 - 1, là 2.
  • [1,2,3,4,5,6,7,8] => length = 8, chúng ta cần chỉ mục [3,4].
    • Để có được các chỉ mục này, chúng tôi lấy length / 2 là 4. Chỉ số thứ hai là 4 - 1, là 3.

Khi chúng ta nhận được các chỉ mục [____10,

2
1.5
2
3.5
2
2
2
1] cho Evens, trung bình là
2
1.5
2
3.5
2
2
2
2.

Mảng có độ dài lẻ

  • 2
    1.5
    2
    3.5
    2
    2
    2
    
    3 => length = 3, chúng ta cần chỉ mục [1].
    • Để có được chỉ số này, chúng tôi lấy length / 2, là 1,5. Chúng tôi
      2
      1.5
      2
      3.5
      2
      2
      2
      
      5 để nhận chỉ số 1.
  • 2
    1.5
    2
    3.5
    2
    2
    2
    
    6 => length = 5, chúng ta cần chỉ mục [2].
    • Để có được chỉ số này, chúng tôi lấy length / 2 là 2,5. Chúng tôi
      2
      1.5
      2
      3.5
      2
      2
      2
      
      8 để có chỉ số 2.
  • 2
    1.5
    2
    3.5
    2
    2
    2
    
    9 => length = 7, chúng ta cần chỉ mục [3].
    • Để có được chỉ số này, chúng tôi lấy length / 2, là 3,5. Chúng tôi
      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
      1 để có chỉ số 3.

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, 2
2] 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, 2
3.

Logic vòng lặp

Lặ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, 2
4.

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

Bài Viết Liên Quan

Chủ Đề