Skip to content

Instantly share code, notes, and snippets.

View Coutlaw's full-sized avatar

Cass Outlaw Coutlaw

View GitHub Profile
@Coutlaw
Coutlaw / longest_substring.rs
Last active May 22, 2020 19:15
Longest substring from a vector of strings in rust
/*
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: ["flower","flow","flight"]
Output: "fl"
@Coutlaw
Coutlaw / roman_to_int.rs
Last active May 22, 2020 14:38
leetcode: roman numerals to int value
/*
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
@Coutlaw
Coutlaw / two_sum.rs
Last active May 22, 2020 10:52
Leetcode: two_sum problem in Rust
// /*
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// Example:
// Given nums = [2, 7, 11, 15], target = 9,
// Because nums[0] + nums[1] = 2 + 7 = 9,
@Coutlaw
Coutlaw / enums_solution.rs
Last active October 26, 2022 20:47
Rustlings: Enums 1, 2 & 3 solution
// Enums 1 problem (doesn't compile)
#[derive(Debug)]
enum Message {
// TODO: define a few types of messages as used below
}
fn main() {
println!("{:?}", Message::Quit);
println!("{:?}", Message::Echo);
println!("{:?}", Message::Move);
@Coutlaw
Coutlaw / clippy_solution.rs
Created May 19, 2020 10:39
Rustlings: clippy 1 & 2
// Clippy 1 problem (this does not compile because float comparison)
fn main() {
let x = 1.2331f64;
let y = 1.2332f64;
if y != x {
println!("Success!");
}
}
// Clippy1 solution
@Coutlaw
Coutlaw / redux.js
Created April 8, 2020 13:48
Redux cycle
console.clear();
// Creating action creators for 3 actions: creating a policy, creating a claim, deleting a policy
// action creator: people dropping off a form
const createPolicy = (name, amount) => {
return { // action, a form in our analogy
type: 'CREATE_POLICY',
payload: {
name: name,
@Coutlaw
Coutlaw / retryWithDelay.js
Last active January 29, 2020 01:38
Recursive retry with exponential backoff in node JS
// A delay function used for our queries
const delay = ms => new Promise(res => setTimeout(res, ms));
// Recursively try the base query when the results are null
// Delay time is 2 seconds and doubles each retry
const baseRetry = ({lead_id}, numberOfRetry = 5, delayMs = 2000) => {
return delay(delayMs).then(() => queryFunction({queryParam}).then((resp = {}) => {
const { potentiallyNullValueFromQuery } = resp;
if(numberOfRetry > 0 && !potentiallNullValueFromQuery ) {
// Retry the query recursively, removing a number of retries remaining
return queryFunction({queryParam}, numberOfRetry - 1, delayMs * 2);
@Coutlaw
Coutlaw / key_generator_script.md
Last active April 9, 2020 18:49
S3 Credential File Key Generator in Go

Key Generation With S3 Credential Files in Go

This script can be used to generate unique keys from credential files in an AWS S3 bucket. If we needed a key that was a hash of the ClientID and Client Secret, then we can use the below script to find a key for every file in a given S3 bucket. This script will use the .aws credential file on the machine that it is running on.

My Script

package main

import (
	"crypto/sha256"
@Coutlaw
Coutlaw / retrieval_script.md
Last active April 9, 2020 18:49
Mongo Cluster Migration Assistant in Go

Mongodb Node IP Retrieval Tool in Go

I was using the migrate-mongo npm package to create indexes on a 3 node mongo cluster running in AWS. The package required IP addresses to be placed in the config.js file for the migration to locate the remote cluster. To accomplish this I created a go script to retrieve the IP addresses and update the config file for migrations. The script will use the .aws credential file on the machine its running on.

About The Migration Library

Migration Package Structure

The migration package has the following structure

@Coutlaw
Coutlaw / snow.sh
Created October 19, 2018 13:10
make it snow in terminal
ruby -e 'C=`stty size`.scan(/\d+/)[1].to_i;S=["2743".to_i(16)].pack("U*");a={};puts "\033[2J";loop{a[rand(C)]=0;a.each{|x,o|;a[x]+=1;print "\033[#{o};#{x}H \033[#{a[x]};#{x}H#{S} \033[0;0H"};$stdout.flush;sleep 0.1}'