4 ms·
I've a few years of experience tutoring programming. From what often see and from reading your description, it sounds like your problem is "need better planning
by blacksoil 8y ago
I've a few years of experience tutoring programming. From what often see and from reading your description, it sounds like your problem is "need better planning before jumping to writing a code".
Programming is akin to building a house. Once the blueprint is figured out, the actual act of building (putting bricks together, etc) is actually pretty straightforward. The key though, is good the planning.
By good planning, in your example of returning the median, you should solve the problem before touching your computer. Use either pencil-and-paper or whiteboard method as much as possible. For example, I'd plan it as follow:
1. Solve it in a high-level manner, as if you're trying to teach someone who never codes a program before
2. Write down all the function definitions you need (don't code the body, just know what each does)
3. Write down how all the functions are to interact (again, don't write the body of any of the functions yet, just assume you have them)
4. Write down pseudocode for each of the functions
5. Code
I've seen lots of beginner programmers tried to skip over to 5 without concrete plan only ended up confusing himself over trivial problem. No matter how tempting it is, jumping straight to 5 without concrete plan is only going to confuse you.
I forgot whether it was Leslie Lamport or Edsger Dijkstra, but one of them said that a good programmer should have the skill to fully plan out a computer program without coding it.
- Tmp1234 8y agoI actually do go through the 5 steps you mentioned, but the problem is I don't see parts of the algorithm I'm missing until test cases fail or until I'm actually translating to code. The need to make what I'm thinking precise and understandable to the computer or the oversight of edge cases or, sometimes, an entire set of cases I didn't have in mind originally when coming up with the high level solution, evades me until I hit a wall. I don't know how to know what I don't know- I don't know how to see what I'm overlooking when it sounds like a solid and complete algorithm on a high level. Then there are times when I simply don't know where to start and nothing comes to me. I think your outline is good, but I'm not sure what to do with it if the ideas and logic simply don't come to my head.
- blacksoil 8y ago>>The need to make what I'm thinking precise and understandable to the computer or the oversight of edge cases or, sometimes, an entire set of cases I didn't have in mind originally when coming up with the high level solution To me this sounds like you should spend more time in the planning phase. Sounds like your plan needs to be more detailed and concrete. If you hadn't yet come up with a high-level solution, jumping straight to code wouldn't help either, it'll only make it more confusing. >>but I'm not sure what to do with it if the ideas and logic simply don't come to my head. What sort of ideas and logic are you specifically having problem with? If they're about algorithms, try reading more algorithm books (Thomas Cormen's Introduction to Algorithm is really good), do more practice programming (I used to practice a lot here https://practiceit.cs.washington.edu/ https://practiceit.cs.washington.edu/). In general I'd suggest try to see more problems where the solutions are available, but don't immediately see the solutions. Try to do it yourself first.
- Tmp1234 8y agoThe thing is I have no idea I need more planning. When I start to code the solution I've come up with seems genuinely complete and correct. It's only when the incorrectness hits me right in the face, usually in the form of test cases failing or not being able to code something I thought I could, that I realize I didn't have it. To be concrete, yesterday I tried to solve this question: https://leetcode.com/problems/wildcard-matching/ https://leetcode.com/problems/wildcard-matching/ I spent 45 minutes on the planning phase which would be the entire time I'd have for the problem in an interview. I had some pseudocode, thought through it, tried an example or two and then decided to code it up. As I did I realized a few more examples which I added in. Finally, I had some code that seemed even more correct than the code I thought was already correct. I run it and test cases fail. I spent 2.5 hours in total trying to get them all to pass without realizing the central issue of the question is what to do when you hit a * character. The necessity to try all the combinations of the string after a * doesn't not occur to me in 2.5 hours; I kept tweaking for specific cases I was failing. I ask a friend about the problem and in less than two minutes he realizes you need to try all combinations. >>What sort of ideas and logic are you specifically having problem with? It seems everything. Sure, some problems turn out to have an algorithm that I didn't know about it. Like the other day I was trying to do a problem that basically needed topological sort and I never really learned top sort. But then I read in the solution in CTCI that it's reasonable to derive that on one's own in a 45 minute interview so there goes that. More generally, it just seems when a problem reaches a certain level of difficulty my brain simply can't solve it. I suspect it's an intelligence thing. I just don't have a high enough IQ to get passed a certain point. I will look at the resources you mentioned.