Skip to content

Instantly share code, notes, and snippets.

View joebartels's full-sized avatar

Joseph Bartels joebartels

View GitHub Profile
@joebartels
joebartels / fibonacci.js
Last active October 25, 2018 20:40
algorithms
/**
* Recursively computes the nth fibonachi #. This method is super slow because
* there is a tonnn of duplicated work.
*
* runtime: O(n*(n-1)(n-2) + n(n-2)) = O(n^2)
*
* @param {number} n
* @return {number}
*/
function naiveFibonacci(n) {
select sum(km_cycled) as total_km_cycled,
case when day_of_week=0 then 'Sunday'
when day_of_week=1 then 'Monday'
when day_of_week=2 then 'Tuesday'
when day_of_week=3 then 'Wednesday'
when day_of_week=4 then 'Thursday'
when day_of_week=5 then 'Friday'
when day_of_week=6 then 'Saturday'
end as day
from cycling_entry
@joebartels
joebartels / medium.date_fields_vs_extract.query_cycling_entry_by_day_of_week_and_hour_of_day.sql
Last active September 12, 2018 15:39
query cycling_entry by day of week and hour of day
select sum(km_cycled) as km_cycled_for_all_mondays
from cycling_entry
where day_of_week = 1
and hour_of_day > 11;
-- result: 39982
-- average time: 7.25ms
select sum(km_cycled) as km_cycled_for_all_mondays
from cycling_entry
where extract(dow from date) = 1
@joebartels
joebartels / medium.date_fields_vs_extract.query_cycling_entry_by_day_of_week.sql
Created September 12, 2018 15:23
query time for cycling_entry using date field and extract() function
select *
from cycling_entry
where day_of_week = 1;
-- average time: 8.75ms
select *
from cycling_entry
where extract(dow from date) = 1;
-- average time: 21ms
@joebartels
joebartels / medium.date_fields_vs_extract.table_cycling_entry.md
Last active September 12, 2018 14:50
data structure for km_cycled on given date
id date                    day_of_week hour_of_day minute_of_hour km_cycled
1  1988-01-04 00:48:00-05  1           0           48             45
2  1988-01-18 16:40:00-05  1           16          40             4
3  1988-01-18 11:30:00-05  1           11          30             45
with date_fields as (
select id,
extract(dow from date) as day_of_week,
extract(hour from date) as hour_of_day,
extract(minute from date) as minute_of_hour
from cycling_entry
)
update cycling_entry
set day_of_week=subquery.day_of_week,
hour_of_day=subquery.hour_of_day,
create table cycling_entry (
id serial primary key,
date timestamptz,
day_of_week smallint default 0,
hour_of_day smallint default 0,
minute_of_hour smallint default 0,
km_cycled smallint
);
@joebartels
joebartels / medium_date_fields_vs_extract_generate_series_random_dates.sql
Last active September 12, 2018 00:35
using generate_series to create bunch of random dates throughout "a year"
with days as (
select *
from generate_series(
'1988-01-01T00:00:00'::timestamptz,
'2019-01-01T00:00:00'::timestamptz - '1 day'::interval,
'1 day'::interval
) as date
)
-- uncomment to insert into the table
@joebartels
joebartels / medium_date_fields_vs_extract_int_between.sql
Last active September 12, 2018 00:47
store date fields (day of week, hour, minute) vs. using extract to query postgres
-- returns a random number from within a range (inclusive)
create or replace function int_between(
in low int,
in high int,
out int
)
returns int
language sql
as $$
select floor(random() * (high - low + 1) + low)::int
# setup
customer = Stripe::Customer.create(email: 'bob@bobsburgers2.soy')
customer.sources.create(source: { object: 'card', exp_month: 12, exp_year: 22, number: '4242424242424242', cvc: 123 })
coupon = Stripe::Coupon.create(id: 'THEDUDEABIDES_2', duration: 'forever', percent_off: 25)
plan_month_1 = Stripe::Plan.create(id: 'tmp_monthly_plan_1', name: 'MONTH 1', interval: 'month', amount: 9900, currency: 'usd')
plan_month_2 = Stripe::Plan.create(id: 'tmp_monthly_plan_2', name: 'MONTH 2', interval: 'month', amount: 9900, currency: 'usd')
plan_year = Stripe::Plan.create(id: 'tmp_annual_plan', name: 'YEAR 1', interval: 'year', amount: 999900, currency: 'usd')