Skip to content

Instantly share code, notes, and snippets.

@malandrina
malandrina / gist:3744867
Created September 18, 2012 18:26
Implementing a Reverse Polish Notation Calculator in Ruby

Implementing a Reverse Polish notation calculator in Ruby

A couple weeks ago I had a go at implementing an RPN calculator in Ruby. I knew I wanted the calculator to function by popping operands out of an array populated with the values of the input expression, operating upon the operands with the appropriate operator, and pushing the result back into the stack of operands.

I was able to implement this in version 1, but it took forever and the resulting code was not very beautiful. Why?

  • I started coding before I had a thorough understanding of RPN

    Wait, 20 10 5 4 + * - is what now?

@malandrina
malandrina / gist:3745321
Created September 18, 2012 19:35
Anagram solvers for same-length anagrams, in Ruby

Anagram solvers for same-length, single word anagrams, in Ruby

I wanted to write an anagram solver that would return a list of the anagrams of the same length of a given word/combination of characters, such that:

same_length_anagrams("deify") => "edify"

or

same_length_anagrams("ogb") => "bog", "gob"

@malandrina
malandrina / gist:3752723
Created September 19, 2012 22:24
Writing a faster anagram solver for same-length anagrams, in Ruby

Writing a faster anagram solver for same-length, one-word anagrams, in Ruby

In [my first attempt] (https://gist.github.com/3745321) at writing an anagram solver, I stored my word list in a hash in which values are words and keys are characters in those words, sorted alphabetically.

I liked this approach, but my implementation was not ideal: I defined the creation of the hash as an activity that occurs when the #same_length_anagrams method is called. This, of course, made it super slow!

This time I was able to structure things so that the word list hash is created when the program is initialized. The Ruby documentation for the #shell class leaves a lot to be desired, but [this very helpful blog post] (http://devver.wordpress.com/2009/10/12/ruby-subprocesses-part_3/) on starting sub-processes in Ruby provided me with a great point of departure. My next step is to figure out how to clean up lines 1 - 12.

# Create word list hash
@malandrina
malandrina / filereader.rb
Created October 27, 2012 00:00 — forked from dbc-challenges/card.rb
FlashCardinator
class FileReader
def initialize(file)
@file = file
end
def read_file
@dictionary = {}
File.open(@file, "r").each do |line|
line.gsub!(/\\/, "")
split_line = line.chomp.split("\t")
def filter_by_highest_ranked_user_coaches
coach_user_ids_string = coach_user_ids.to_s.gsub('[', '').gsub(']', '')
highest_ranked_coach_ids = ActiveRecord::Base.connection.execute(<<-TEXT
SELECT DISTINCT on (user_id) user_id, id
FROM coaches c
WHERE coachup_ranking_score =
(SELECT MAX(r.coachup_ranking_score)
FROM coaches r
WHERE c.user_id = r.user_id
class Geocoder
queryGeocodingApi: (options) ->
if options.coordinates
formattedLatlng = "#{options.coordinates.latitude},#{options.coordinates.longitude}"
@.reverseGeocode(latlng: formattedLatlng)
reverseGeocode: (options) ->
$.ajax
type: 'GET'
url: 'https://maps.googleapis.com/maps/api/geocode/json'
module GeospatialObject
METERS_PER_MILE = 1609.34
module Callbacks
def set_geographic_point
ActiveRecord::Base.connection.execute(<<-TEXT
UPDATE addresses
SET geographic_point = ST_GeographyFromText('POINT(' || longitude || ' ' || latitude || ')')
WHERE id = #{self.id}
TEXT
@malandrina
malandrina / gist:9237572
Last active August 29, 2015 13:56
HotelTonight Rails 4 Deprecation Warnings
DEPRECATION WARNING: It looks like you are eager loading table(s) (one of: agents, hoteltonight) that are referenced in a string SQL snippet. For example:
Post.includes(:comments).where("comments.title = 'foo'")
Currently, Active Record recognizes the table in the string, and knows to JOIN the comments table to the query, rather than loading comments in a separate query. However, doing this without writing a full-blown SQL parser is inherently flawed. Since we don't want to write an SQL parser, we are removing this functionality. From now on, you must explicitly tell Active Record when you are referencing a table from a string:
Post.includes(:comments).where("comments.title = 'foo'").references(:comments)
If you don't rely on implicit join references you can disable the feature entirely by setting `config.active_record.disable_implicit_join_references = true`. (called from _app_views_agents_index_html_haml__860689087346826956_70228045189400 at /Users/training/Source/thoughtbot/hotelstonight/app/vi
@malandrina
malandrina / map-config.json
Last active August 29, 2015 14:18
map config json
{
"mapStyles": {
"destinationHighlight":{
"useClientId":true,
"ease":"Back.easeInOut.config(1.7)",
"fill":"#FFFFFF"
},
"categoryHighlight":{
"ease":"Back.easeInOut.config(1.7)",
"fills":{
@malandrina
malandrina / default-map-config
Last active August 29, 2015 14:23
default map config
{
"mapStyles": {
"destinationHighlight":{
"useClientId":true,
"ease":"Back.easeInOut.config(1.7)",
"fill":"#looool"
},
"categoryHighlight":{
"ease":"Back.easeInOut.config(1.7)",
"fills":{