Skip to content

Instantly share code, notes, and snippets.

@finsterthecat
finsterthecat / update-log.json
Created November 13, 2012 21:42
Update Log
{
"metadata" : {
"created" : "2013-10-17 13:45:20",
"author" : "Tony Brouwer"
},
"data" : {
[
{
"oen" : 123456,
"action" : "update",
@finsterthecat
finsterthecat / euler8.rb
Last active February 8, 2019 03:39
euler8: Find biggest product for 5 consecutive numbers
num = <<NUM
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
@finsterthecat
finsterthecat / gist:1176968
Last active January 16, 2018 21:01
Euler Problem #7 - 10001st prime number (much (20x!) faster - now break when primes > sqrt prime candidate)
primes = [2]
count = 1
prime_candidate = 2
start = Time.now()
until (count == 10001) do
prime_candidate +=1
while (true) do
psqrt = Math.sqrt(prime_candidate)
unless primes.
find do |prime|
@finsterthecat
finsterthecat / gist:1175976
Last active September 26, 2015 23:27
Euler Problem #5 in Ruby - Smallest number divisible by each number in 1..20
factors = []
(1..20).each do |x|
rem = x
factors.each do |f|
rem /= f if rem % f == 0
end
factors << rem if rem > 1
end
puts "The answer is... " + factors.inject(1) {|prod, f| prod *= f}.to_s
@finsterthecat
finsterthecat / euler3.rb
Created June 18, 2011 21:40
Euler Problem #3 in Ruby - Find largest prime factor
def big_prime(x)
prime = x
(2..Math.sqrt(x).to_i).each do |i|
break if prime <= i
prime /= i while (prime > i && prime % i == 0)
end
prime
end
s = Time.new
@finsterthecat
finsterthecat / naics_synonym_listener.rb
Created May 25, 2011 20:53
Simple REXML-SAX stream listener example
require 'rubygems'
require 'rexml/document'
require 'rexml/streamlistener'
include REXML
class NaicsSynonymListener
include StreamListener
def initialize(f)
@outfile = File.open(f, 'w')
@finsterthecat
finsterthecat / uncomment.sh
Created April 14, 2011 15:17
Remove all comment lines. Shows how to invoke ruby as line filter using regex
#Remove all line comments. Backup may be important...
ruby -pi.bak -e '$_.sub!(/^#.*/, "")' /work/test.txt
@finsterthecat
finsterthecat / CatgManufacturingMachine.rb
Created March 18, 2011 15:11
State machine to read spreadsheet contain classes, categories and subcategories and produce sql inserts to populate relational db.
require 'CSV'
class CatgManufacturingMachine
attr_accessor :main_class, :catg_class
attr_reader :cur_catg
# Base state - always reset on blank line
class StateLook
def blank_line(o)
o.reset
@finsterthecat
finsterthecat / update_duplicates.sql
Created February 22, 2011 18:23
Fix duplicates. Useful for when you want to add an alternate key to fields that currently have dups.
update searchable s1
set datasource_guid = datasource_guid || ' @@DUPLICATE@@ ' || searchable_id,
published_flag = 'N'
where s1.searchable_id >
(
select min(s2.searchable_id)
from searchable s2
where s1.datasource_id = s2.datasource_id
and s1.datasource_guid = s2.datasource_guid
);
@finsterthecat
finsterthecat / modify_sql_ddl_script.py
Created January 31, 2011 18:42
Drops certain options and qualifiers from create table statements. Could this be any more complicated?
import re, sys, getopt
from collections import deque
DEFAULT_STATEMENT_TERMINATOR = ';'
OPTION_PATTERN = re.compile('''^--<ScriptOptions (.*)/>''', re.I)
TERM_OPTION_PATTERN = re.compile('''^--<ScriptOptions.*statementTerminator="(.*)"/>''', re.I)
TERM_OPTION = '''--<ScriptOptions statementTerminator="%s"/>'''
LOGGING_PATTERN = re.compile('''(.*)^\s+LOGGING\s*$(.*)''', re.I + re.S + re.M)