mirror of
https://github.com/ossu/computer-science.git
synced 2025-08-05 21:04:18 -04:00
Find the cube root of a perfect cube - range
This commit is contained in:
parent
6dc4301c3e
commit
eb20d59e1d
6 changed files with 307 additions and 1 deletions
|
@ -0,0 +1,70 @@
|
||||||
|
// Modules
|
||||||
|
var range = require( 'range' ).range;
|
||||||
|
var prompt = require( 'prompt' );
|
||||||
|
|
||||||
|
// // Find the cube root of a perfect cube
|
||||||
|
// prompt.start();
|
||||||
|
// prompt.get([
|
||||||
|
// {
|
||||||
|
// name : 'x',
|
||||||
|
// description : 'Enter a interger'
|
||||||
|
// }
|
||||||
|
// ], function( err, results ) {
|
||||||
|
|
||||||
|
// var x = parseInt( results.x, 10 );
|
||||||
|
// var ans = 0;
|
||||||
|
|
||||||
|
// while ( Math.pow( ans, 3 ) < Math.abs( x )) {
|
||||||
|
|
||||||
|
// ans += 1;
|
||||||
|
// console.log( 'Current guess:', ans );
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if ( Math.pow( ans, 3 ) !== Math.abs( x )) {
|
||||||
|
|
||||||
|
// console.log( x, 'is not a perfect cube' );
|
||||||
|
|
||||||
|
// } else {
|
||||||
|
|
||||||
|
// if ( x < 0 ) {
|
||||||
|
// ans = -ans;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// console.log( 'Cube root of ' + x.toString() + ' is ' + ans.toString());
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// });
|
||||||
|
|
||||||
|
// Find the cube root of a perfect cube
|
||||||
|
prompt.start();
|
||||||
|
prompt.get([
|
||||||
|
{
|
||||||
|
name : 'x',
|
||||||
|
description : 'Enter a interger'
|
||||||
|
}
|
||||||
|
], function( err, results ) {
|
||||||
|
|
||||||
|
var x = parseInt( results.x, 10 );
|
||||||
|
var ans;
|
||||||
|
|
||||||
|
for ( ans in range( 0, Math.abs( x ) + 1 )) {
|
||||||
|
|
||||||
|
if ( Math.pow( ans, 3 ) === Math.abs( x )) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( Math.pow( ans, 3 ) !== Math.abs( x )) {
|
||||||
|
|
||||||
|
console.log( x + ' is not a perfect cube' );
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
console.log( 'Cube root of ' + x.toString() + ' is ' + ans.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
23
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/LICENSE.md
generated
vendored
Normal file
23
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/LICENSE.md
generated
vendored
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# FreeBSD License
|
||||||
|
|
||||||
|
# Copyright 2011 Andrew Pennebaker. All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without modification,
|
||||||
|
are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation and/or
|
||||||
|
other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
||||||
|
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||||||
|
SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||||
|
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||||
|
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
||||||
|
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||||
|
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
124
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/README.md
generated
vendored
Normal file
124
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/README.md
generated
vendored
Normal file
|
@ -0,0 +1,124 @@
|
||||||
|
# range - A simple library for range(a, b, step).
|
||||||
|
|
||||||
|
range.js is a Node library approximation of Python's `range()` function.
|
||||||
|
|
||||||
|
# EXAMPLE
|
||||||
|
|
||||||
|
```
|
||||||
|
$ node
|
||||||
|
> var range = require("range");
|
||||||
|
> range.range(0, 20);
|
||||||
|
[ 0,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
3,
|
||||||
|
4,
|
||||||
|
5,
|
||||||
|
6,
|
||||||
|
7,
|
||||||
|
8,
|
||||||
|
9,
|
||||||
|
10,
|
||||||
|
11,
|
||||||
|
12,
|
||||||
|
13,
|
||||||
|
14,
|
||||||
|
15,
|
||||||
|
16,
|
||||||
|
17,
|
||||||
|
18,
|
||||||
|
19 ]
|
||||||
|
> range.range(0, 20, 2);
|
||||||
|
[ 0,
|
||||||
|
2,
|
||||||
|
4,
|
||||||
|
6,
|
||||||
|
8,
|
||||||
|
10,
|
||||||
|
12,
|
||||||
|
14,
|
||||||
|
16,
|
||||||
|
18 ]
|
||||||
|
```
|
||||||
|
|
||||||
|
# HOMEPAGE
|
||||||
|
|
||||||
|
https://github.com/mcandre/node-range
|
||||||
|
|
||||||
|
# NPM
|
||||||
|
|
||||||
|
https://www.npmjs.com/package/range
|
||||||
|
|
||||||
|
# LICENSE
|
||||||
|
|
||||||
|
FreeBSD
|
||||||
|
|
||||||
|
# REQUIREMENTS
|
||||||
|
|
||||||
|
* [Node.js](http://nodejs.org/) 0.8+
|
||||||
|
|
||||||
|
## Optional
|
||||||
|
|
||||||
|
* [Ruby](https://www.ruby-lang.org/) 2+
|
||||||
|
* [Bundler](http://bundler.io/)
|
||||||
|
* [Guard](http://guardgem.org/)
|
||||||
|
* [aspelllint](https://github.com/mcandre/aspelllint)
|
||||||
|
|
||||||
|
# DEVELOPMENT
|
||||||
|
|
||||||
|
## Test
|
||||||
|
|
||||||
|
Ensure the example script works as expected:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ npm test
|
||||||
|
|
||||||
|
> range@0.0.2 test /Users/apennebaker/Desktop/src/node-range
|
||||||
|
> mocha
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
range
|
||||||
|
range
|
||||||
|
✓ should behave like Python range()
|
||||||
|
|
||||||
|
|
||||||
|
1 passing (5ms)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Lint
|
||||||
|
|
||||||
|
Keep the code tidy:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ grunt lint
|
||||||
|
```
|
||||||
|
|
||||||
|
## Spell Check
|
||||||
|
|
||||||
|
```
|
||||||
|
$ aspelllint
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Local CI
|
||||||
|
|
||||||
|
Guard can automatically run testing when the code changes:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ bundle
|
||||||
|
$ guard -G Guardfile-cucumber
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
Guard can automatically lint when the code changes:
|
||||||
|
|
||||||
|
```
|
||||||
|
$ bundle
|
||||||
|
$ guard -G Guardfile-lint
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
## Git Hooks
|
||||||
|
|
||||||
|
See `hooks/`.
|
28
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/lib/range.js
generated
vendored
Normal file
28
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/lib/range.js
generated
vendored
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
// Returns an array of integers starting at a, incrementing by step, ending before b.
|
||||||
|
//
|
||||||
|
// Example:
|
||||||
|
//
|
||||||
|
// > var range = require("range").range;
|
||||||
|
// > range(0, 10);
|
||||||
|
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
|
|
||||||
|
function range(a, b, step) {
|
||||||
|
if (arguments.length === 1) {
|
||||||
|
b = a;
|
||||||
|
a = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
step = step || 1;
|
||||||
|
|
||||||
|
var x, r = [];
|
||||||
|
|
||||||
|
for (x = a; (b - x) * step > 0; x += step) {
|
||||||
|
r.push(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.range = range;
|
60
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/package.json
generated
vendored
Normal file
60
computer-science/01-introduction-to-cs-and-programming-mit/src/node_modules/range/package.json
generated
vendored
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
{
|
||||||
|
"name": "range",
|
||||||
|
"description": "A simple library for range(a, b, step).",
|
||||||
|
"version": "0.0.3",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git://github.com/mcandre/node-range.git"
|
||||||
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "Andrew Pennebaker",
|
||||||
|
"email": "andrew.pennebaker@gmail.com",
|
||||||
|
"url": "http://www.yellosoft.us/"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"exec": "exec",
|
||||||
|
"test": "mocha"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.8"
|
||||||
|
},
|
||||||
|
"main": "lib/range",
|
||||||
|
"devDependencies": {
|
||||||
|
"cmp": "0.0.2",
|
||||||
|
"editorconfig-tools": ">= 0.0.1",
|
||||||
|
"eslint": ">= 0.15.0",
|
||||||
|
"grunt": "~0.4.2",
|
||||||
|
"grunt-cli": "~0.1.13",
|
||||||
|
"grunt-contrib-uglify": "^0.9.1",
|
||||||
|
"grunt-exec": "~0.4.5",
|
||||||
|
"jshint": ">=2.1.2",
|
||||||
|
"jslint": ">= 0.8.1",
|
||||||
|
"mocha": "^1.21.4"
|
||||||
|
},
|
||||||
|
"gitHead": "83bc5f63521e0bad88a5a2d0d3e668b203766745",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/mcandre/node-range/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/mcandre/node-range",
|
||||||
|
"_id": "range@0.0.3",
|
||||||
|
"_shasum": "b5b8eb2463a516b624a563bd32b18fe89e70151b",
|
||||||
|
"_from": "range@",
|
||||||
|
"_npmVersion": "2.6.0",
|
||||||
|
"_nodeVersion": "0.10.36",
|
||||||
|
"_npmUser": {
|
||||||
|
"name": "mcandre",
|
||||||
|
"email": "andrew.pennebaker@gmail.com"
|
||||||
|
},
|
||||||
|
"maintainers": [
|
||||||
|
{
|
||||||
|
"name": "mcandre",
|
||||||
|
"email": "andrew.pennebaker@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dist": {
|
||||||
|
"shasum": "b5b8eb2463a516b624a563bd32b18fe89e70151b",
|
||||||
|
"tarball": "http://registry.npmjs.org/range/-/range-0.0.3.tgz"
|
||||||
|
},
|
||||||
|
"directories": {},
|
||||||
|
"_resolved": "https://registry.npmjs.org/range/-/range-0.0.3.tgz"
|
||||||
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
"author": "Eric Douglas <eric.douglas.mail@gmail.com> (https://github.com/ericdouglas)",
|
"author": "Eric Douglas <eric.douglas.mail@gmail.com> (https://github.com/ericdouglas)",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"prompt": "^0.2.14"
|
"prompt": "^0.2.14",
|
||||||
|
"range": "0.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue