Pessimism Porn

Pessimist political blogs. You’ve probably seen their articles posted and shared on Facebook, Twitter, or whatever social network you’re using. The theme is always the same: it’s about how the Philippines suck and there’s nothing you can do about it.

On the surface, there’s nothing bad about being critical the various aspects of your country’s society and culture. Even I like to be a devil’s advocate from time to time.

But the problem lies when you keep on talking about the same pessimistic shit over and over again. Let me give you an example:

When I entered UP, I was enamored by the protesters around the campus, the ones I only used to see on TV. They opened my eyes to the ills of society, and when I see them protesting, I’m like

Wow, these guys are great! The country needs more of them!

By my graduating year, whenever the protesters pass by the halls chanting whatever they’re protesting against, that sentiment became:

Will you guys just shut up?!? Wouldn’t it be better if you go back to your classes to study so when you graduate you have a better chance of changing the system from within?!?

And that’s the thing we’re seeing here, no one likes a pessimist who does nothing but whine.

The more you think about how these pessimist blogs are written, the more you realize what’s wrong about their approach.

First off, they’re not “realists”. They’re pessimists. Period. Realists would know that what they’re doing is counterproductive to his goals in the long run as it desensitizes and promotes apathy as I experienced with the protesters in UP.

Secondly, anyone who has seriously studied revolutions will tell you that what they’re doing will not result in a drastic change of the status quo. When all is said and done, all they’re doing is armchair pseudo-intellectualism, hiding behind shallow “rationalism” while ignoring the realities of society and human psychology (e.g. Learned Helplessness, Angry Monkeys).

If you want to change the status quo, you’ll need to get your hands dirty with concrete action. Writing pessimistic political blog posts (or worse, reading and sharing them, giving these people page views) will not change anything.

In short, all of these articles should be considered pessimism porn – pieces of work created to give smug satisfaction to pseudo-intellectuals who, instead of working towards fixing those problems, are content with “masturbating” to it.

Keep this in mind whenever you see another anti-Filipino rant from not-so-friendly neighborhood conspiracy nutjob.

How to Fix “Your Conference Presentation”

A few days ago, a PHD Comics cartoon appeared on the Startup PH Facebook group:

your conference presentation

While it’s not there anymore (possibly because it deals with academic conferences and not tech/startup conferences and therefore not relevant to the group), I had a voice at the back of my head nagging me to write a blog post on how the strip makes it look like speaking in conferences is a lot harder than it is.

So here’s a blog post on how to prepare yourself for the worst things that can happen when participate in a public speaking engagement.

Previous speaker runs late and eats into your time.

Not really a problem, as almost all of the events I’ve been in perform the laptop setup during the Q&A of the previous speaker. So even if the speaker runs late, it’s not going to eat into your allotted time.

Technical difficulties connecting your laptop.

In my experience, the biggest cause of technical difficulties in laptop setup are Macs. “They just work” my ass.

First off, they need special adapters for video. Your brand new MacBook Air is cool and all, but you’re pretty much screwed if you forgot to bring your mini-DP to VGA adapter.

Second is that non-frequent speakers don’t know how to change the display settings of their Mac to provide the optimal presentation experience. Unless you’re presenting in a Mac heavy venue, don’t expect the technical staff to help you with that.

I’m not saying that if you are using a laptop with a VGA connector, you’re out of the woods already. You still need to know how to change your monitor settings. You can even set your laptop display to 1024 x 768 even before connecting it to the projector to reduce the possibility of getting just a blank screen.

There’s also a plan B for all laptops: Export your presentation to PDF and/or upload it to an online presentation site (eg. SpeakerDeck, Slideshare, or Prezi if you’re feeling fancy). This way, even if your machine fails to connect to the projector, you can borrow another laptop and still be able to have your slides.

“But what about my presentation animations?” you may ask.

Screw animations. Go read Presentation Zen and educate yourself on why you don’t need them.

Forget introducing yourself.

Let me tell you a secret that you’ve probably forgotten once you got that invite to speak on stage:

Nobody gives a shit who you are.

Giving a long introduction about yourself at the beginning of the talk is stupid. For one, if it’s a big conference, you’ll probably have your bio on the programme. On the other hand, if it’s a small conference, you’ll probably be forgotten by the audience by the end of the day especially if you give a boring talk.

And if for some reason you’re a really big name in the field, why introduce yourself to people who already know all about you?

So don’t waste your time talking about who you are. Make a memorable talk and put your website/facebook/twitter/etc on the last slide and people will look you up themselves.

Spend waay too much time describing the outline.
Realize you have 3 minutes left.
Power through the rest of your 30 slides.

Let’s ignore for a moment that tech/startup conference talks don’t have motivation, methodologies and shit.

Anyway, timing problems can be easily be solved by having 2 things: practice and a timer.

Practice is obvious, but too often I see newbie speakers just wing it in front. When I do my talks, I immediately see in the first few practice runs what parts can eat up a lot of time and what parts allow me some time to ad lib. And the shorter and more time-constrained my talks are, the more practice runs I do – this way, I could consider all possibilities and think of backup plans far in advance of the talk.

The timer can be anything: the timer in your presentation software, a pocket watch, or a timer in your handheld clicker/presenter. Combined with practice, you will eventually know what time to expect to reach certain parts of your talk and all you need to do is to adjust accordingly.

Motiva – Annoying audience member interrupts with self-aggrandizing question – tion

You are the speaker. The audience has no control over you. In fact, you are in control of them.

If an annoying audience member (as opposed to good audience members that facilitate interaction and discussion) constantly interrupts your talk, it is your responsibility to put them in their place. It’s up to you whether you’ll humiliate them to shut them up or just tell them to talk to you directly after your presentation – point is, you don’t let them waste your time.

Awkward silence Q&A

Just expect this to happen. Out of the dozens of talks I’ve done these past few years, only about 1 in 10 had engaging Q&As, while only 1 in 3 even had questions for me.

Now that wasn’t so hard, was it?

Programming Joke

A wife asks her husband, a programmer, “Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?”

A short time later the husband comes back with 6 cartons of milk and his wife asks, “Why did you buy 6 cartons of milk?”

He replies, “They had eggs.”

I find this to be the most unfunny programming joke in the dozens of programming jokes I’ve heard since I started programming. It’s even worse than the “Why is Halloween the same as Christmas?” joke – at least that one’s a cute coincidence for number systems.

At first glance, it looks like a straightforward joke: a direct translation of the statement make the husband buy six cartons of milk:

milk_to_be_bought = 1
if they_have_eggs
  milk_to_be_bought = 6
end
buy_cartons_of_milk(milk_to_be_bought)

But that’s not even a direct translation! A direct translation would be:

buy_one_carton_of_milk
if they_have_eggs
  buy_six_cartons_of_milk
end

This results in the husband buying 7 cartons of milk!

But no, the conversion from command to program is still incorrect. Here’s a much more direct translation:

buy_one_carton_of_milk
if they_have_eggs
  get_six
end

get_six what?

That’s the thing: no self respecting programmer would do what the husband did in the joke. A joke computer scientist would not do anything until the linguistic ambiguity is resolved, while a joke software engineer would also refuse to do anything until the unclear requirements are clarified, documented, and signed off.

Now if the joke went like “A wife asks her husband, who just started Codecademy last night…“, it would probably work.

Unofficial Solutions to DevCon C-Cup 2012 Problems

Two weekends ago, DevCon held its Code Challenge Cup (C-Cup) at Xavier School Greenhills.

Now while I am the current VP for Technology of the said group and spent 6 hours painstakingly checking a good chunk of the submissions, I wasn’t involved in the making of the problems (I was the tech support guy, not a “judge”). I didn’t even look at the problems until it was linked to on the event page days after the event.

Anyway, being a sucker for computer science problems I’ve decided to solve all of them on my own. And given that nobody used Ruby at the event, I going to use Ruby to solve them.

Note that while I’ve confirmed that the solutions below pass the judges’ test cases, they are not (yet) endorsed by any of the judges in the event.

Oh, and yeah, to back up my claim that Quiwa’s Data Structures will allow you to solve most of the problems in programming competitions, I’ve included references to the said book in the solutions below.

Problem A: Best-Fit Lines

Problem Type: Statistics

My solution:

While this problem can be solved with some difficulty using brute-force, going through all possibilities of a and b in 0.001 steps, the formula for this problem (simple linear regression) is pretty straightforward.

A few things to note about the code above:

  • I use String#strip() in all of the problems just to be sure. I don’t really need to do it.
  • Truncate is preferred over rounding (even though both will get full points) but Ruby doesn’t have a built-in truncate function. And so I had to make one.
  • Unlike Python, Ruby doesn’t have a built-in sum() function. Using a full reduce() works fine but I don’t like the syntax so I chose a mapreduce form instead.

Problem B: Weird Writing

Problem Type: Coding

My solution:

This is what I’d call a “coding problem”. There are no Computer Science or Math related tricks here; the problem can be solved by anyone who knows the basics of programming e.g. basic data structures, string manipulation, and sorting.

The lines 20 – 26 above can be confusing for non-Ruby or non-Perl programmers due to the amount of small things done per line. Let’s translate it one by one:

Line 20 creates a translation table/hash which we’ll use to translate the words.

  • alphabet.split(//) – converts the string to a character array, as Ruby does not have this built in
  • .zip('A'..'Z') – “zips” the array with the real alphabet creating an array like [["Q", "A"], ["W", "B"]...]
  • Hash[*...flatten] – flattens the array so we could turn it into a Hash e.g. { "Q" => "A", "W" => "B", ...}

Line 21 – 24 creates a new array containing word pairs consisting of the input words and their equivalent words in the Ripley alphabet.

  • word_pairs = words.map do |word| – maps the entire array to a new array based on the return value of the block
  • word.split(//) – again, converts the string to a character array
  • .map { |c| translation_table[c] }.join – maps each character to the Ripley alphabet using the hash we made earlier. The join turns the array back to a string
  • [word, ...] – return value of the block is the word pair array e.g. ["SCOTT", "RVIEE"]

After line 24 you now have an array like [["SCOTT", "RVIEE"], ["WEAVER", "BCSWCD"]]. We then sort this list by the second element of the pair (i.e. .sort_by { |pair| pair[1] }) and put each word on the output file (i.e. .each { |pair| output.puts pair[0] })

Problem C: Congressional Progression

Problem Type: Graph Theory

My solution:

This is the first problem whose solution you could find in Data Structures. A major district in the problem is a vertex, once removed, produces a graph where there is no path between one or more pairs of vertices. This can easily be determined once you have a transitive closure for the graph, which we will construct using Warshall’s algorithm.

The code above a bit straightforward:

  • Line 24 simply removes all of the edges that contain the currently tested major district / vertex
  • Lines 25 – 28 looks fancy, but all it does is create a translation table for the name of the district with their respective positions e.g. { "Borgia" => 0, "Camelville" => 1 }
  • Lines 33 – 45 is a direct translation of the Warshall’s Algorithm from the book to Ruby
  • Line 46 determines if the district is major by checking for the existence of falses in the adjacency matrix.

Data Structures reference: page 331, Warshall’s Algorithm

Problem D: Islands

Problem Type: Graph Theory

My solution:

This problem can be solved in many ways, with the simplest being graph traversal algorithms. And the simplest of those is depth-first search which is used by the code above.

To summarize what the code above does:

  • Look for an island tip closest to the top.
  • Increment the island count.
  • Use DFS to delete the island piece by piece.
  • Repeat until there are no islands left.

Data Structures reference: page 253, Depth-First Search

Problem E: Basketball

Problem Type: Coding

My solution:

Another coding problem. Expect to fail many technical interviews if you can’t program this in less than 30 minutes.

The existence of Array#count() makes this problem too easy in Ruby.

Problem F: Utility Maximization

Problem Type: Mathematical Optimization / Microeconomics

My solution:

Like Problem A, this problem can be solved either through brute-force or having knowledge of microeconomics or mathematical optimization.

And it can be solved using blind luck.

To explain why it can be solved through luck, we must first look at a “real” solution using Lagrange multipliers:

Not fun, right?

However, if you set the function and constraint to the ones given in the problem and take time to solve the partial differential equations, you’ll end up with something like:

If you were lucky, you could’ve seen this pattern in the examples:

In short, the price x quantity of each good is proportional to their substitutability scale. From there, it should not be hard to derive:

i.e. line 18 above.

Problem G: Evolution of Industries

Problem Type: Graph Theory

My solution:

We need to find the path between two points in the graph that produces the highest probability. Calculating probability of independent events is simple (just multiply them all together) while finding the path is only a matter of choosing which pathfinding algorithm to choose. For the code above, I chose the most common algorithm: Dijkstra’s Algorithm.

Line 24 onwards is a direct implementation of the algorithm from the book.

Data Structures reference: page 312, Dijkstra’s Algorithm

Problem H: Relatively Prime

Problem Type: Number Theory

My solution:

The easiest problem of the set. All it takes is for you to know how to calculate the GCF of two numbers efficiently. If you’re not using Ruby or Python (which has built in GCD functions), you can code the Euclidian Algorithm in less than a minute.

Data Structures reference: page 5, Euclid’s Algorithm

Problem I: Jugs

Problem Type: Graph Theory

My solution:

This classic puzzle isn’t that hard when you use graph-traversal algorithms on it. Since we’ve already used DFS and Dijkstra’s algorithm above, I chose to use Breadth-First Search this time around. (Note that DFS is fine because there’s no “shortest path” requirement.)

Here’s how BFS can be used to solve the first example, 3 5 4:

We start with our first node, representing 0 gallons for jug A and 0 gallons for jug B.

We can only do two things, fill A and fill B. All other moves returns you to the same node.

On node (3,0), we can fill B and pour A B. Again, all else returns you to a previously visited node.

On node (0,5), we can only fill A and pour B A. Note that (3,5) has already been created in the previous step.

Node (3,5) is a dead end.

On node (0,3), we can fill A.

On node (3,2), we can empty A.

On node (3,3), we can pour A B.

On node (0,2), we can pour B A.

On node (1,5), we can empty B.

On node (2,0), we can fill B.

On node (1,0), we can pour A B.

On node (2,5), we can pour B A leaving 4 gallons on jug B.

Backtracking, we now have our solution.

The whole thing, animated:

My BFS implementation is kinda weird in that it doesn’t use a real queue (it uses a simple array) and we store the back edges along with the action in a hash of a hash.

The solution above also finds the solution for putting N gallons in jug B. According to the spec, you only need to put the water in either jug. We’ll just leave modifying the solution above to do this as your assignment.

Data Structures reference: page 265, Breadth-First Search

Problem J: Just the Facts

Problem Type: Coding

My solution:

Last and probably one of the least, we have this coding problem.

Your main problem here is how to calculate factorials – using normal integers, you’ll get to the integer limit very quickly. There are many clever ways of calculating the last non-zero digit of a factorial, but there’s really no need to do that. Using arbitrary-precision arithmetic (e.g. Java’s BigInteger) you can calculate 10000! in all of the given languages in less than a second using less than 1MB of memory.

Here’s what we did in the Ruby code above:

  • (1..n).reduce(:*) – calculate factorial using reduce(). Ruby automatically converts integers to Bignum when they reach a certain size.
  • .to_s.gsub("0", "") – convert factorial to string and replace all 0’s with blanks
  • [-1] – get the last character

And that’s that. To summarize, we got

  • 3 problems that can be solved with minimal CS instruction
  • 5 that can be solved using Quiwa’s Data Structures
  • 2 niche statistics and economics problems (A and F)

In other words, it’s a pretty good problem set for programmers of all levels. I might even go as to say that, given enough time, a second-year Computer Science student should be able to answer 8 of them armed only with a laptop, their textbooks, and a complete programming language API.

How to Vote in the Global Startup Battle (because the site sucks)

From our testing and based on the large discrepancy between the number of likes and the number of votes on the entries, we’ve concluded that the Global Startup Battle’s voting site’s usability sucks.

The correct way to vote would be to like the Startup Weekend page, then add Offerpop as an app, then wait for the entry to fully load before clicking the small orange Vote button.

Go vote now! http://bit.ly/mystartupacademy