Skip to content

Instantly share code, notes, and snippets.

@negamorgan
negamorgan / negamorgan-details.md
Last active August 29, 2015 13:56
NegaMorgan profiles and bio
@negamorgan
negamorgan / basic-reddit-posts.rb
Last active August 29, 2015 13:56
Create array of reddit front page posts; no type data included
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"]
@negamorgan
negamorgan / ruby-setup.md
Last active August 29, 2015 13:56
New Ruby project set-up checklist

#New Ruby project set-up checklist

Add a simple README file

touch README.md

mkdir

  • bin
  • lib/models
  • lib/views
@negamorgan
negamorgan / largest-prime.rb
Last active August 29, 2015 13:57
Largest Prime
# 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
@negamorgan
negamorgan / five-digit-max.rb
Created March 17, 2014 21:02
Largest 5 digit number in a series
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
@negamorgan
negamorgan / euler-8.rb
Created March 18, 2014 13:29
Largest Product of Five (Euler 8)
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
@negamorgan
negamorgan / fizzbuzz.rb
Created April 30, 2014 20:17
fizzbuzz.rb
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
@negamorgan
negamorgan / euler-problem-1.rb
Created April 30, 2014 20:39
euler-problem-1.rb
1.upto(999).select do |n|
n % 3 == 0 || n % 5 == 0
end.reduce(:+)
@negamorgan
negamorgan / euler-problem-2.rb
Created April 30, 2014 21:32
euler-problem-2.rb
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]

Studying for a Tech Interview Sucks, so Here's a Cheat Sheet to Help

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.

Data Structure Basics

###Array ####Definition:

  • Stores data elements based on an sequential, most commonly 0 based, index.
  • Based on tuples from set theory.