Toggle navigation
Golibrary Code Playground - Everyone for education
Run
(current)
Fork
Settings
Share
0
339
Anonymous
HTML/CSS/Javascript
Python
Ruby
Clojure
PHP
Javascript (NodeJS)
Scala
Go
C/C++
Java
VB.Net
C#
Bash
Objective C
MySQL
Perl
Rust
HTML/CSS/Javascript
Search
Login
Login
Reset Password ?!
Login
Register
×
RESET PASSWORD
@
×
Allowed Extensions
Only this extensions are allowed: jpg,png,gif,jpeg
×
Error Message
×
Share this code
Url of this code
Send SMS with url of this code
Save As Github Gist
×
Github Gist
×
SETTINGS
HTML
CSS
Javascript
Title
Description
Meta Tags
Add meta tag example
Known ressources
--
Foundation
Bootstrap
CSS Preprocessor
CSS
LESS
SCSS
External CSS
Add another ressource
Known ressources
--
JQuery UI
Modernizr
Tree.js
JQuery
External JS
Add another ressource
CSS
→
/* CSS */ background: #f9f9f9; height: 100%;
Full Screen
Javascript
→
// Javascript init(); function init() { var output = []; var weights = [1, 2, 4, 8, 16, 32, 64]; var maxWeightToCount = 121; for (var i = 1; i <= maxWeightToCount; i++) { searchWeights(weights, i, output); document.write('Sequence containing minimum number of weights with sum equal = ', i, ' pounds from the given list of available weights ', weights, ' = ', output+'<br/><br/>'); output = []; } } function searchWeights(arr, value, result) { var val = binSearch(arr, value); if (value - val === 0) { result.push(val); return; } if (val === -1) { val = getNearestValue(arr, value); result.push(val); } searchWeights(arr, value - val, result); } function binSearch(arr, W, bounds=[]) { var l = 0, r = arr.length - 1; while (l <= r) { var m = Math.floor(l + (r - l) / 2); // Check if W is present at mid if (arr[m] == W) return arr[m]; // If W greater, ignore left half if (arr[m] < W) l = m + 1; // If W is smaller, ignore right half else r = m - 1; } bounds[0] = l, bounds[1] = r; return -1; } function getNearestValue(a, value) { var bounds = []; if(value < a[0]) { return a[0]; } if(value > a[a.length-1]) { return a[a.length-1]; } var retVal = binSearch(a, value, bounds); var lo = bounds[0], hi = bounds[1]; if (retVal === -1) { retVal = (a[lo] - value) < (value - a[hi] && a[lo] < value) ? a[lo] : a[hi]; } return retVal; }
HTML
→
<!-- HTML --> <b>Start new code !</b>