Skip to content

Instantly share code, notes, and snippets.

View bcjordan's full-sized avatar

Brian Jordan bcjordan

View GitHub Profile
@bcjordan
bcjordan / parse_brackets.rb
Created January 18, 2011 02:43
parsing / combination exercise
# Run using rspec parse_brackets.rb
# Uses array combination/permutation method from
# http://trevoke.net/blog/2009/12/17/inter-array-permutations-in-ruby/
# Takes an array of arrays and returns permutations of them
# e.g., fancy_array_permutation[["one", "two"], ["three", "four"]]
# returns: ["onethree", "onefour", "twothree", "twofour"]
def fancy_array_permutation array
return array[0] if array.size == 1
@bcjordan
bcjordan / gist:1072952
Created July 8, 2011 22:12
nohup an already running foreground or background process
[root@bjordan-dev scripts]# ruby long_task.rb
# Press ctrl+z to suspend the process
[1]+ Stopped ruby long_task.rb
[root@bjordan-dev scripts]# bg
[1]+ ruby long_task.rb &
[root@bjordan-dev scripts]# pidof ruby
29794
[root@bjordan-dev scripts]# disown 29794
[root@bjordan-dev scripts]# exit
@bcjordan
bcjordan / gist:1225947
Created September 19, 2011 04:13
Heroku working with Facebook's iframe canvas (Sinatra)
# Replace this line:
get "/" do
# With these lines:
def get_or_post(path, opts={}, &block)
get(path, opts, &block)
post(path, opts, &block)
end
@bcjordan
bcjordan / heroku_facebook.js
Created September 19, 2011 04:41
Heroku working with Facebook's iframe canvas (Node.js and Express)
// add the following to the end of your web.js
// respond to POST /
app.post('/', function(request, response) {
response.redirect('/home');
});
@bcjordan
bcjordan / instant_movies.rb
Created October 17, 2011 20:06
Identifies some Netflix Instant movies in a Reddit thread
require 'rubygems'
require 'open-uri'
require 'pp'
require 'nokogiri'
PAGE_URL = "http://www.reddit.com/r/AskReddit/comments/lf38m/what_is_the_funniest_movie_you_have_ever_seen/"
page_text = Nokogiri::HTML(open(PAGE_URL))
page_text.css('div.usertext-body').each do |content|
@bcjordan
bcjordan / instant_watcher.rb
Created October 17, 2011 20:27
Uses InstantWatcher to find possible instant movies
require 'rubygems'
require 'open-uri'
require 'pp'
require 'nokogiri'
PAGE_URL = "http://www.reddit.com/r/AskReddit/comments/lf38m/what_is_the_funniest_movie_you_have_ever_seen/"
INSTANT_WATCHER = "http://instantwatcher.com/titles?q=QUERY&search_episodes=0"
page_text = Nokogiri::HTML(open(PAGE_URL))
@bcjordan
bcjordan / jtv-completion.js
Created October 28, 2011 17:40
Autocompletes nicknames in Justin.tv chat
// Protoype version (uses effects.js)
var names = $$('li.nick a').map(function(val){ return val.innerHTML });
new Autocompleter.Local('chat_text_input', 'related_channels', names, {});
// JQuery version
var script = document.createElement("script");
script.src = "http://code.jquery.com/jquery-latest.js";
@bcjordan
bcjordan / mongoid-hash-inc.log
Created December 28, 2011 04:46
Mongoid Hash atomic increment issue
##
## As a search result, atomic inc works fine on the 'taggings' hash:
##
>> Gif.create!()
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {}, views: 0, upvotes: 0, downvotes: 0>
>> Gif.first.inc('taggings.test', 1)
=> 1
>> Gif.first
=> #<Gif _id: 4efa9e283b44374c0b000003, _type: nil, tag_ids: [], user_ids: [], taggings: {"test"=>1}, views: 0, upvotes: 0, downvotes: 0>
  • From Joshua Harris in python (gist)

  • From Denis Karpenko in Ruby (with rspec tests!) (gist)

  • From Nikki DelRosso in python (gist):

"And an explanation of the solution (since the Python is very dense): For each value, since we're doing sums consecutively, we want to calculate the maximum that we could attain if we use the current value as the last value in the sequence. The only information we need to calculate this was what the maximum value was if we ended on the value before this in the sequence. If the previous value was negative, then the max value attainable by ending on this value is just this value. Otherwise, we add the current with the value for the previous element. All we need, then, is to iterate through our sequence and keep track of the previous best option, as well as the maximu

@bcjordan
bcjordan / index.html
Last active December 27, 2015 12:39 — forked from mpenkov/mcs.html
<html>
<head>
<title>Max Consecutive Sum</title>
</head>
<body>
Enter some integers:
<input type="text" id="txtInput" value="-1 5 6 -2 20 -50 4"></input>
<button onClick="btnOnClick();">Calculate</button>
<p>
Max consecutive sum is: <span id="divResult"></span>