Skip to content

Instantly share code, notes, and snippets.

View gartenfeld's full-sized avatar

David Rosson gartenfeld

View GitHub Profile
find /dir/ -iname "*.mp3" -print
# /dir/ - Search directory
# -iname - File name search pattern (case insensitive)
# -print - Display name of files on screen
find / -iname "*.mp3" -exec mv {} /new_dir/mp3 \;
# -exec mv {} /mnt/mp3 \;
# Execute mv command.
# The string '{}' is replaced by the file name.
import re
import os
import locale
def inspect_file(file_name):
# Do stuff
return
def process_all(files_list):
db.quotes.ensureIndex( { text_or_whatever : "text" }, { default_language : "german" } )
function getQuery (searchString, callback) {
mongo.MongoClient.connect(
uri,
function(err, db) {
if(err) throw err;
query = { "$text": { "$search": searchString.toString() } };
var cursor = db.collection("quotes").find(
@gartenfeld
gartenfeld / restore.sh
Created January 30, 2015 05:48
mongorestore
mongorestore --host mongodb1.example.net --port 37017 --username user --password pass /opt/backup/mongodump-2011-10-24
@gartenfeld
gartenfeld / max_comb.js
Created February 16, 2015 01:34
Binary combination adding to max.
function ArrayAdditionI(arr) {
// code goes here
var max = Math.max.apply( Math, arr ),
idx = arr.indexOf(max);
if (idx>-1) { arr.splice(idx, 1) }
return arr
}
@gartenfeld
gartenfeld / output.txt
Last active August 29, 2015 14:15
Recursive combinatorics.
State of binary switches: 0,0,0,0,0,0
Corresponding combination:
State of binary switches: 0,0,0,0,0,1
Corresponding combination: 100
State of binary switches: 0,0,0,0,1,0
Corresponding combination: 5
State of binary switches: 0,0,0,0,1,1
Corresponding combination: 5,100
State of binary switches: 0,0,0,1,0,0
Corresponding combination: 4
@gartenfeld
gartenfeld / mode.js
Created February 18, 2015 03:50
Finding a mode in an array of numbers.
var array = [1, 3, 6, 6, 6, 6, 7, 7, 12, 12, 17],
c = {}, // counters
s = []; // sortable array
for (var i=0; i<array.length; i++) {
c[array[i]] = c[array[i]] || 0; // initialize
c[array[i]]++;
} // count occurrences
for (var key in c) {
@gartenfeld
gartenfeld / min_change.js
Last active August 29, 2015 14:15
Iterative solution for the minimal change problem.
var solutions = {},
coins = [1,5,7,9,11];
var process = function (t) {
if (t == 0) {
// solved
return 0;
} else if (t < 0) {
// wrong path; return really large number
return 2e10;
@gartenfeld
gartenfeld / fizzbuzz.js
Last active August 29, 2015 14:22
A less readable rendition of FizzBuzz.
var f = function (n) {
var s = "";
// 0 is falsy, the ternary then evaluates to the value right of the colon
s += n % 3 ? "" : "Fizz";
s += n % 5 ? "" : "Buzz";
// empty string is also falsy
return s ? s : n;
};
console.log(f(7));