This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def product_list(numbers): | |
result = 1 | |
for number in numbers | |
result = result * number | |
return result | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def time_execution(code): | |
start = time.clock() | |
result = eval(code) | |
run_time = time.clock() - start | |
return result, runtime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
OlderNewer