Skip to content

Instantly share code, notes, and snippets.

View connordavison's full-sized avatar

C. Davison connordavison

View GitHub Profile
@connordavison
connordavison / gnome-screenshot-imgur
Last active May 7, 2020 16:46
gnome-screenshot-imgur
#!/bin/bash
# gnome-screenshot-imgur
# Upload a screenshot directly to imgur from CLI; see `man gnome-screenshot` for
# params (-f is reserved)
#
# Generate temp file for image
tmp=$(mktemp --suffix=.png)
# Take screenshot as per user's params
@connordavison
connordavison / safe_rm
Last active September 21, 2015 02:09
safe_rm
#!/bin/bash
SCRIPT=$(basename "$0")
DELETED=$HOME/deleted
# Ensure mandatory files/directories exist
touch $HOME/.restore.info
mkdir -p $DELETED
# Parse options
while getopts "ivr" opt; do
@connordavison
connordavison / oc-ga-3p.js
Last active October 30, 2015 14:46
GA Cross-Domain Linking (local)
if ('undefined' === typeof ga) {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
}
ga('create', 'UA-60598043-2', 'auto', {'allowLinker': true, 'name': 'oc'});
ga('oc.require', 'linker');
ga('oc.linker:autoLink', ['primary.co.uk']);
@connordavison
connordavison / count-duplicates-both-ways.php
Last active October 9, 2015 08:52
Functions for identifying duplicates within arrays.
<?php
/**
* Count duplicates in an array before and after each element.
*
* <code>
* $counts = count_duplicates_both_ways([0, 1, 1, 2, 2, 1]);
* print_r($counts);
* // [
* // 'before' => [0, 0, 1, 0, 1, 2],
* // 'after' => [0, 2, 1, 1, 0, 0]
@connordavison
connordavison / if-idle
Last active October 3, 2015 15:25
Execute command if idle
#!/bin/bash
#
# Execute a command if the user is idle for a specified period of time
# Requires: xprintidle
# Usage: if-idle IDLE_MIN IDLE_COMMAND [NOT_IDLE_COMMAND]
#
IDLE_MIN=$1
IDLE_TIME=$(xprintidle)
@connordavison
connordavison / Solution.java
Last active October 13, 2015 15:25
Count number of matching elements in two integer arrays
import java.util.Arrays;
class Solution {
public static int solve(int[] A, int[] B) {
int i = 0;
int j = 0;
Arrays.sort(A);
Arrays.sort(B);
@connordavison
connordavison / Solution.java
Created October 8, 2015 21:50
Counter number of n-duplicates in an array
import java.util.HashMap;
import java.util.Map;
class Solution {
public static int solve(int[] A, int n) {
HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
int i;
for (int a : A) {
<?php
function solution($A, $B) {
// Build an array relating maximum circumferences to depths
$smallest_at = [-1 => PHP_INT_MAX];
for ($i = 0; $i < count($A); $i++) {
$smallest_at[$i] = min($smallest_at[$i - 1], $A[$i]);
}
// Start dropping
@connordavison
connordavison / date_diffs.php
Created October 14, 2015 08:56
Calculating standard deviation/average of differences between a list of DateTimes, and determining if a given diff is unlikely.
<?php
/**
* Yields the list of differences between a list of datetimes.
* @param array $dates An ordered list of datetimes of which to find the
* differences.
* @return array The differences between each sequential datetime in $dates.
*/
function date_diffs($dates) {
$diffs = array();
@connordavison
connordavison / dm.sh
Created October 19, 2015 22:49
Shell script to start & daemonise a process. Detaches the process from the console & redirects its output to /dev/null.
#!/bin/bash
function iscmd()
{
if hash $1 2>/dev/null; then
return 0
fi
return 1
}
iscmd $1