![]() |
Problem solving
I've been participating in TopCoder.com's online algorithm competitions. Actually, I've only been doing practice problems for the past two weeks. I'm really addicted to solving these types of problems. You guys should give it a try!
Anyway, after I solved this one problem, I looked at how other's did their and made me realize, I approach problems awkward. >_> First, I try to understand the problem. Second, I look at the examples and see how the program should run. Then, I try solving the problem which data structure would be best or something along those lines. For example, here's one the problem I did today when i got home Quote:
Here's my solution to the problem above :
public class RussianSpeedLimits |
Re: Problem solving
Your approach and answer look perfect to me.
> First, I try to understand the problem. Yes, because solving a different problem won't get you your marks now, and won't get you paid later. > Second, I look at the examples and see how the program should run. Also good. It might produce the correct answer, but if all the detail of the program I/O is wrong, then it's still a failure. > Then, I try solving the problem which data structure would be best or something along those lines. Also good. The choice of good algorithms + data structures vs. bad ones can turn mere seconds on run-time into days or weeks. > how I could have solved it better? What would you consider "better"? Less lines of code. Doesn't work for me, since I value readability above pretty much everything else. Being a good coder is not about writing the shortest and most cryptic bit of code possible. Commercially, your code might last for 20 years or more, and be read by hundreds of different programmers in that time. Communicating clearly with all of them is paramount. |
Re: Problem solving
First, I LOLed when I saw the copyright notice you posted. :P
Then I decided this would be kinda fun. I haven't tested my solution, but I'll put it up anyways (I'm off to bed so maybe I'll test it tomorrow if I remember; by the way, did they provide a test file/framework or did you just do a bunch by hand?) :
using System;If it doesn't work, my lame excuse is that I spent about 5 minutes thinking and typing combined, and it's 2 in the morning. [edit:] Just noticed the part about the class name and signature, but I was too lazy to name mine :P |
Re: Problem solving
@Salem: By better, I actually mean more efficient. Regardless what language I progam in, I still want the most efficient solution for any problem I approach.
@Jimbo: Your solution looks good! A lot better than mine! Thanks for the replies, guys! |
Re: Problem solving
Quote:
Quote:
|
Re: Problem solving
> By better, I actually mean more efficient.
The reduced argument is why are you writing in anything other than assembler then? Mostly the answer is you don't, because it would take too long, and the result is in no way portable. Read this before going all gung-ho over trying to squeeze every last uS out of your code. > I still want the most efficient solution for any problem I approach. Unless you have an infinite amount of time, you're not going to find it. There'll always be someone else who can get that little bit extra. Other efficiency measures: - time it takes to write, 20 minutes (C) or a month (asm) - time it takes to test and debug, similar extremes. Your clever asm code may be very fast, use lots of obscure technique, but if you make a mistake, you're hosed. - time it takes to explain it to the maintenance guy, similar extremes. You've got to run the code a hell of a lot to make several months of effort (compared to a couple of hours) pay off. As soon as you start programming against any kind of a deadline (like professionally), then you'll realise than 99% of the program is nowhere near the program critical performance path, so busting a gut to make it microseconds quicker is a waste of effort. Further, predicting where the performance bottleneck is is a black art, never mind the problems of figuring out the true cause. For example, if your profile shows say strcpy() being called a lot, the answer isn't "rewrite strcpy", it's investigate why it's being called so many times in the first place. The fastest code of all is the code which isn't called. Single function quiz assignments on the other hand are much easier to write, and much easier to profile. With some skill, and plenty of time, you can usually make a pretty "optimal" job, in terms of run-time performance. > I actually think that things like Top Coder can be a bad influence Probably because something like "performance" is dead easy to measure. All you need is a clock to time it, and 'diff' to compare the outputs. Measuring "code quality" is an altogether different prospect, one which machines are currently useless at. Nor are they capable of measuring with any accuracy the amount of effort spent on the program. |
Re: Problem solving
Some of your stuff is redundant, for example, for insideCity, just return num mod 2 equals 0, no need for ternary operators.
My attempt that I haven't tested: :
public int getCurrentLimit(String[] signs) { |
Re: Problem solving
I respect and agree with your responses. Although, my question and comments was towards this kind of programming problems and not high budget projects were code quality matters more. I'm aware of code quality due to the fact I debug my code approximately half of the day at work.
Quote:
@null_ptr0: lol when I was going through Jimbo's code and comparing it with mine, I realize the same thing! :$ |
| All times are GMT -5. The time now is 3:49 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC