Skip to content

Instantly share code, notes, and snippets.

@dogterbox
Last active October 17, 2018 16:12
Show Gist options
  • Save dogterbox/16a3134cf9cb81cb07182ea52ac570f4 to your computer and use it in GitHub Desktop.
Save dogterbox/16a3134cf9cb81cb07182ea52ac570f4 to your computer and use it in GitHub Desktop.
Largest Penalty per Unit Length (LPUL)
/*
data input format
list = [[t1,p1],
[t2,p2],
[t3,p3],
[.....]]
*/
list = [[10,1],
[10,7],
[7,5],
[20,20],
[30,4],
[30,4],
[20,5],
[100,25],
[10,10]]
// sorting follow definition
function sort(list){
listSort = [list[0]] // initial append to listSort
for(var i = 1; i < list.length; i++) {
const iValue = list[i]
const iP = iValue[1]
const iU = iValue[2]
for(j in listSort) {
const jValue = listSort[j]
const jP = jValue[1]
const jU = jValue[2]
if(iU < jU) {
listSort.splice(j, 0, iValue)
break
} else if(iU == jU && iP < jP) {
listSort.splice(j, 0, iValue)
break
} else if (j == listSort.length-1) {
listSort.push(iValue) // listSort.splice(j+1, 0, iValue)
break
}
}
}
return listSort
}
// unit funtion (u) follow definition
function unitFunction(value) {
const t = value[0]
const p = value[1]
const u = t / p
return u
}
// calulate unit (u)
function unitCalulate(list) {
for(var i in list) {
var value = list[i]
const u = unitFunction(value)
value.push(u);
}
return list
}
function main() {
list = unitCalulate(list) // calulate u value
console.log('Calulate unit value:\n', list)
list = sort(list) // sorting value follow definition
console.log('Sorting unit value:\n', list)
}
main()
@dogterbox
Copy link
Author

dogterbox commented Oct 17, 2018

problem
output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment