Skip to content

Instantly share code, notes, and snippets.

@hackjoy
hackjoy / change_column_data_type.rb
Created December 16, 2011 16:54
Ruby [Migration]: Change Field/Column Data Type
class ChangeDataTypeForWidgetCount < ActiveRecord::Migration
def self.up
change_table :widgets do |t|
t.change :count, :float
end
end
def self.down
change_table :widgets do |t|
t.change :count, :integer
@hackjoy
hackjoy / insert-method.rb
Last active June 13, 2020 14:15
Ruby [Method]: Insert/Add Characters into String
# Transforms ABCDEFGHIJKLMNOPQRSTUVWXY to ABCDE-FGHIJ-KLMNO-PQRS-UVWXY
# @number.change_to_licence
def change_to_licence
h = "-"
string = string.insert(5, h)
string = string.insert(11, h)
string = string.insert(17, h)
string = string.insert(23, h)
@hackjoy
hackjoy / count_frequency_of_string_values.rb
Created January 11, 2013 12:37
A method to count the frequency that each value within a string of comma separated values appears in another string of comma separated values. Usage: count_frequency_of_string_values(["1,2,"], ["1,2,2,2"]) => ["1-1", "2-3"]
require 'benchmark'
require 'test/unit'
def count_frequency_of_string_values(key, observation)
# convert arguments into arrays for comparison
key_array = key.split(",").map { |s| s.to_i }
observation_array = observation.split(",").map { |s| s.to_i }
# for each key - count how many matches within observation and add result to the output
output = []
key_array.each do |k|
@hackjoy
hackjoy / month_hall_problem.rb
Created January 11, 2013 15:35
Problem: Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what's behind the doors, opens another door, say No. 3, which has a goat. He then says to you, "Do you want to pick door No. 2?" Is it to your advantage to swi…
def createDoors
doors = [false, false, false]
# set one door equal to the car
doors[rand(3)] = true
doors
end
def openADoorThatHasAGoat(doors, userChoice)
# return the first false element after removing the users choice and a true value (if present)
([0, 1, 2] - [userChoice, doors.index(true)]).first
@hackjoy
hackjoy / multiply_numbers_in_array.py
Last active December 11, 2015 08:49
Return the sum of multiplying all numbers in an array
def product_list(numbers):
result = 1
for number in numbers
result = result * number
return result
@hackjoy
hackjoy / search_engine.py
Last active December 11, 2015 08:58
Simple implementation of web crawler and search engine
# pull content of a url
def get_page(url):
try:
import urllib
return urllib.urlopen(url).read()
except:
return ""
# find the links within a page
def get_next_target(page):
@hackjoy
hackjoy / sudoku_validator.py
Created January 20, 2013 13:59
Validates that sudoku grid contains once instance of each number per column and row.
def check_sudoku(grid):
n = len(grid)
digit = 1
while digit <= n:
i = 0
while i < n:
row_count = 0
column_count = 0
j = 0
while j < n:
@hackjoy
hackjoy / split_string.py
Last active December 11, 2015 09:58
Split a string based on predefined characters
def split_string(string, split_characters):
output = []
atsplit = True
for char in source:
if char in splitlist:
atsplit = True
else:
if atsplit:
output.append(char)
atsplit = False
@hackjoy
hackjoy / execution_time.py
Created January 21, 2013 22:52
Calculates time to execute a method
import time
def time_execution(code):
start = time.clock()
result = eval(code)
run_time = time.clock() - start
return result, runtime
@hackjoy
hackjoy / longest_repetition.py
Created February 6, 2013 23:38
Calculates and returns the element in a list that has repeated the most times in succession.
def longest_repetition(list):
best_length = 0
best_element = None
current_length = 0
current_element = None
for e in list:
if e != current_element:
current_element = e
current_length = 1
else: