Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, May 18, 2012

Code learn code learn code redux

To anyone who cares about the {Learn to code} movement and the surprise controversy it caused from some very software engineers. Here is a new post about the topic.

There has been noise about this topic. Here is a cool response: Please Don't Become Anything, Especially Not A Programmer. Of course, there are also those who side with Jeff. Like Please learn to write. Today, I received a comment saying that coding horror is actually saying a completely different thing.

I think that, regardless of whether I am able to understand Coding Horror's triggering post or not . This is a good discussion some people out there are having. And I love to at least post my opinions in this blog so I can read them and maybe some of the few guys who read this blog.

Coding, that non-essential skill

After thinking and over thinking. I think Jeff's main annoyance with this whole thing is the notion that coding is essential.

So, is coding an essential skill? Do you have to learn to code to live a good life? The answer is, to me, nope. Surely, there really are few skills and knowledge that you could call essential, and programming is not one of them... But wait, neither are reading, writing nor math... I mean, surely you need *some* writing, *some* reading and *some* math (mere arithmetic, in fact). You also need *some* basic social skills. Everything else is optional, really.

It seems that most energy spent by the strong opponents of the learn to code movement is to remind us that coding is not essential. To which I have to ask: Big deal? If a guy makes learning to code his new year resolution, is it really a big crisis for us?

The relevant question is whether the Code Year campaign is really saying this. That coding is as essential a life skill as basic reading and arithmetic. I do not think so. It seems that Code Year is simply an advertisement for Code Academy, and that they are really saying that, if you want to learn something new, that code is an option and not one that is as obscure as some engineers would like you to think. That you can learn to code this year. I also do not think anyone is being forced to learn to code. It is a completely voluntary thing to do.

Learn to write?

If writing is not an essential skill beyond a certain basic level. The question is then, why should someone who has already acquired decent writing and communication skills be compelled to comply with the request to focus on writing and ignore code? There is certainly no reason. If you have run out of essential things to learn, then you should feel happy because you can find new things to learn. If you pick coding I will not make a big deal about it. You can also improve your writing. It has always been a choice you can make. You can even choose to do everything and improve your writing while you also learn to code.

Some people really should learn programming

A slight problem with the [it is not essential!] meme is that just because something is not a essential thing it means there is nothing good for society if you learn it. There are increasingly more careers that need some knowledge of how coding works.

The knowledge of how code works shall be , to me, not different than how some judges and lawyers have to learn know about environmental science (We got some judges out there making very important decisions regarding programming. They are just an example. I also think that many authority positions in companies related to this should also get to know at least some coding.

I say this because, unlike plumbing, programming is not really something that people can intuitively know what is it about. e.g. consider Hollywood. So, really, I think this information about the basics of programming is something that is needed for certain groups with a job description different to software engineer.

Become a programmer?

At least to me it seems that the {Learn to code} people are not saying: Become a programmer. So, I don't really think Bloomberg is planning to replace any programmer in the city hall or anything. Because I cannot find evidence of Code Year movement to ever say that after you learn to code you would replace part of your job description with "being a crack at javascript". That would be silly.

Hello world

Jeff mocks the notion of Bloomberg's discovery of programming to be a hello world-like program. Well, what would be so bad about it? Perhaps more people just getting to run their codes just once will make the world slightly better? It is difficult for me to imagine a way in which more overall knowledge about something would be a bad thing. In fact, it seems to me that historically, those in the thinking that some knowledge was not appropriate for some people were the ones in the wrong.

If more people learn to code

Is it going to be the end of the world? We will have more bad coders. That is for sure. There will be many people that will hate code after having been encouraged to learn to code only to find out they do not have the patience for it. There will be some guys and women who have found a cool new hobby, and they might spend some of their free time making cool minor projects, and eventually finding out the reasons to have a good methodology. Some other people will find a new career path. Some of them will become bad programmers. But a couple of them will become great programmers. More people will actually know what programming is all about though. There will be more knowledge around. And the next time a jerk says that he invented the double click, more people will know that such claim is bogus.

I think that regardless of whether the learn to code group is based on real points or not. The net outcome will be positive. As I mentioned before, it is difficult for me to imagine a good justification to think that more people knowing about a subject is ever going to be a bad thing.

Learn: Writing, plumbing, reading, coding, math, astronomy, chemistry, biology, music. Learn everything, damnit

There, that is my conclusion. It has become my new ideal to tell people that they have to learn everything. You shall never stop learning because your final objective is to learn everything. You can, however, pick the order in which to learn your stuff. Enjoy.

Edit:

You too can earn 100k dollars a year?

This post reminds me of another thing that is being assumed about the learn to code meme. Many people seem to interpret it as "you can learn to code in one year and make millions with code.". I will be honest that I have not really seen much from this movement, the litte from the code year home page does not seem to imply that to me.

Of course, that premise is terribly wrong. Once again though, if people really fall for it, then this whole experience will help more people find out that no, 100K a year with coding is not as easy as learning to code. And that is a great thing to learn.

We may be missing something, that learning is so great that even if your starting premise is wrong, you still learn useful things. Let these beginners be wrong about their expectations.

Tuesday, May 15, 2012

Learn to code? Why not?

(mostly a transcript of a comment I just posted)

Imagine if Mike Bloomberg, major of New York wanted to learn to play the guitar. To which guitarist guild would reply: "NO! Don't learn to code! There are millions of terrible guitar players, we don't need any more!".

Then Mike Bloomberg decided to learn astrophysics. To which Neil deGrasse Tyson would take offense. "Can you explain to me how Michael Bloomberg would be better at his day to day job of leading the largest city in the USA if he woke up one knowing the total mass of Andromeda?"

So, these thoughts sound nonsensical. Yet somehow, in regards to programming, they are not instantly nonsensical. At least not for many people. As coding Horror' post Don't learn to code and many of the comments placed in there would show.

The "everyone should learn to code" movement isn't just wrong because it falsely equates coding with essential life skills like reading, writing, and math"

Programming is math.

Look, I love programming. I also believe programming is important … in the right context, for some people. But so are a lot of skills. I would no more urge everyone to learn programming than I would urge everyone to learn plumbing. That'd be ridiculous, right?

Which makes me wonder what is Jeff's problem with more people learning plumbing?

So, why not. If you want to learn plumbing why not. Worst case scenario, plumbing is not for you and you trying to learn it will make you figure that out.

Our schools teach us music, calculus, sports, chemistry and a lot of stuff that we won't necessarily use in our lives. So what? And again, What is wrong of learning for the sake of learning? That is part of what makes us human.

What Jeff is saying sounds to me like this: If more people learn to code, we will have more bad coders. Boohoo. Suddenly we are back to medieval time, and we are suddenly afraid of other people learning our precious knowledge, really? Is this much better than Pythagorean hiding the square root of 2 (Just been reading Carl Sagan lately, sorry).

Also, what is up with this?:

Please don't advocate learning to code just for the sake of learning how to code. Or worse, because of the fat paychecks. Instead, I humbly suggest that we spend our time learning how to:

  • Research voraciously, and understand how the things around us work at a basic level.
  • Communicate effectively with other human beings.

These are skills that extend far beyond mere coding and will help you in every aspect of your life.

Why not, if you want to, learn coding and also learn those things mentioned? I mean, it is not like we had to choose between the two. I see no issue with a human being learning all those things AND coding. If that is what she would like.

Rant: Just because industrial engineers exist, does not mean you shouldn't ever give carpentering a try. And in that regards, just because your job uses something as lovely and wonderful as programming as part of the million of times more ridiculous, silly and frustrating process that is making software for boring business, it does not mean that everyone else should be denied the joy of programming. There are a lot of ways amateur programming can work as an entertaining hobby that is outside of the lame thing that software development is. We got modding, the demo scene, scratch, algorithm contests, games.

More so, more programmers means not only more bad programmers, but also due to any law of proportion, more good programmers.

Monday, May 10, 2010

Google code jam qualification rounds.

I've participated in google code jam since the 2008 version. Considering how the 2009 one went and seeing the problems in 2010's qualification round, I can predict with 95% confidence that in the 2012 google code jam:
- There will be 5 advancers to on site finals.
- The world champion will win 100 dollars.
- Rounds will last for four hours.
- Problems will be 350% more about implementation than they are now.
- Problems will be 10% as interesting as they are now.
- Filtering scoreboard by country will still be impossible.


Anyway, the positive thing about this round was that I finally learned my lesson. Nope, python is not the answer when there are bignum problems. Just because the contest organizer's don't feel like you should use C++ to solve a problem does not mean you shouldn't. In fact, after some time in chat I finally saw the light. GMP !.

Instead of spending 30 minutes of the following GCJ rounds trying to remember how to use python's bizare standard i/o (which for some reason is not as simple as cin >> n or n = sys.stdin.readInt()) (I am guessing all GCJ rounds from now on will use bignums). I can just use the GMP library. Because:

- It is free software!.
- It works.
- The c++ binding makes clever use of operator overloading - It is easier to use than Java's bignums...

Anyway, so here is what I learned.

Installing GMP
In ubuntu: sudo apt-get install libgmp3-dev libgmpxx4ldbl

Using GMP in your code jam c++ template

Well, it is easy, after #include "gmpxx.h" , simply use the mpz_class just as you would use a number type... Do not forget to change your compile command to link to this new library (I added -lgmpxx -lgmp to my g++ command inside the script that runs gcj code).

After installing and preparing GMP I gave it a try and coded problem B in c++ using GMP, it was very easy. Granted, I am no fan of large non-sense names as mpz_class so I used a typedef to call it big.

What follows is the elegant resulting c++ code I have fallen in love with:

#include <iostream>
#include "gmpxx.h"
typedef mpz_class big;

using namespace std;

//=========================================================
// program:
//
int N;
big t[1000];

big gcd(big A , big B) {
while (B != 0) {
big C = B;
B = A%B;
A = C;
}
return A;
}

big solve() {
big T = t[0] - t[1];
for (int i=0; i<N; i++) {
for (int j=i+1; j<N; j++) {
T = gcd(T, t[i] - t[j] );
}
}
T = abs(T);
return (T - t[0] % T) % T;
}


inline void init(){}
//=========================================================
// I/O:
//
int main()
{
init();
int C; cin>>C;
for (int i=1;i<=C;i++)
{
cerr<<"["<<i<<" / "<<C<<"]"<<endl;
cin >> N;
for (int j=0; j<N; j++){
cin >> t[j];
}
cout<<"Case #"<<i<<": " << solve() << endl;
}
return 0;
}

Saturday, January 23, 2010

Postmortem: UVA World finals warmpup II

Contest link

BLeh , why did they have to make today's warmup the 14:00 GMT one instead of last one? I had an exam during the warmup and it seems I didn't have time to solve or even open the interesting parts of the problem set.

The exam
So, at 10:00 AM GMT-4 (exactly the same time as the warm up's start time) I was supposed to solve an exam. This was so lame.

10:40 : Exam begins, professor didn't arrived at 10:30 even though the exam's schedule was 10:00 AM - 12:00 PM...

Lame, only two questions , the first one was very easy using predicate transformer semantics (zero loops, conditional statements or anything.

Second question: Even lamer. So we have to use Hoare-Floyd logic to verify a program which has ... no post-condition. Well I got used to this during the course since it seems that verifying a program also includes making the post-condition up. Anyway... the algorithm was simple, just product using successive sums. The difficulty was that it had a do...while loop, and during the course we were only introduced to while loops. But the professor was kind enough to include the do...while loop's rule ... but THE RULE WAS WRONG. It is just the first time I get into Hoare-Floyd logic, but even I know there is no way in hell that B could be part of the post-condition of (do C while B) since we need B to be false to end the loop... So, I just figured out the correct rule by myself, included an explanation why the given rule was wrong, and a sort of proof that mine works based on the fact that do..while can be converted to a while loop by copying the loop's contents to the section before the loop...

So either I got a good 100% or 50% if somehow we were supposed to use the wrong rule to prove it or 0% knowing my luck.

11:00 : I am finished. I should probably go back home to actually try the warmup II contest... But I don't know if I should go or wait for the official end time... Always confusing.

11:40 : Ok, some people are just giving him their tests and leaving. I better do as well.

12:20 : I am at home and ready to open the problems...

Problem D
So, I look at statistics and problem D is by far the one with the most solutions. Turns out it was excessively easy. I actually double checked the statement just in case there wasn't a cheap difficulty device like having to use bignums or something. Turns out there wasn't. I just solved it. I wonder how would people manage not to have at least 1 problem in this warmup...

Problem J
Apparently the second easiest problem, I first thought that it was another easy one since I thought that if light a can trigger light b the converse is true as well. But then I noticed that it probably isn't the case. I asked for clarification to be sure. But the response to the question never came to my inbox...

So, after inspecting the examples, I finally figured out that the graph is directed, this makes things a little harder. I quickly noticed that all nodes belonging to a cycle could be treated as the same node. So we can do a SCC algorithm and then treat the transformed DAG graph for a solution. Once we can assume the graph is a DAG, things are easy. Just notice that you will have to manually turn a light on if and only if there are no lights that can trigger it. This can be read as "count all the nodes in the DAG that have in-degree equal to 0".

Coding the solution was hard since I noticed I never actually implemented a SCC algorithm before. I had just blurry memories of CLRS' lesson about it, so I went to wikipedia. It recommended tarjan's algorithm and also included pseudo-code for it. I took some time convincing myself that the pseudo-code is actually correct.

1:00 PM : The time I finished coding the problem coincided with lunch time. So I submitted it and checked the result... WA!

Lunch
I spent the whole lunch hour wondering if maybe the algorithm I conceived was wrong or not. I ended up quite convinced that it should work.

Back to problem J - Today's blunder
14:00 PM (ish)- I had no choice but to inspect my solution. I created some test cases and noticed that it was failing many of the new ones. After a lot of manual debugging I finally noticed my mistake... I had something like:

for (int i=0; i<T; i++) {
cin >> N;
for (int j=0; j<N; j++) {
adj[i].clear();
...
}


It was supposed to be adj[j].clear() ... So, some ghost edges could remain and ruin everything... I just fixed this issue and got AC.

Problem B
14:40-ish . The next easiest problem was B. I quickly noticed it was just a dp/memoization one. So, I made a recurrence, but it allowed some cycles, so you had to solve the equation inside the recurrence to avoid the cycles (so that dp/memo worked). There is also the other problem of having to get a) the prime divisors of a number and b) The number of primes between 1 and a number. These both are easy using a sieve, I modified Eratosthenes' one so that it would store a number's lowest prime divisor in its array. If no prime divisor was found for a number use this number as the minimum divisor for the multiples that don't have such number yet.

I was having problems at first with the example cases. it turns out my recurrence was wrong. At the end I finally figured out:

f(x) = ( 1 + sum( f(y) for each y such that y is prime and x divides y) ) / ( (number of good y primes ) / (number of primes between 1 and x) )

I actually finished this problem very close to the end of the contest.

Final result: 77-th place . Hmnn, I never do well in world finals warmups...

Conclusion
I think that maybe if it wasn't for the time wasted in problem J due to not knowing enough about SCC and the lame mistake I could have solved another problem or at least have more fun from the contest. I need to practice AND study more theory.

Final thoughts
Why do blogs use HTML? instead of something like BBCode or whatever wikipedia uses? Since they have to be paranoid about XSS attacks, they don't let you use many tags and it can get confused by things like C++ code in which > and < are used... With bbcode, we wouldn't have so many problems...

Saturday, December 26, 2009

Postmortem: Topcoder SRM 456


Minus 161 rating points, that cannot ever feel good. Even those guys that claim that rating does not matter would not enjoy it. This was however a fair result and a consequence of me not following my own rules. What rules? I have two rules when solving SRMs.
Rule #1: "Always make sure to have at least 35 minutes to solve the easy problem".
Rule #2: "Do not challenge! No, not even when you are 100% sure the solution is wrong. Do not challenge! If your life depended on it, kill yourself. Challenging is not worth it!"

450 points: CutSticks
At first I didn't get why they made it a 450 points problem the constraints were very large. I was not able to understand it completely well after the first attempt to read the statement, so I bothered the admins with 3 silly questions. Once I finally understood the question I first thought it needed a greedy or math solution so I was discouraged since those things are not my strength.

I eventually noticed that if a math solution was available it is way too hard for a 450 points problem. I also noticed that even if we had some sort of direct greedy solution, the simulation would still run too slow as there may be up to 1000000000 steps. That's when I noticed the output is continuous and not integral - that's the sort of thing that hints for a binary solution. There it is! We can use a binary search and then ask "For a given length X, return whether it is possible for the K-th stick to have a length larger than or equal to x".

Unfortunately, an algorithm that answers that question was still a little tricky for me to implement. At first I was not considering the max cuts constraint which made me fail a single example, and when I added it I started failing all the other examples... I think I just had too many bugs and debugging was taking a good while. That's when I noticed... 25 minutes left! Darn I missed the deadline to open the easy problem. I had 10 minutes less than planned. My only hope was that I could solve 250 fast enough.

250 points problem - SilverDistance
#|@~! Ad hoc shortest distance problem. By the time I noticed that it was one of such problems, I knew I was doomed. I tried to simplify the problem and avoid as many pitfalls as possible. As usual, these problems can be converted to ones in which you start at (0,0) (then subtract x and y from gx and gy). I tried to take a look at what options we have in two moves:



#####
.$$$.
##*##
.$.$.
#.#.#


* is the silver (0,0), $ are the cells accessible by one step and # are the cells accessible by two steps. I noticed that (-2,-2), (0,2), (2,-2), (-2,0), (2,0), (2,-2), (-2,2), (0,2) and (2,2) are all accessible with two steps, so I concluded that if both x and y are even, the solution is as easy as : 2*( x/2 + y/2 - min(x/2,y/2). And if they are not, then we must just move the silver to another square until the parities change.

Blunder: Huge mistake, I assumed that just one move is required to change the parities correctly. I did not notice that it is not possible to exclusively change the x parity without changing the y parity in one step. My solution would just try any of the 5 possible directions when the coordinates are not even (and pick the minimum distance among the ones that make the differences even) This was evident after I failed the example tests.

Only five minutes left and I had to fix the mistake. I assumed that two steps should be enough (and they are), so I changed my brute force loop to consider move costs and then manually generate the 2-step moves that exclusively change x. This was not brilliant as it turned out a little complicated to do those things manually. I only had one minute left when I finished but then I had to fix typos like forgetting to change [5] to [11] ... I swear that the match ended just at the second I finished all those typos! 5 extra seconds and I could have submitted it...

Challenge phase
For some reason, the fact I had 0 points and that my golden rule says "Do not challenge" I still managed to make an unsuccessful challenge. I was sure it would fail because I knew that tantian's solution was going to return 3 for {0,0, 2,1} and it did! - unfortunately, 3 was the right result. Somehow when solving the case manually by hand I thought the correct result was 2.

.W.
..C
*..

What happened is that when I tested in paper I thought (2,1) was at the W cell instead of the C cell...

---
The correct version of my 250 did pass system tests in the practice room. I later noticed that the worst mistake was approaching the problem that way. The real simpler solution was just to notice that the difficulty of the problem lies in the asymmetric "up" step. If it was gone the problem would be very easy, and in fact we can get rid of it! Just brute force for the number of "up" steps to take, then call a function that solves the simpler problem, and pick the minimum result for all the possible "up" counts - due to the constraints, we can try all possibilities from 0 "up"s to 2000010 (the extra 10 is just in case).

--
So, there we go, if I followed rule #2, I would have gotten a good 0.00 and my rating drop would have been lighter. If I didn't fail to follow rule #1, I would have had a positive score like 130 that would probably still lose rating, but perhaps not enough points to fall bellow 2000.

The real issue is the long time it is taking me to actually code a correct solution. In fact, the real time consumer is debugging the solution. I need to practice yet again on being able to get a solution right in the first try. With that skill I could have easily gotten decent scores in both 450 and 250. That will be my focus for practice before the next match.