Quotes help make search much faster. Example: "Practice Makes Perfect"
Wednesday, February 6, 2013
Codecademy: A Closure To The Rescue
//sample solution
/* this rotates a string: "lohel".rotate(3) is "hello" */
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // if n too large: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
/*** Wrap everything below this line in a rot13_creator function! ***/
function rot13_creator(){
// compose the dictionary
var alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var keys = (alph + alph.toLowerCase()).split("");
var values = (alph.rotate(13) +
alph.rotate(13).toLowerCase()).split("");
var rot13_dict = {}; // dict: {A:'N', ..., z:'m'}
keys.forEach( function(key, index){
rot13_dict[key] = values[index]; } );
// wrap dictionary in a mapping function
var rot13_map = function(character) {
return rot13_dict[character] || character;
};
// create the function we're actually interested in
var rot13 = function(text) {
return text.split("").map(rot13_map).join("");
};
return rot13();
}
Monday, February 4, 2013
Sqlcourse Create Table Exercise
Sample solution
create table myemployees_nw0101(
firstname varchar(30),
lastname varchar(30),
title varchar(30),
age number(2),
salary number(10,2)
);
create table myemployees_nw0101(
firstname varchar(30),
lastname varchar(30),
title varchar(30),
age number(2),
salary number(10,2)
);
Math Problems: Finding a Product
Script solution for finding a product problem:
Answer: 30
console.log("----------------------------------");
function somfun(a,b,c){
var num = ((a*b)-1)*((b*c)-1)*((c*a)-1);
var den = a*b*c;
if(num % den === 0){
return true;
} else {
return false;
}
}
var max=100;
for(var i=2; i<=max; i++){
for(var j=2; j<=max; j++){
for(var k=2; k<=max; k++){
if(somfun(i,j,k)){
var temp = i*j*k;
console.log(i+" "+j+" "+k+" "+temp);
}
}
}
}
Math Problem: Square Root Sums
Question:
How many pairs of integers (x,y) will satisfy the following equation:
sqrt(x) + sqrt(y) = sqrt(432)
Answer: 13
Solution: Solved using the script below
console.log("----------------------------------");
function comp(a,b){
var temp = Math.sqrt(a)+Math.sqrt(b);
if(temp == Math.sqrt(432)){
return true;
} else {
return false;
}
}
var max=10000;
var count=0;
for(var i=0; i<max; i++){
for(var j=0; j<max; j++){
var temp=comp(i,j);
if(temp){
console.log(i+" "+j);
count++;
}
}
}
console.log("count is " + count);
How many pairs of integers (x,y) will satisfy the following equation:
sqrt(x) + sqrt(y) = sqrt(432)
Answer: 13
Solution: Solved using the script below
console.log("----------------------------------");
function comp(a,b){
var temp = Math.sqrt(a)+Math.sqrt(b);
if(temp == Math.sqrt(432)){
return true;
} else {
return false;
}
}
var max=10000;
var count=0;
for(var i=0; i<max; i++){
for(var j=0; j<max; j++){
var temp=comp(i,j);
if(temp){
console.log(i+" "+j);
count++;
}
}
}
console.log("count is " + count);
Sunday, February 3, 2013
Math Problem: Cutting a Plane with X Lines
Question: What is the maximum number of regions a plane can be cut into by an X number of lines
Answer: number of regions = 0.5 * ( x^2 + x + 2)
So, for example, if we have 9 lines, the maximum number of regions the plane would be divided into is 46.
Answer: number of regions = 0.5 * ( x^2 + x + 2)
So, for example, if we have 9 lines, the maximum number of regions the plane would be divided into is 46.
Sqlcourse Selecting Data Exercise 6
Problem:
Display all columns for everyone whose first name contains "Mary".
Solution:
select * from empinfo where first like '%Mary%';
Sqlcourse Selecting Data Exercise 5
Problem:
Display all columns for everyone whose first name equals "Mary".
Solution:
select * from empinfo where first = 'Mary';
Sqlcourse Selecting Data Exercise 4
Problem:
Display the first and last names for everyone whose last name ends in an "ay".
Solution:
select first, last from empinfo where last like '%ay';
Sqlcourse Selecting Data Exercise 3
Problem:
Display all columns for everyone that is over 40 years old.
Solution:
select * from empinfo where age > 40;
Sqlcourse Selecting Data Exercise 2
Problem:
Display the first name, last name, and city for everyone that's not from Payson.
Solution:
select first, last, city from empinfo where city <> 'Payson';
Sqlcourse Selecting Data Exercise 1
Problem:
Display the first name and age for everyone that's in the table.
Solution:
select first, age from empinfo;
Friday, February 1, 2013
Codecademy: Bind It In Chains
// Sample solution
// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // too large numbers: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");
var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");
array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
//rot13_dict;
function rot13_map(somechar){
if(alphabet.indexOf(somechar)>=0 || alphabet.toLowerCase().indexOf(somechar)>=0){
return rot13_dict[somechar];
} else {
return somechar;
}
}
//console.log(rot13_map('?'));
//console.log(rot13_map('A'));
function rot13(somestring){
if(somestring.length>1){
return somestring.split("").map(function(n){ return rot13_map(n);}).join("");
} else {
return rot13_map(somestring);
}
}
Codecademy: Wrap It!
// Sample solution
// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // too large numbers: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");
var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");
array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
//rot13_dict;
function rot13_map(somechar){
if(alphabet.indexOf(somechar)>=0 || alphabet.toLowerCase().indexOf(somechar)>=0){
return rot13_dict[somechar];
} else {
return somechar;
}
}
console.log(rot13_map('?'));
console.log(rot13_map('A'));
Codecademy: Write A Dictionary
// Sample solution
// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // too large numbers: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");
var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");
array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
rot13_dict;
Codecademy: ROT Stands For Rotation
// Sample solution
// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
n %= this.length; // too large numbers: modulo
var cut = n < 0 ? -n : this.length - n; // cutting point
return this.substr(cut) + this.substr(0,cut);
};
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// Now use it:
console.log(alphabet.rotate(13));
console.log(alphabet.rotate(13).rotate(13));
Codecademy: Getting Closer To String Translation
// Sample solution
// This is a dummy, to be refined later
function mapping(character) {
if (character >= 'A' && character <= 'z') {
return 'x';
}
else return character;
}
function substitute(string) {
/* your code here */
return string.split("").map(function(n){ return mapping(n); }).join("");
}
// Test your function:
// this should print "xxxx xx xxxxxxxxxx!"
console.log(substitute("Make me unreadable!"));
Codecademy: Inline Functions In Chains
// Sample solution
// Sum of squares of negatives, using a loop:
function sum_squares_negatives_loop(array) {
var sum = 0;
for (var i = 0; i < array.length; i++) {
if (array[i] < 0) { // filter: negatives only
sum += (array[i] * array[i]); // square & add
}
}
return sum;
}
// Same thing without a loop, using Array methods:
function my_sum_squares_neg(array) {
return array
.filter( // filter negative numbers:
function(n) {
return n<0 ? n : null; // <-- true if and only if n is negative
}
)
// at this point we have an array of negative numbers
.map( // map each of these numbers to its square
function(n) {
return n * n; // <-- the square of n
}
)
// now, we have an array of squared numbers
.reduce( // let's add them all together!
function(sum, next) { // each array element in turn is next
return sum+=next; // <-- add next (value) to sum
}, 0 // set sum = 0 initially
);
}
// test if your function does the same as the loop:
var numbers = [-23, 95, 0, -2, -47];
console.log(sum_squares_negatives_loop(numbers));
console.log(my_sum_squares_neg(numbers));
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
var my_canvas=document.getElementById('canvas'); var context=my_canvas.getContext('2d');
-
This is caused by the entrance of water into the air sacs which makes the lungs doughy, readily pits on pressure, exuding water and froth
-
Question: What current is required for full scale deflection of a galvanometer having a current sensitivity of 50 micro-amperes per scale di...
-
#sample solution print "What's your first name?" first_name = gets.chomp.capitalize! print "What's your last na...
-
//Sample Solution const gameState = {} function preload() { this.load.image('codey', 'https://s3.amazonaws.com/codecademy-...
-
#Sample Solution --- title: "Data Cleaning in R" output: html_notebook --- ```{r message=FALSE, warning=FALSE} # load libra...
-
Solution: SELECT first_name, age, FROM table_name;
-
# Update data types year_1_str = str(year_1) revenue_1_str = str(revenue_1) # Create a complete sentence combining only the string d...
-
#sample solution secret_identities = { "The Batman" => "Bruce Wayne", "Superman" => "Clark Ke...
-
Answer: This is the type of injury when a blow to the forehead causes a contusion of the eyeball due to the fracture of the bone at the r...