# Sample solution
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
if not my_file.closed:
my_file.close()
print my_file.closed
Quotes help make search much faster. Example: "Practice Makes Perfect"
Monday, October 28, 2013
#Python #Codecademy File Input/Output Try It Yourself
# Sample solution
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
with open("text.txt", "w") as my_file:
my_file.write("Kumbaya Sharkey!")
#Python #Codecademy File Input/Output PSA: Buffering Data
# Sample solution
# Open the file for reading
read_file = open("text.txt", "r")
# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")
write_file.close()
# Try to read from the file
print read_file.read()
read_file.close()
# Open the file for reading
read_file = open("text.txt", "r")
# Use a second file handler to open the file for writing
write_file = open("text.txt", "w")
# Write to the file
write_file.write("Not closing files is VERY BAD.")
write_file.close()
# Try to read from the file
print read_file.read()
read_file.close()
#Python #Codecademy File Input/Output Reading Between the Lines
# note: The instructions state to use "text.txt" but an error comes up stating that the file does not exist
# as a work around, I used "output.txt" instead
my_file = open("output.txt","r")
print my_file.readline()
print my_file.readline()
print my_file.readline()
my_file.close()
#Python #Codecademy File Input/Output Reading
# Sample solution
my_file = open("output.txt","r")
print my_file.read()
my_file.close()
my_file = open("output.txt","r")
print my_file.read()
my_file.close()
#Python #Codecademy File Input/Output Writing
# Sample solution
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
# Add your code below!
for i in my_list:
my_file.write(str(i)+"\n")
my_file.close()
my_list = [i**2 for i in range(1,11)]
my_file = open("output.txt", "r+")
# Add your code below!
for i in my_list:
my_file.write(str(i)+"\n")
my_file.close()
#Python #Codecademy File Input/Output The open() Function
# Sample Solution
my_file = open("output.txt","r+")
my_file = open("output.txt","r+")
Saturday, October 26, 2013
How to implement an image Carousel using Twitter Bootstrap?
<!-- Sample Code for implementing Carousel -->
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
<li data-target="#myCarousel" data-slide-to="4"></li>
</ol>
<!-- Carousel items -->
<!-- images are stored in the img folder which is at the same location or directory as the HTML file-->
<div class="carousel-inner">
<div class="active item"><img src="img/img1.jpg" width="100%" ></div>
<div class="item"><img src="img/img2.jpg" width="100%" ></div>
<div class="item"><img src="img/img3.jpg" width="100%" ></div>
<div class="item"><img src="img/img4.jpg" width="100%" ></div>
<div class="item"><img src="img/img5.jpg" width="100%" ></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
<!-- Make sure you include the necessary Bootstrap files -->
<!--The link to the CSS file in between the <head> and </head> tags -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<!--It is recommeneded that you place the following files near the closing </body> tag-->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
<li data-target="#myCarousel" data-slide-to="4"></li>
</ol>
<!-- Carousel items -->
<!-- images are stored in the img folder which is at the same location or directory as the HTML file-->
<div class="carousel-inner">
<div class="active item"><img src="img/img1.jpg" width="100%" ></div>
<div class="item"><img src="img/img2.jpg" width="100%" ></div>
<div class="item"><img src="img/img3.jpg" width="100%" ></div>
<div class="item"><img src="img/img4.jpg" width="100%" ></div>
<div class="item"><img src="img/img5.jpg" width="100%" ></div>
</div>
<!-- Carousel nav -->
<a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a>
<a class="carousel-control right" href="#myCarousel" data-slide="next">›</a>
</div>
<!-- Make sure you include the necessary Bootstrap files -->
<!--The link to the CSS file in between the <head> and </head> tags -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<!--It is recommeneded that you place the following files near the closing </body> tag-->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
Thursday, October 24, 2013
#Codeschool CSS Level 1 Sample Solution Columns
//sample solution to style.css
article {
float: left;
width: 120px;
}
aside {
float: right;
width: 120px;
}
article {
float: left;
width: 120px;
}
aside {
float: right;
width: 120px;
}
#Codeschool CSS Level 1 Sample Solution Floats
//sample solution to style.css
aside {
width: 120px;
float: right;
margin: 0px 0px 10px 10px;
}
aside {
width: 120px;
float: right;
margin: 0px 0px 10px 10px;
}
#Codeschool CSS Level 1 Sample Solution Style Specificity
<!-- Sample solution to index.html-->
<!doctype html>
<html lang="en">
<head>
<title>Sven's Snowshoe Emporium</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section class="content home">
<header>
<h1>Sven's Snowshoe Emporium</h1>
<h2>"For the Snow-Savvy Shoe-Shopper"</h2>
</header>
</section>
</body>
</html>
//Sample solution to style.css
header {
background: #e0e2e6;
}
<!doctype html>
<html lang="en">
<head>
<title>Sven's Snowshoe Emporium</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section class="content home">
<header>
<h1>Sven's Snowshoe Emporium</h1>
<h2>"For the Snow-Savvy Shoe-Shopper"</h2>
</header>
</section>
</body>
</html>
//Sample solution to style.css
header {
background: #e0e2e6;
}
#Codeschool CSS Level 1 Sample Solution Compound Selector
//Sample solution to style.css
.content {
border: 2px solid #ccc;
}
.content.home {
border: 0px;
}
.content {
border: 2px solid #ccc;
}
.content.home {
border: 0px;
}
#Codeschool CSS Level 1 Sample Solution ID Selector
//Solution to style.css
#slogan {
text-align: center;
font-style: italic
}
#slogan {
text-align: center;
font-style: italic
}
#Codeschool CSS Level 1 Sample Solution External Stylesheets
<!--Solution for index.html-->
<!doctype html>
<html lang="en">
<head>
<title>Sven's Snowshoe Emporium</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<section class="content">
<header>
<h1>Sven's Snowshoe Emporium</h1>
</header>
</section>
</body>
</html>
//Solution for style.css
body {
color: #4b4648;
font-family: tahoma, arial, sans-serif;
font-size: 14px;
}
.content {
border: 1px solid #cac3c6;
margin: 0 auto;
padding: 20px;
width: 260px;
}
h1 {
color: #6d9fac;
font-size: 22px;
text-align: center;
}
Tuesday, October 22, 2013
#Codeschool Backbone.js Sample Solution to Render the View
appointmentView.render();
$('#app').html(appointmentView.el);
#Codeschool Backbone.js Sample Solution to Set The Title
appointment.set({'title':'My Backbone Hurts'});
#Codeschool Backbone.js Sample Solution to Define Render
var AppointmentView = Backbone.View.extend({
render: function() {
var html = '<li>' + this.model.get('title') + '</li>';
$(this.el).html(html);
}
});
#Codeschool Backbone.js Sample Solution to View Instance
var appointmentView = new AppointmentView({
model: appointment
});
#Codeschool Backbone.js Sample Solution to View Class
var AppointmentView = Backbone.View.extend({});
#Codeschool Backbone.js Sample Solution to Model Instance
var appointment = new Appointment({'title':'Lunch Meeting'});
#Codeschool Backbone.js Sample Solution to Model Class
var Appointment = Backbone.Model.extend({});
In R, how do you test if the correlation between two vectors is statistically significant?
Answer:
cor.test(VectorA, VectorB)
// this will display a report showing the p-value
// a p-value less than 0.05 is considered statistically significant
cor.test(VectorA, VectorB)
// this will display a report showing the p-value
// a p-value less than 0.05 is considered statistically significant
In R, how do you read the contents of a .txt file into a data frame?
Answer:
someDataframe <- read.table("someFile.txt", sep="\t", header=TRUE)
//in this example, the columns in the .txt file are separated by tabs (tab delimited)
//header = TRUE indicates that the first row in the .txt file represents the headers of the data frame
//change this to FALSE if the first row already represents raw data
someDataframe <- read.table("someFile.txt", sep="\t", header=TRUE)
//in this example, the columns in the .txt file are separated by tabs (tab delimited)
//header = TRUE indicates that the first row in the .txt file represents the headers of the data frame
//change this to FALSE if the first row already represents raw data
In R, how do you store the contents of a .csv file into a data frame?
Answer:
someDataframe <- read.csv("someFile.csv")
someDataframe <- read.csv("someFile.csv")
In R, how do you determine the unique elements in a vector?
Answer:
someFactor <- factor(someVector)
//convert the vector to a factor using the factor function
levels(someFactor)
//use the levels function to identify the unique elements in the vector
someFactor <- factor(someVector)
//convert the vector to a factor using the factor function
levels(someFactor)
//use the levels function to identify the unique elements in the vector
In R, how do you compute for the standard deviation of a vector composed of numbers, numVector?
Answer:
sd(numVector)
sd(numVector)
In R, what are the ways you can plot a matrix someMatrix?
Possible answers:
contour(someMatrix)
persp(someMatrix, expand=0.2)
//this creates a 3D-plot where the z-axis is dependent on the values of the elements of the matrix
image(someMatrix)
//this creates a heat map of the matrix, kind of a colored version of contour
contour(someMatrix)
persp(someMatrix, expand=0.2)
//this creates a 3D-plot where the z-axis is dependent on the values of the elements of the matrix
image(someMatrix)
//this creates a heat map of the matrix, kind of a colored version of contour
In R, how do you access an element in a matrix someMatrix?
Answer:
someMatrix[rowIndex, columnIndex]
someMatrix[rowIndex, columnIndex]
In R, how can you re-shape a vector?
Answer:
dim(someVector) <- c(numRows, numCols)
//note, the length of someVector must be equal to the product of numRows and numCols
dim(someVector) <- c(numRows, numCols)
//note, the length of someVector must be equal to the product of numRows and numCols
In R, how do you create an n row by m column matrix?
Answer:
matrix(elementValues, n, m)
//this will create an n x m matrix where all elements have a value of elementValues
//if elementValues = 0 , all the elements of the n x m matrix will be 0
//if elementValues is a vector of length equal to the product of n and m, then the matrix will contain all the elements of the vector populated from top to bottom, then left to right
matrix(elementValues, n, m)
//this will create an n x m matrix where all elements have a value of elementValues
//if elementValues = 0 , all the elements of the n x m matrix will be 0
//if elementValues is a vector of length equal to the product of n and m, then the matrix will contain all the elements of the vector populated from top to bottom, then left to right
In R, how do you neglect elements with an assigned value NA in a vector when it is used in someFunction?
Answer:
//use
help(someFunc)
//if someFunc has the provision, na.rm = FALSE, then
someFunc(someVector, na.rm = TRUE)
//this will neglect the elements in the vector someVector with NA values
//use
help(someFunc)
//if someFunc has the provision, na.rm = FALSE, then
someFunc(someVector, na.rm = TRUE)
//this will neglect the elements in the vector someVector with NA values
In R, how do you create a scatter plot given two vectors, x and y?
Answer:
plot(x, y)
//where the x vector will represent the x-axis and the y vector will represent the y-axis
plot(x, y)
//where the x vector will represent the x-axis and the y vector will represent the y-axis
In R, how do you access the nth element in a vector someVector?
Answer:
someVector[n]
//note: unlike most programming languages where array indices are zero-based, in R, the first element's index is 1
someVector[n]
//note: unlike most programming languages where array indices are zero-based, in R, the first element's index is 1
In R, how could you make a vector filled with values from 1 to 50?
Answer:
Method 1
c(1:50)
//note, this uses the c (combine) function and start:end notation
Method 2
seq(1,50)
//note, this uses the seq function, and instead of a colon (:), a comma is placed between the start and end numbers
Method 1
c(1:50)
//note, this uses the c (combine) function and start:end notation
Method 2
seq(1,50)
//note, this uses the seq function, and instead of a colon (:), a comma is placed between the start and end numbers
In R, what happens when you try to create a vector with different types (modes)?
Answer:
R will convert all values to a single mode, usually to a string, since a vector's elements need to be of the same type (mode)
R will convert all values to a single mode, usually to a string, since a vector's elements need to be of the same type (mode)
In R, how do you run a script names someScript?
Answer:
source("someScript")
source("someScript")
In R, how can you list files in the current working directory?
Answer:
list.files()
list.files()
In R, how do you find help on a function?
Answer:
help(functionName)
example:
help(sqrt)
//will bring up the R documentation on the sqrt function
help(functionName)
example:
help(sqrt)
//will bring up the R documentation on the sqrt function
In R, wow do you store a value in a variable someVar?
someVar <- 7
// this assigns the value 7 to someVar
someVar <- "Hello World!"
//this assigns a string to someVar
someVar <- TRUE
//this assigns the logical value of TRUE to someVar
// this assigns the value 7 to someVar
someVar <- "Hello World!"
//this assigns a string to someVar
someVar <- TRUE
//this assigns the logical value of TRUE to someVar
In R, what is the shorthand for FALSE?
Answer: F
In R, what is the shorthand for TRUE?
Answer: T
Sunday, October 20, 2013
#Javascript - How can I reverse a string?
The reverse() method for arrays can be used together with the split() and join() methods to reverse a string.
Given:
var someString = "Hello World!";
var reversedString = someString.split("").reverse().join("");
Test it out below!
{{someText.split("").reverse().join("")}}
Given:
var someString = "Hello World!";
var reversedString = someString.split("").reverse().join("");
Test it out below!
{{someText.split("").reverse().join("")}}
Saturday, October 19, 2013
#Codeschool Sample Solution to Convert Blocking
var fs = require('fs');
var contents = fs.readFile('index.html', function (err, contents) {
if (err) throw err;
console.log(contents);
});
console.log(contents);
#Codeschool Sample Solution to Hello You
var http = require('http');
http.createServer(function(request, response) {
// Answer goes here.
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello, this is World");
response.end();
}).listen(8080);
Friday, October 11, 2013
#LearnStreet: Teacher's Preview Assignment Lesson 10 Sample Solution
def who_are_you(green, large):
# Write if-elif-else statements below
if green and large:
return "hulk"
elif green:
return "alien"
elif large:
return "elephant"
else:
return "Bruce Banner"
#This is just for you to see what happens when the function is called
print who_are_you(True, True)
# Write if-elif-else statements below
if green and large:
return "hulk"
elif green:
return "alien"
elif large:
return "elephant"
else:
return "Bruce Banner"
#This is just for you to see what happens when the function is called
print who_are_you(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 9 Sample Solution
def kitchen_or_bed(hungry, thirsty):
# your if statement here
if hungry or thirsty:
return "go to kitchen"
else:
return "go to bed"
#This is just for you to see what happens when the function is called
print kitchen_or_bed(True, True)
# your if statement here
if hungry or thirsty:
return "go to kitchen"
else:
return "go to bed"
#This is just for you to see what happens when the function is called
print kitchen_or_bed(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 8 Sample Solution
def should_eat(hungry, awake):
#your if statement here
if hungry and awake:
return "eat"
else:
return "don't eat"
#This is just for you to see what happens when the function is called
print should_eat(True, True)
#your if statement here
if hungry and awake:
return "eat"
else:
return "don't eat"
#This is just for you to see what happens when the function is called
print should_eat(True, True)
#LearnStreet: Teacher's Preview Assignment Lesson 7 Sample Solution
def check_interval():
#Your code here
if num >= 1 & num <=5:
return "1-5"
elif num >= 6 & num <=10:
return "6-10"
elif num >= 11 & num <=15:
return "11-15"
elif num >= 16 & num <=20:
return "16-20"
else:
return "not in between 1-20"
#This is just for you to see what happens when the function is called
print check_interval()
#Your code here
if num >= 1 & num <=5:
return "1-5"
elif num >= 6 & num <=10:
return "6-10"
elif num >= 11 & num <=15:
return "11-15"
elif num >= 16 & num <=20:
return "16-20"
else:
return "not in between 1-20"
#This is just for you to see what happens when the function is called
print check_interval()
#LearnStreet: Teacher's Preview Assignment Lesson 6 Sample Solution
def check_length(phrase):
# your if condition here
if len(phrase) < 30:
return 1
# your elif condition here
elif len(phrase) == 30:
return 2
# your else condition here
else:
return 3
#This is just for you to see what happens when the function is called
print check_length("hi, i am a phrase")
# your if condition here
if len(phrase) < 30:
return 1
# your elif condition here
elif len(phrase) == 30:
return 2
# your else condition here
else:
return 3
#This is just for you to see what happens when the function is called
print check_length("hi, i am a phrase")
#LearnStreet: Teacher's Preview Assignment Lesson 5 Sample Solution
def colorful_conditions():
color = "blue"
if color == "red":
return "first block"
elif color == "white":
return "second block"
elif color == "blue":
return "third block"
else:
return "fourth block"
#This is just for you to see what happens when the function is called
print colorful_conditions()
color = "blue"
if color == "red":
return "first block"
elif color == "white":
return "second block"
elif color == "blue":
return "third block"
else:
return "fourth block"
#This is just for you to see what happens when the function is called
print colorful_conditions()
#LearnStreet: Teacher's Preview Assignment Lesson 4 Sample Solution
def check_condition():
#return this if the condition is True
ifRun = "if code block run"
#return this is if the condition is False
elseRun = "else code block run"
if condition == True:
return ifRun
else:
return elseRun
#This is just for you to see what happens when the function is called
print check_condition()
#return this if the condition is True
ifRun = "if code block run"
#return this is if the condition is False
elseRun = "else code block run"
if condition == True:
return ifRun
else:
return elseRun
#This is just for you to see what happens when the function is called
print check_condition()
#LearnStreet: Teacher's Preview Assignment Lesson 3 Sample Solution
flag = "unchanged"
def always_false():
global flag
if False:
flag = "changed"
#This is just for you to see what happens when the function is called
always_false()
print flag
def always_false():
global flag
if False:
flag = "changed"
#This is just for you to see what happens when the function is called
always_false()
print flag
#LearnStreet: Teacher's Preview Assignment Lesson 2 Sample Solution
def always_true():
if 1 == 1:
#insert your code here
return "complete"
#This is just for you to see what happens when the function is called
print always_true()
if 1 == 1:
#insert your code here
return "complete"
#This is just for you to see what happens when the function is called
print always_true()
#LearnStreet: Teacher's Preview Assignment Lesson 1 Sample Solution
def check_wounds():
#your code here
arms = 0
if arms == 1:
return "tis but a scratch"
elif arms == 0:
return "flesh wound"
else:
return "cross bridge"
#This is just for you to see what happens when the function is called
print check_wounds()
#your code here
arms = 0
if arms == 1:
return "tis but a scratch"
elif arms == 0:
return "flesh wound"
else:
return "cross bridge"
#This is just for you to see what happens when the function is called
print check_wounds()
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
//Sample Solution for MainController.js app.controller('MainController', ['$scope', function($scope) { $scope.title = ...
-
Question: What current is required for full scale deflection of a galvanometer having a current sensitivity of 50 micro-amperes per scale di...
-
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...
-
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
-
var my_canvas=document.getElementById('canvas'); var context=my_canvas.getContext('2d');
-
#Sample Solution --- title: "Data Cleaning in R" output: html_notebook --- ```{r message=FALSE, warning=FALSE} # load libra...
-
# 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 const gameState = {} function preload() { this.load.image('codey', 'https://s3.amazonaws.com/codecademy-...
-
Question: How do I startup Blend for Visual Studio once it has been installed? Answer: Click on the Windows button at the bottom left co...
-
Instructions: Count the number of rows from the flights table, where arr_time is not null and the destination is ATL. Sample Solution: S...