Morgan Evans
GitHub: NegaMorgan
BlogURL: http://negamorgan.github.io
Tagline: "Keep calm and clear cache."
Treehouse: http://teamtreehouse.com/NegaMorgan
Morgan Evans
GitHub: NegaMorgan
BlogURL: http://negamorgan.github.io
Tagline: "Keep calm and clear cache."
Treehouse: http://teamtreehouse.com/NegaMorgan
require 'json' | |
require 'rest-client' | |
r = JSON.parse(RestClient.get('http://reddit.com/.json')) | |
p = r["data"]["children"] | |
posts_array = p.collect { |post| post["data"] } | |
# posts_array is an array of post hashes | |
# you can iterate over posts_array or do posts_array[0]["url"] |
# Euler problem 3 | |
# used my old prime method from school week 1 | |
class LargestPrime | |
def largest(number) | |
Math.sqrt(number).to_i.downto(2) do |i| | |
# prime factor is a prime number that divides the integer exactly | |
return i if number % i == 0 && prime?(i) | |
end | |
end |
def solution(digits) | |
group_by_five(digits).max | |
end | |
def group_by_five(digits) | |
digits.split("")[0..-5].map.with_index do |digit, i| | |
"#{digit}#{digits[i+1]}#{digits[i+2]}#{digits[i+3]}#{digits[i+4]}" | |
end | |
end |
def largest_product(digits) | |
product_of_five(digits).max | |
end | |
def product_of_five(digits) | |
digit_array = digits.to_s.split("") | |
digit_array[0..-5].map.with_index do |digit, i| | |
digit.to_i * digit_array[i+1].to_i * digit_array[i+2].to_i * digit_array[i+3].to_i * digit_array[i+4].to_i | |
end | |
end |
def fizzbuzz(n) | |
1.upto(n).map do |num| | |
response = "" | |
response << "Fizz" if num % 3 == 0 | |
response << "Buzz" if num % 5 == 0 | |
response.empty? ? num : response | |
end | |
end |
1.upto(999).select do |n| | |
n % 3 == 0 || n % 5 == 0 | |
end.reduce(:+) |
def even_fibonacci_numbers | |
fibonacci_numbers.select {|n| n.even? }.reduce(:+) | |
end | |
def fibonacci_numbers | |
numbers = [1, 2] | |
while numbers[-1] < 4_000_000 do | |
numbers << numbers[-2] + numbers[-1] | |
end | |
numbers[0..-2] |
This list is meant to be a both a quick guide and reference for further research into these topics. It's basically a summary of that comp sci course you never took or forgot about, so there's no way it can cover everything in depth. It also will be available as a gist on Github for everyone to edit and add to.
###Array ####Definition: