Archive for June, 2011

This is what computer science home-works should look like

We have a Spooler S, and a Processor P.

S spools jobs and P processes them.

S can spool 3 jobs per second, while P can process 1 job per second.

We can only have one S but as many Ps as possible.

Since S is three times faster than P, we can assume that for every S start, we need to start 3P. This assumption is WRONG.

If we start S and 3P at the same time, 3P will actually sit idle for a second before they start processing records.

So we need to start 3P after S has spooled the first job. While each of the P is processing the job, S can spool another 3. So every one is happy.

Since we can have as many Ps as we want, we decide to have 9Ps.

We can also assume that we should start 9P after S has completed 3 jobs. WRONG AGAIN

If we go by the above assumption, 9P will process 9 jobs while S fetches 3 jobs.

On the next second, there will be 9P but 3 jobs, so oly 3Ps will be working, 6Ps will be idle, this happens all the time after the first second.

We don’t like idle Ps. So we decided to allow S run 90 times before we start 9P, so that each the Ps have 10 jobs waiting for them before they start.

If you are following me, you know that there will be a point where the Ps will catch up with S.

Questions:

  1. Determine for 20Million jobs if the Ps will ever catch up with S
  2. If the answer above is YES, then determine amount of jobs each P needs to be waiting for them before they start so that they can never catch up with s.
  3. If answer above is NO, then determine how many jobs was needed for the Ps to catch up with S
  4. Write a program that takes the number of Ps and The total amount of jobs needed as input. Your program should output how many jobs S needs to spool before we start the Ps such that they will never catch up with S.

e.g If we have 18 jobs and 6P, then S needs to spool 9 jobs before we start the Ps.

| seconds | jobs | processed | total rema |
| 1 | 3 | 0 | 3 |
| 2 | 3 | 0 | 6 |
| 3 | 3 | 0 | 9 |
| 4 | 3 | 6 | 6 |
| 5 | 3 | 6 | 3 |
| 6 | 3 | 6 | 3 |

Get answer in the comments đŸ˜‰

Advertisements

Comments (1)

Open letter to Scala

This post was inspired by Make Love Not Flame Wars

Hi Scala and the other “wannabes”,

This is a quote I heard somewhere.

You can know people with convoluted self esteems by how much they try to put other people down

The truth is I hate people like that. I know you can do some things better than I can, but please tell me what you can do, not how you are better than me.

This contractor walked into my office and talked for 30 mins, for 25 out of those 30 mins, he was telling me about his competitor and how he was better than him. The remaining 5 mins he told me some bollocks about how happy he would be if he got the job. What I did? I called the competitor and gave them the job. Why? He already told me what the competitor can do.

Dear Scala, please stop telling me how Java is retarded, I know. Stop showing me how you can do X in 1 LOC in Scala, but it will take a gazillion LOC in Java, I know. The problem I have with you is that you seem to be unable to successfully present yourself to me IN YOUR OWN LIGHT, but you just must find a way to trash Java so as to make you look good.

This was the attitude your distant cousin Ruby too some years past, where is she now? I hate her, not the kind of passionate hatred I have for PHP, or the kind of “dining with a long spoon” hatred I have for Python, but the kind of hatred that made me see Ruby as incompetent in herself. In fact, I am sure I might NEVER write another LOC in Ruby.

And your other half cousin Python, always wanting to be everything. Last time he was telling me how he is also functional and object oriented at the same time. I heard him trashing C, my old faithful and he was saying he is here to replace bash and shell the first time I met him.

Look Scala, I like you, and that is why I am writing you this letter. Tell me what you can do, and leave me to Judge if you can do it better than X or Y. Look at perl, he is not trying to be anything but perl. Look at erlang, he is not competing with anyone but himself, he tells you what he can do, and leaves you to decide. I love erlang. I want to love you too, but please stop being annoying. You are better than that. Tell me about how “functional” you are. Tell me about how you up hold “Object Orientation” in your core. Tell me the beauty of building a language on the JVM, let us know why it is so elegant to combine OOP with functional programing. You see all those wonderful things people have achieved through you, like Lift and AKKA and SPEC, tell me more about them. That is the stuffs I want to hear. If I see another LOC comparison again, that is it between us.

The truth is most of the boiler plate codes you show me in your LOC comparisons are rubbish. I never ever have to write those codes myself, that is the work of my IDE, netbeans handles those well. I also hear about IntelliJ and eclipse and how wonderful they are, so quit talking out of your ass, tell me something you know, not something you heard.

That is it, I hope this letter reaches you on time.

I am not hoping to hear from you soon, but if you deem it fit to respond, then fine, you know where to get me.

Your Sincere Admirer and Learner.

Comments (8)

Programming is for boys

Programming is for boys.

This is a response to Alfred Thompson’s post

Programming is for girls.

That men are logical women are emotional is a standard fact.
MSNBC Report

Programming is all about mathematics, mathematics is logic. The reason men thrived more in coding is because of their innate logical nature. For a man, 1 + 1 is always equal 2. For a woman, well…it depends. The first mathematicians are programmers or vice versa, which makes sense to find more women in coding back then, but over time, we didn’t push them off, they left themselves.

This is the same reason there are more men in mathematics now than women.

Women have been competing with men in quite a number of fields, but not in programming. The few here are rather leaving.

The reason why boys spent more hours in the computer center and still have lower grades was because of pacman. I remembered my experience, while we (boys) spent a lot of time on pacman, the girls just come on, do their assignments and leave. Then after hours of pacman, 30mins to the close of the lab, we hurriedly put something together and we get a B instead of an A. Some geniuses among us even manage to get an A.

But pacman was good. At a point we began to think, wouldn’t it be nice to have a pacman that can shoot bombs or guns at the enemy indefinetly? oh, this pacman is becoming boring, there should be a way to build walls so that enemies can’t come close, etc etc. These kind of thoughts gave birth to our interests in computer programming. One of the reasons I decided to learn how to program was because I wanted to play nintendo gameboy on my computer (I couldn’t afford one). Of course we often don’t get to complete such projects, but they birth the interests in us. Today it is still difficult to find a woman who is very good at computer games, for boys, it is almost natural.

For me it is pacman, for some other boys, it is some other form of distractions, but the key point here is, if the boys attack their projects the same way the girls do, they definitely will also have good grades. In school, I know of guys who don’t attend classes and still do well, do very well at academics. I don’t know of such women. The funny thing is those boys who don’t attend classes might not do well like the girls at academics, but at practicals, which is what coding is all about, they do very very well. They might not be able to define a turing machine like the course manual defines it, or tell you what normalization is, but they can code well.

Boys are not planners, most of us are like that. Women are natural planners, but boys are logical. Planning will get you past R&D, logic will build the code. People who plan generally become managers, not programmers. Planning will help you write a program that conforms to specs, not generally a good program. Planning is good, but not all that there is to programming. All good programmers are inquisitive (daring) which is generally not a woman’s thing. Focus is another key, most programmers can transfer that same focus that make them excel at pacman to their code. A man could focus on the TV so much that he won’t hear the kids fighting, a woman couldn’t. This un-divided attention contributes to some of the things that make men good/better programmers than women. There are other things that make a good programmer and planning is one of them, but not planning is not ALL.

Of course there is always a midpoint, where you find a good programmer who is a woman and a good planner who is a man. But generally, I think women should be more of managers than programmers.

Comments (11)