Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a beginner learning Java and after reading the docs I'm trying to solve some of the problems at codingbat.com. Some I managed to solve pretty quickly in a matter of seconds but some not quite. The problems themselves are easy (example http://codingbat.com/prob/p126212) but I sometimes have a hard time finding the solutions. In part because I think of the best possible solution and automatically discard the ones that might work but aren't so elegant and in part because I can't find the right algorithm. This is either due to my inexperience (I have used PHP for web development for about 2 months so I'm not a total beginner - even worked with one framework to build a website) or due to my inability to think of a good algorithm.

I normally don't look at the solutions. I sometimes get stuck even for 1 1/5 hours at simple problems and some of them I eventually solve but some I can't so I eventually look at the solution.

Is it normal/good/bad to concentrate this long if I find a problem I can't solve? I might overreact but should this be so hard for me? Is there such thing as not being able to think of algorithms even with much learning and reading?

Thank you all in advance.

share|improve this question
    
I'm not sure what's so hard about return str.replace("yak", "");. Can you point to a more difficult problem? –  Josh K Nov 23 '10 at 18:01
    
That's what I did for that one too... I checked the solution as well... and it's terrible. At the very least they should have used a StringBuilder. I guess these are for beginners, but it would be nice to show something with best practices. –  Michael Walts Nov 23 '10 at 21:46
2  
You are a complete beginner. You will realize that you have only just begun to learn after you have been programming for 10 years. :D –  DominicMcDonnell Nov 23 '10 at 21:48
    
The solution at that site may not be optimal - but it does solve the problem, whereas @Josh's answer does not. The actual problem says, "return a version where all the yak are removed, but the a can be any char" - implying that a is a meta-character, and strings such as yek would also be removed. –  Cyclops Apr 26 '11 at 17:40
    
@Cyclops: The thirty seconds I spent on that did not necessitate updating my solution. You could use a simple regular expression /y.k/ to do the replacements as well, instead of the horrid mess they have. –  Josh K Apr 26 '11 at 17:47
show 1 more comment

5 Answers

up vote 7 down vote accepted

Its actually a pretty easy situation to get into when you're coding for yourself. Its bad to spend a long time focusing on the best way to do something when you don't produce anything in the end.

However I think it's also bad to never stop and reflect. You are pretty much a beginner with only a few months dev. experience if I read your question right. At this point in time you should be focusing on writing something, anything. If you're writing for yourself. If you're not writing for yourself you'll learn to be quicker as if you don't your job will be in trouble.

My advice is

  1. Write code. Bad code / good code it doesn't matter at this stage. You just need to know you can bash something out when required.
  2. Set yourself deadlines. Try to be reasonable with them but not so loose as you may as well not have any deadlines.
  3. Don't try to alter functioning code to be better written unless.
    1. You have a performance issue you need to fix.
    2. You have enough time and you feel that your new way is an awful lot better.
    3. You feel you can learn something new by trying to implement in a different way.
  4. Pay attention to the small details, such as comments. It can really help you fix your mind on the problems and the solutions with your own code when you comment it. Try it, it allows you to step away from the solution and focus on the problem.
  5. Get other people to review your work if possible.
  6. If doing this as a hobby enjoy it. If the worrying about the problems is taking away from your enjoyment, it implies your doing something wrong.
  7. Ask people what they would do with the same problems. This may be the best site for that.

Code that functions but is written badly is a lot more useful than no code.

One last comment that may help you. The interface to your code is a lot more important than the code itself.

Finally In time you will gain speed and confidence. If you can't look back at your work in a years time and say "What was I thinking?" then you really haven't learn't enough.

share|improve this answer
1  
"Code that functions but is written badly is a lot more useful than no code." How true. –  HLGEM Nov 23 '10 at 22:07
    
"The interface to your code is a lot more important than the code itself." +1 –  Seralize Apr 11 '12 at 16:55
add comment

I don't really like solving hard problems. They are complicated and confusing and easy to screw things up. So I break them up into small problems then solve those smaller problems. It works pretty well.

share|improve this answer
add comment

The most important thing in software development is working software. This is especially so in commercial development.

A lot of people spend far too much time trying to be clever or come up with a supposed "elegant" solution when what they should be doing is producing working code.

What you should be doing is solving the problem as quickly as possible. Once you have proven your solution works, then is the time to refactor to make it elegant, fast, etc, if required. Often, once you have working code in front of you it becomes easier to see the more elegant solution.

share|improve this answer
    
This is certainly something I have been learning the hard way. I keep thinking I have to make everything Abstract right from the get-go. –  Bryan Harrington Nov 23 '10 at 20:31
add comment

Take a break every now and then. It's good that you aren't looking at the solutions right off the bat, but if you are well and truly stuck go ahead. You will still learn even if you did not find the solution yourself.

I've heard that it takes 10 years to master a skill (don't have a source for that, sorry). So, don't worry if it seems to take you a long time at first. By the way, 1.5 hours is not a long time to be figuring out a problem. If you continue in programming you will find problems that may take you days, even weeks to figure out. Keep on driving at the problems, and you will find as the months go by that it becomes easier and easier.

As far as "Is there such thing as not being able to think of algorithms even with much learning and reading?" is concerned, practice makes perfect. There are only a limited number of basic data structures, and the algorithms for working with them are the same for any implementation. Once you learn, really learn to apply a few algorithms, you will remember them and be able to use the knowledge in other problems. Over time this knowledge base builds up to the point where problems appear to all be variations on a theme rather than disconnected.

share|improve this answer
1  
"10 years to master a skill" source: norvig.com/21-days.html –  Bruce Alderman Nov 23 '10 at 21:23
    
Heck I've spent weeks trying to figure out a complex solution! –  HLGEM Nov 23 '10 at 22:06
    
+1 for the "variations on a theme rather than disconnected" observation –  Gary Rowe Nov 24 '10 at 11:49
add comment

From what you've said in your question, the issue isn't that you can't solve the problems, it's that you can't think of an elegant solution that would work, so you give up. The main problem with this approach is that you're not really learning anything.

A better approach would be to try one of the not so elegant possible solutions. If it doesn't work, it's not the end of the world, just move on to plan B. If it does work, you now have the chance to make it more elegant. Either way, you will have learned something, and this approach will serve you better if and when you write code for a living. Remember: you can't improve what's not there.

share|improve this answer
1  
@Robert: thanks for the edit. It was a paragraph fail on my part, which actually proves my point. –  Larry Coleman Nov 23 '10 at 18:11
    
That's precisely what I've learned to do. Come up with an inelegant solution to part of the problem, and keep nibbling at it. –  David Thornley Nov 23 '10 at 22:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.