Skip to content

Instantly share code, notes, and snippets.

View jenny-codes's full-sized avatar

Jenny Shih jenny-codes

View GitHub Profile
@jenny-codes
jenny-codes / dynamic_programming_recipe.rb
Created October 26, 2024 17:09
dynamic_programming_recipe
# The recipe for doing dynamic programming
def dynamic_programming(table, recipe, data)
if !table[recipe[:key][data]].nil?
table[recipe[:key][data]]
elsif recipe[:cond].call(data)
table[recipe[:key][data]] = recipe[:then].call(data)
else
table[recipe[:key][data]] = recipe[:after].call(
recipe[:before].call(data).map { |d| dynamic_programming(table, recipe, d) }
)
@jenny-codes
jenny-codes / collage_struct_template.plist
Created December 10, 2021 05:00
Simulate race condition during backup collage upload
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>scraps</key>
<array>
<dict>
<key>image</key>
<dict>
<key>source_url</key>
@jenny-codes
jenny-codes / list_records_with_duplicate_fields.sql
Created June 1, 2021 03:15
SQL query to list records with duplicate fields
# table: _TABLE_
# possibly duplicated field: _FIELD_
select id, _FIELD_, duplicate_count from (
select id, _FIELD_,
count(_FIELD_) over (partition by _FIELD_) as duplicate_count
from _TABLE_ where _FIELD_ is not null
) as processed_table
where duplicate_count > 1
order by duplicate_count DESC, _FIELD_;
@jenny-codes
jenny-codes / sql_query_with_arbitrary_order.md
Last active May 30, 2021 08:42
SQL query with arbitrary order

Question

Fetch all the columns from table artists with id among (1, 2, 3) and with the arbitrary order (2, 3, 1)

Solutions

# Option 1
select * from artists join (values(1, 2), (2, 3), (3, 1)) as t (id, ordering) using (id) order by ordering;

# Option 2
select * from artists join unnest(array[2, 3, 1]) WITH ORDINALITY t(id, ordering) using (id) order by ordering;
for i in {0..255}; do feature/3026-start-feed-support-ab-testing-api
printf "\x1b[38;5;${i}mcolour${i}\x1b[0m\n"
done
@jenny-codes
jenny-codes / unit-testing-example-restaurtant-test.rb
Created August 30, 2020 06:04
The Example in A Field Guide to Unit Testing series in https://jennycodes.me
# frozen_string_literal: true
require 'rspec'
require_relative 'restaurant'
RSpec.describe Restaurant::Server do
describe '#take_order' do
it 'with French toast passes the order to Cook' do
expect_any_instance_of(Restaurant::Cook).to receive(:make_french_toast)
@jenny-codes
jenny-codes / assert_database_queries_example.rb
Last active March 9, 2020 00:17
Asserting the number of database queries or cache lookups performed for tests
require 'path/to/test_helper'
class ExampleTest
test 'this should trigger 1 database query' do
assert_db_queries(1) do
# ... code
end
end
end
@jenny-codes
jenny-codes / custom_graphql_newrelic_tracing.rb
Last active February 21, 2020 13:20
a code example that enables custom GraphQL tracing on NewRelic
class CustomGraphqlNewrelicTracing < GraphQL::Tracing::NewRelicTracing
# We need to create a singleton method here that inherit from the original/parent class.
self.platform_keys = GraphQL::Tracing::NewRelicTracing.platform_keys
# This is the only method we need to overwrite.
def platform_trace(platform_key, key, data)
if key == 'execute_query'
operation = data[:query].selected_operation
# Possible type: 'query', 'mutation', 'subscription'
@jenny-codes
jenny-codes / literal_calculation.rb
Created December 5, 2019 23:50
an interface that allows basic calculations with strings
%i(one two three four five six seven eight nine ten).each_with_index do |num_literal, i|
define_method(num_literal) do |block = nil|
block ? block.call(i + 1) : i + 1
end
end
def plus(second)
lambda { |first| first + second }
end
module Resolvers
class RecommendedItems < GraphQL::Schema::Resolver
# Return type and arguments can be specified either here or in the field arguments.
# Uncomment the following if you want to put them here.
#
# type [CategoryType], null: false
#
# argument :order_by, Types::ItemOrder, required: false
# argument :category, Types::ItemCategory, required: false