Skip to content

Instantly share code, notes, and snippets.

@ahirschberg
ahirschberg / node_permutations_python.py
Created March 29, 2015 02:06
Sam's all permutations of a list in python
# Sam's python implementation
def permutations(listt): # apparently 'list' is a reserved keyword in python (or at least is a different color in the editor)
if len(listt) == 1:
yield listt
return
else:
for i, o in enumerate(listt):
listt_copy = listt.copy()
del listt_copy[i]
for perm in permutations(listt_copy):
@ahirschberg
ahirschberg / node_permutations_ruby.rb
Last active August 29, 2015 14:17
all permutations of a list in ruby
def permutations(list)
return [list] if list.size == 1
result = []
list.each do |locked|
list_copy = list.dup
list_copy.delete locked
permutations(list_copy).each do |permutation|
result << permutation.insert(0, locked)
end
@ahirschberg
ahirschberg / node_permutations.java
Last active August 29, 2015 14:17
Recursive algorithm to generate all possible orderings of a list of elements
private static List<List<TreeNode>> permutations(List<TreeNode> a) { // a is a list containing the child elements of a node
if (a.size() == 1) { // base case of recursion, there is only one possible permutation for a one element list
List<List<TreeNode>> res = new LinkedList<List<TreeNode>>();
res.add(a); // add a into the list of permutations (it is the only element)
return res;
} else {
List<List<TreeNode>> result = new LinkedList<List<TreeNode>>(); // we are returning a list of all the possible orderings of nodes, hence the List<List<>>
for (int i = 0; i < a.size(); i++) {
TreeNode locked = a.get(i); // remove element to be added back in later, 'locking' it in place
List<TreeNode> copiedList = new LinkedList<TreeNode>(a);
@ahirschberg
ahirschberg / yield_demo.rb
Created March 26, 2015 21:57
yield statements in ruby
def foo
yield 1
yield 2
puts "* This code is inside #foo"
yield 3
puts "* This code is also inside #foo"
end
foo # Exception: no block given (yield) (LocalJumpError)
# from yield.rb:9
@ahirschberg
ahirschberg / librifox_ui_state.js
Created March 17, 2015 18:35
First attempt at storing UI state for librifox in a dedicated object
function UIState(args) {
this.currentBook = args.currentBook;
this.currentChapter = args.currentChapter;
this.bookCache = args.bookCache; // did not hard-code the coupling with our global bookCache
this.setCurrentBookById = function(id) {
this.currentBook = this.bookCache[id];
return this.currentBook; // should setter methods be returning values?
}
this.setCurrentChapterByIndex = function(index) {
/// This is an excerpt from the app's search result generation code
/// entry is a book's json data
var book = new Book({'json': entry}); // -> Book object with keys: id, title, description, and json
bookCache[book.id] = book; // add book to cache with its id as a key
// Book id is in the link href: for example href="chapters.html?9396"
bookListItem = $('<li><a href="chapters.html?'+ book.id + '"><h2>' + book.title + '</h2><p>' + book.description + '</p></a></li>');
@ahirschberg
ahirschberg / redundant_librifox.js
Last active August 29, 2015 14:17
some code in librifox that I refactored
// Initial code:
var xml = xhr.response;
var xmlDoc = $.parseXML( xml ); // xml variable is never referenced after this line
var newXML = $( xmlDoc ); // redundant form of above line, does nothing
var title = newXML.find( "title" ); // misleading, should be plural 'titles' (#find returns all title tags in the document)
var enclosure = newXML.find("enclosure"); // never used
var currTitle; // unnecessary
var currEnclosure; // unnecessary, never used
// Refactored into:
pre.code {
border: 1px solid #919191;
color: rgb(153, 58, 23);
padding: 2px;
padding-left: 10px;
border-radius: 2px;
}
// TODO: needs more jQuery
$( $(1)[0] + $(1 + 1)[0] + $(100).length ).first()[0] // -> 4
var $enclosure_search = $(xml).find("enclosure")
// different values for similar calls
$enclosure_search // -> Object { 0: <enclosure>, 1: <enclosure>, length: 2, prevObject: Object, context: undefined, selector: "enclosure" }
$enclosure_search[0] // -> <enclosure url="a">
$($enclosure_search[0]) // -> Object { 0: <enclosure>, context: <enclosure>, length: 1 }
// Get the first enclosure tag explicitly - #attr() can no longer be called
$enclosure_search[0].attr('url') // -> TypeError: $enclosure_search[0].attr is not a function