Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Grangaard spazm

View GitHub Profile
@spazm
spazm / _provided.rs
Last active May 4, 2020
Ransom Note (leetcode)
View _provided.rs
/**
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
@spazm
spazm / _provided.rs
Last active May 1, 2020
code puzzles
View _provided.rs
// Provided code
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
val: i32,
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
@spazm
spazm / attempt_3.rs
Last active Apr 30, 2019
matching brackets rust code. Attempt #2
View attempt_3.rs
pub fn brackets_are_balanced(string: &str) -> bool {
// second attempt/idea:
// push opening brace onto array
// pop array on close and check for match
// pop on empty => false
// not empty at end => false
// third attempt:
// make a hashmap of open/close remove code duplication in the
// close bracket logic
// Is this really the best way to make a fixed hashmap? Also, we
View Protein_name.rs
use itertools::Itertools;
use std::collections::HashMap;
/// Split a string into substrings of length sub_size
/// Final element may be shorter than sub_size
fn sub_strings(source: &str, sub_size: usize) -> Vec<String> {
source
.chars()
.chunks(sub_size)
.into_iter()
@spazm
spazm / ssh-rc
Last active Jan 23, 2019
ssh rc file ( `.ssh/rc` ) that symlinks ssh auth sock to a known location.
View ssh-rc
MY_SSH_AUTH_SOCK=$HOME/.ssh/ssh_auth_sock
if [ -n "$SSH_AUTH_SOCK" ] ; then
if [ -L $MY_SSH_AUTH_SOCK ] && [ -e $MY_SSH_AUTH_SOCK ]; then
[ -n "$DEBUG" ] && echo "SSH_AUTH_SOCK is still valid"
else
[ -n "$DEBUG" ] && echo "linking SSH_AUTH_SOCK to $MY_SSH_AUTH_SOCK"
ln -fs "$SSH_AUTH_SOCK" "$MY_SSH_AUTH_SOCK"
fi
fi
@spazm
spazm / .vimrc
Last active Oct 21, 2020
vimrc code for setting light/dark from environment variable and zsh aliases to set the ENV
View .vimrc
" check ITERM_PROFILE for 'DARK' or 'LIGHT' substrings, set background to match.
if $ITERM_PROFILE =~? 'DARK\c'
set background=dark
endif
if $ITERM_PROFILE =~? 'LIGHT\c'
set background=light
endif
@spazm
spazm / 0: create the mapping
Last active Aug 17, 2016
elasticsearch: strings in integer field
View 0: create the mapping
curl -XPUT "https://localhost:9200/int_vs_str_test" -d '{"mappings": { "foo" : { "properties": { "label_id": { "type": "integer"}}}}}'
{"acknowledged":true}
@spazm
spazm / ff-merge-check.sh
Last active Jan 29, 2016
Check if git branch could be successfully merged via fast-forward.
View ff-merge-check.sh
#!/bin/sh
set -e
# Check if HEAD and REF can be related by a fast-forward merge.
# Requires that either HEAD or REF be the merge-base between the two branches
usage () {
cat << EOM
$0 -v -h REF
Checks if HEAD and REF can be fast-forwared to each other.
@spazm
spazm / post-checkout
Created Jun 24, 2015
.git/hooks/post-checkout for logging branch changes. Will mine this to see what sort of frecency map would be useful for finding "hot" branches
View post-checkout
#!/bin/sh
previous_head_ref=$1
new_head_ref=$2
is_branch_checkout=$3
if [[ "$previous_head_ref" != "$new_head_ref" ]] && [[ "$is_branch_checkout" == 1 ]]; then
branch=$(git rev-parse --abbrev-ref HEAD)
#if [[ "develop" != "$branch" ]]; then
path="$(dirname "$0")/.."
View gist:926060205fde73d2b858
# wrapper around aws s3 to simpllify get/put of secure resources from our s3 bucket.
AWS:= aws
S3:=s3://secure.example.com
S3PATH:=/
FILE=secure.conf
REMOTE=${S3}${S3PATH}${FILE}
help:
You can’t perform that action at this time.