Skip to content

Instantly share code, notes, and snippets.

View mjhea0's full-sized avatar

Michael Herman mjhea0

View GitHub Profile
@mjhea0
mjhea0 / py-linked in
Created November 13, 2012 19:36
Super simple Python script for pulling your Linked-In contacts and outputting them to the screen
#!/usr/bin/env python
import oauth2 as oauth
import httplib2
import time, os
#authenticate
api_key = 'XXXXX'
api_secret = 'XXXXX'
user_token = 'XXXXX'
@mjhea0
mjhea0 / 1_to_5.rb
Created March 25, 2013 17:47
Ruby feels like Perl in that there's a TON of different ways to do one thing. Bloated API? Theme music: Ween - So many people in the neighborhood
# loop
x = 0
loop do
x += 1
puts x
break if x > 4
end
puts
@mjhea0
mjhea0 / procs_multiples_5_and_7.rb
Created April 23, 2013 15:31
Find the sum of all the multiples of 5 or 7 below 100
# create a Proc to use with a Block
multiples = Proc.new do |n|
n % 5 == 0 || n % 7 == 0
end
# turn the Proc into a Block
array = (1..99).to_a.select(&multiples)
# sum the members of the array
array.inject{|sum,x| sum + x }
@mjhea0
mjhea0 / map_vs_collect.rb
Created April 23, 2013 15:51
map and collect do the exact same thing - why? why don't they deprecate one of them? why do i see map used more?
numbers = (1..10)
array1 = numbers.to_a
array2 = numbers.to_a
# map
strings_map = array1.map(&:to_s)
# collect
strings_collect = array2.collect(&:to_s)
@mjhea0
mjhea0 / odd_values_fibonacci.rb
Created April 23, 2013 16:33
Find the sum of the odd-valued terms of the Fibonacci sequence whose values do not exceed 2,000.
num1 = 0
num2 = 1
i = 0
total = 0
while i <= 2000
# add previous two values
i = num1 + num2
# if i is odd, add it to the total
if i % 2 != 0
@mjhea0
mjhea0 / datatypes_from_an_array.rb
Created April 23, 2013 20:03
Pull out specific data types from an array via Blocks.
array = [43, "Foo Fighters", 45.7, "not_true", :beans, 8, 42.34]
strings = array.select {|x| x.is_a? String}
symbols = array.select {|x| x.is_a? Symbol}
integers = array.select {|x| x.is_a? Integer}
floats = array.select {|x| x.is_a? Float}
@mjhea0
mjhea0 / basic_ruby_class.rb
Created April 23, 2013 22:44
super basic ruby class
class Person
# initialize fire's up each object in the class
def initialize(name)
# @ indicates that the variable is attached to the instance of the class
@name = name
end
def name
puts "Hi, my name is #{@name}."
end
end
@mjhea0
mjhea0 / missing_socks.rb
Last active December 16, 2015 15:29
find your missing socks / for polina / http://screencast.com/t/EckaqSCJco
system("clear")
socks = Hash.new
puts
puts "To find your missing sock(s) -"
puts " (1) Enter all of your sock colors/patterns."
puts " (2) Type \"quit\" when done."
puts " (3) Look at the results."
puts
puts
@mjhea0
mjhea0 / guessing_game.rb
Last active December 16, 2015 15:59
just a basic guessing game - cheers!
class GuessingGame
def initialize(answer)
@answer = answer
end
def guess(guess)
@guess = guess
if guess > @answer
puts "high"
return :high
@mjhea0
mjhea0 / sentiment_analysis.r
Created May 1, 2013 18:11
Sentiment Analysis via R
Create an app on https://dev.twitter.com/, make sure you leave Callback URL blank (so that it will return to a page where twitter shows you a PIN, useful during twitCred$handshake below. Don't worry yet, read on.)
Run these on R (on separate lines):
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- "yourconsumerkey"
consumerSecret <- "yourconsumersecret"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)