Day 11, N queens and bit shifting

Today is crazy and most people agreed that this is one of the hardest sprint so far. Marcus made a deliberate point that the N queens sprint tends to divide people into two groups. As a matter of fact, I can provide anecdotes. At least one person said “this is one of the most awesome sprints” and at least one person said the other way.

Zak and I were average in this sprint. We made solid progresses and finished all basic requirements plus some extra credits. The road map of optimization is : using all the helper functions provided -> using HashTables -> using bit shifting. We got the HashTable version up and  running, and I was disappointed that the speed boost wasn’t as big as I expected. Zak spent tons of time on bit shifting but we didn’t have to time to implement it.

Anyway, an exhausting two days. The most difficult concept for me is to recursively traverse all possible arrangements on the board. Recursive traversing is such a hard concept in general that I still need sometime to really get it.

Now I know at least two solutions to traverse. One is to toggle on the location before going to the next row and toggle off  when finished. The other is to pass two parameters, namely a prefix and a bag. I got the toggle version working. I got the prefix and bag solution to work on traversing a tony matrix, so the solution is close.

Other lessons:

– There are 3 ways to solve the 1to5 to 1to7 problem. First one is the one I used, throwing dice seven times; second one is using a 5 by 5 matrix, basically only throwing dice twice; third one is using bit shifting. Not 100% sure about the other two ways.

– Also learned about “grep – R ‘searchterm’ * “; ENV_VAR = 1, then echo $ENV_VAR. I keep forgetting about the $prefix…


Day 10, N queens

Today, we started working on N queens problem. I don’t even know the rules of rooks or queens, so when I was asked to answer a question from Marcus, I totally bluffed.

Our toy problem is an interesting one. In short, you are supposed to use Math.random to write a random1to5 function that returns a random integer of 1 to 5. Then, you are supposed to use random1to5 to write a random1to7 function that returns a random integer of 1 to 7. My initial thought was fairly naive because I forgot about the integer requirement. I scaled the number space between 1 to 5 to the number space between 1 to 7. That was easy, I thought. When I started writing the test, I realized I answered the wrong question. The real question is similar to: given a dice, how to generate a random integer between 1 and 7. My short answer is that you just need to throw the dice 7 times.

We also learned about jQuery. It is ridiculous that I have been using jQuery without understanding the difference between the jQuery collection object and the DOM objects.

What is jQuery?

The short answer is jQuery is a function that produces jQuery collection object.

What is a jQuery collection object?

– It is an array-like container.

– It contains zero or more DOM nodes.

– It also contains tons of jQuery methods, which are functions that attach to the jQuery collection object and allows easy interaction with the wrapped DOM nodes.

– $node[index] will return a DOM node WITHOUT any jQuery methods.

Three ways to generate a jQuery object:

1. jQuery(‘<p>dd</p>’);

2. jQuery(‘.name’);


4. Note: jQuery collections don’t update dynamically. (I miss you, Meteor)

What can jQuery methods do?

– CRUD + more. Create; read such as $node.parent(), $node.each(callback); update such as $node.animate(), $node.on(‘click’, callback), $node.appendTo(target); delete;

Other lessons:

– Elements panel in Chrome is the actual DOM object represented in HTML. That is why they are perfectly indented.

– Any object has a splice() method will be treated by Chrome console as an array like object.