Help in converting Javascript code to Python

function diskStacking(disks){
  disks.sort((a,b) => a[2] - b[2])
  let maxHeight = disks.map((arr) => arr[2])
  let seq = disks.map((arr) => null)

  for(let i = 1; i < disks.length; i++){
    for(let j = 0; j < i; j++){
      let currDisk = disks[i]
      let prevDisk = disks[j]
      if(isValidDisks(prevDisk,currDisk)){
        if(maxHeight[j] + currDisk[2] > maxHeight[i]){
          seq[i] = j
          maxHeight[i] = maxHeight[j] + currDisk[2]
        }
      }
    }
  }

  let maxHeightIdx = maxHeight.indexOf(Math.max(...maxHeight))
  let maxStackDisks = getMaxStackDisks(maxHeightIdx, disks,seq)

  return maxStackDisks
}


function isValidDisks(prevDisk, currDisk){
  return prevDisk[0] <  currDisk[0] && prevDisk[1] <  currDisk[1] && prevDisk[2] <  currDisk[2]
}
function getMaxStackDisks(idx, disks,seq){
  let result = []
  while(idx !== null){
    result.unshift(disks[idx].concat())
    idx = seq[idx]
  }
  return result
}

main(disks)
  disks.sort((a, b) => a[2] - b[2])
  let maxHeight = disks.map((arr) => arr[2])
  let seq = disks.map((arr) => null)

  for(let i = 1; i < disks.length; i++){
    let currDisk = disks[i]
    for(let j = 0; j < i; j++){
      let prevDisk = disks[j]
      if(isVaildDisk(currDisk,prevDisk)){ // Helper Method
          if(maxHeight[j] + currDisk[2] > maxHeight[i]){
            seq[i] = j
            maxHeight[i] = maxHeight[j] + currDisk[2]
          }
      }
    }
  }

  let maxHeightIdx = maxHeight.indexOf(Math.max(...maxHeight))
  let maxStackDisks = getMaxStackDisks(maxHeight, disks) // Helper
  return maxStackDisks

Please help, I have no knowledge of Javascript and want to understand the logic used in this piece of code.

function diskStacking(disks){
  //sort an array of arrays based on the third element being lower
  disks.sort((a,b) => a[2] - b[2])
  //maxHeight == an array of the third elements in disks
  let maxHeight = disks.map((arr) => arr[2])
  //seq == an array with as many nulls as disks has elements
  let seq = disks.map((arr) => null)

  //starting at 1 loop this until this (i < disks.length) is false 
  for(let i = 1; i < disks.length; i++){
    //a double loop for no reason, and this logic is bad
    for(let j = 0; j < i; j++){
      //assigns currDisk to the ith eleement of disks
      let currDisk = disks[i]
      //assigns prevDisk to the jth eleement of disks
      let prevDisk = disks[j]

      //this hoists the functions isValidDisks which I genarlly do not recommend, but in any cas it returns a boolean
      if(isValidDisks(prevDisk,currDisk)){
        //I have not done any python, but I will assume you know how if statements work so I will not be explaining them unless they are bad or very js centric
        if(maxHeight[j] + currDisk[2] > maxHeight[i]){
          seq[i] = j
          maxHeight[i] = maxHeight[j] + currDisk[2]
        }
      }
    }
  }
  //maxHeightIdx == the position of th biggest number within the maxHeight array
  let maxHeightIdx = maxHeight.indexOf(Math.max(...maxHeight))
  //maxStackDisks == the return value of getMaxStackDisks
  let maxStackDisks = getMaxStackDisks(maxHeightIdx, disks,seq)
  
  //returns the array maxStackDisks when diskStacking is called
  return maxStackDisks
}

//returns a boolean
function isValidDisks(prevDisk, currDisk){
  return prevDisk[0] <  currDisk[0] && prevDisk[1] <  currDisk[1] && prevDisk[2] <  currDisk[2]
}

//creates what maxStackDisks is
function getMaxStackDisks(idx, disks,seq){
  let result = []
  while(idx !== null){
    result.unshift(disks[idx].concat())
    idx = seq[idx]
  }
  return result
}

//this calls the function main
main(disks)
  //sort an array
  disks.sort((a, b) => a[2] - b[2])
  //makes an array
  let maxHeight = disks.map((arr) => arr[2])
  //this code is repeating
  let seq = disks.map((arr) => null)

  for(let i = 1; i < disks.length; i++){
    let currDisk = disks[i]
    for(let j = 0; j < i; j++){
      let prevDisk = disks[j]
      if(isVaildDisk(currDisk,prevDisk)){ // Helper Method
          if(maxHeight[j] + currDisk[2] > maxHeight[i]){
            seq[i] = j
            maxHeight[i] = maxHeight[j] + currDisk[2]
          }
      }
    }
  }

  let maxHeightIdx = maxHeight.indexOf(Math.max(...maxHeight))
  let maxStackDisks = getMaxStackDisks(maxHeight, disks) // Helper
  //this is not in a functtion so it will not work
  return maxStackDisks

I commented the function out a little bit more, but the logic in it is flawed, and it would not work in its current state

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.