This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Q1. | |
# "def color" is a regular method and it will create a new instance method, when used without an explicit receiver. | |
# In the context of a class, self refers to the current class. Defining a method on self.shape creates a class method. | |
# def bar.radius creates singleton method attached to the object bar. | |
# The difference between class methods and singleton methods is that class methods are available to all instances of | |
# a class object while singleton methods are available only to that single instance. | |
------------------------------------------------------------------------------------ | |
Q2. | |
module Enumerable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def benchmark | |
start_time = Time.now | |
yield | |
end_time = Time.now | |
x = end_time - start_time | |
end | |
# Be careful, pasting this into IRB will take a long time to print. | |
# It's a loooong string. :) | |
long_string = "apple"*100000000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const swapper = function(key1, object1, key2, object2) { | |
console.log('Swap!'); | |
const keyValue1 = object1[key1] //=> 1 | |
const keyValue2 = object2[key2] //=> 5 | |
object1[key1] = keyValue2 // a : 5 | |
object2[key2] = keyValue1 // c : 1 | |
console.log('object1: ', object1); | |
console.log('object2: ', object2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const whereCanIPark = function (spots, vehicle) { | |
let coordinates = false; | |
for(i = 0; i < spots.length; i++) { | |
for(j = 0; j < spots[i].length; j++) { | |
if(vehicle === 'regular') { | |
if(spots[i][j] === 'R') { | |
coordinates = [j,i]; | |
break; | |
} | |
} else if (vehicle === 'small') { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const urlDecode = function(text) { | |
// Put your solution here | |
result = {}; | |
let splitText = text.replace(/%20/g, ' ') | |
.split('&') | |
.map(x => x.split("=")); | |
splitText.forEach( function(elm) { | |
// statements | |
result[elm[0]] = elm[1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const instructorWithLongestName = function(instructors) { | |
let sorted = instructors.sort(function(a, b) { | |
return b.name.length - a.name.length; | |
}); | |
return sorted[0]; | |
}; | |
console.log(instructorWithLongestName([ | |
{name: "Samuel", course: "iOS"}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let numberOfVowels = function(data) { | |
let count = 0; | |
for (var i = 0; i < data.length; i++) { | |
if(data[i] === 'a' || data[i] === 'e' || data[i] === 'i' || data[i] === 'o' || data[i] === 'u') { | |
count++; | |
} | |
} | |
return count; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let conditionalSum = function(values, condition) { | |
var sum = 0; | |
values.forEach( function(value) { | |
if(condition == "even" && value % 2 === 0) { | |
sum += value; | |
} else if (condition == 'odd' && value % 2 !== 0) { | |
sum +=value; | |
} | |
}); | |
return sum; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sumLargestNumbers = function(data) { | |
let sorted = data.sort(function(a,b){ | |
return b - a; | |
}) | |
return sorted[0] + sorted[1]; | |
}; | |
console.log(sumLargestNumbers([1, 10])); | |
console.log(sumLargestNumbers([1, 2, 3])); | |
console.log(sumLargestNumbers([10, 4, 34, 6, 92, 2])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start = dt.datetime(20XX, 1, 1) | |
end = dt.datetime.now() | |
for ticker in tickers: | |
if not os.path.exists('stock_dfs/{}.csv'.format(ticker)): | |
df = pdr.DataReader(ticker.replace('.', '-'), 'yahoo', start, end) | |
df.reset_index(inplace=True) | |
df.set_index("Date", inplace=True) | |
df.to_csv('stock_dfs/{}.csv'.format(ticker)) | |
else: |
NewerOlder