This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// 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>'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pre.code { | |
| border: 1px solid #919191; | |
| color: rgb(153, 58, 23); | |
| padding: 2px; | |
| padding-left: 10px; | |
| border-radius: 2px; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // TODO: needs more jQuery | |
| $( $(1)[0] + $(1 + 1)[0] + $(100).length ).first()[0] // -> 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |