As the title says, I am really really really curious about this.
I've been trying to find the cases where you can't use backtracking to solve them but I can't find them.
Share your knowledge please
If there are too many possibilities to check, backtracking search will be impractical - see e.g. http://en.wikipedia.org/wiki/Lighthill_report. There may also be cases where you cannot state the problem simply and clearly enough to recognize the correct answer when you find it.
I was talking to someone in a bar not too long ago, and the inevitable question of "What do you do?" Came up. My response "Oh, I just recently graduated; I work in company X. I'm a programmer" didn't quite satisfy her.
I've had this conversation a hundred times and people are never quite happy with the answer. They always have more questions. I think this is because, for the mostpart, people don't really know what a programmer does. The answer to this question normally gives you an insight into a person (sort of): Accountants might be analytical and good with numbers, lawyers might be good with words and debate etc. 'programmer' doesn't.
so I tried to give an answer that would give that sort of insight.
I tried to illustrate my point of "Well It's basically fancy problem solving" with an example of a classic programming problem.
The first thing that jumped into my head (for some reason) was the smallest subset problem... but I'd have to explain arrays, time complexity, etc
I thought of n-queens problem, but she'd never seen a chessboard in her life (yeah, I know)
Towers of hanoi was too hard to envision in general...
So I Was stuck and the topic changed.
Can you guys think of an example of a problem; that a complete lay(wo)man can understand, which would give someone an idea of the kind of thinking programmers have to do? (and by extension save me answering awkward questions about how I make my living)
I get asked what I do too, and basically, all I say is that You wouldn't understand much about it, which will get you one of two responses: "Try me" and shes interested ->move to next section of comment, or she will just shut down and think that you think she's not intelligent. USE WITH DISCRETION.
Now, if she says try me, you have a challenge. Obviously what you do is going to be over her head, but you know you can explain a little to her. Basically, what I try to explain is that I am a logician, kinda like a magician. When the computer needs to be told to do something, and no one else knows how to tell it, they call me up. Sometimes it's simple, other times, its likes climbing Mt. Everest with your fingers on some impossibly difficult task. Imagine the worlds largest word search, and you don't know what words you're looking for. That's what I do. I find those words. So as you could imagine, I'm quick to notice little things and remember them. ;)
That should elicit the kind of response out of any women worth your time.
Good Luck!
I am reading 'The Little Schemer' in an effort to better understand some of the core elements of programming (namely recursion) and to get more of an idea how to think like a programmer.
The book comes recommended as an entry-level book and the introduction states that all I need to know are English, numbers and counting (which I do).
I am kind of confused though as the first section and questions start off by asking "Is it true that this is an atom?"
Am I missing something? Am I supposed to know what an atom is? I am confused as I thought it was meant to be in more plain English.
Thanks in advance,
Tim
It can be a hard book to get into; it took me two tries separated by about a year. The way you to read it is that you are figuring out these concepts for yourself by listening in on a dialogue between two other people. The first question about a concept will lose you, but the hope is that you say, "Aha! I've figured out the concept they must be talking about" before the end of the questions on a given topic. By the end of the section you'll be answering the questions yourself before reading the answers in the book.
If you hit the end of a section and haven't gotten to that point, start over again but try to give the answers yourself without reading them. When you can supply the answers yourself, you've either figured out the concept in your own terms or memorized the answers in the book. Later sections will refer back to these concepts, though, and will reinforce your understanding.
Think of the student in the book as a proxy for you who seems to begin each section smarter than you, but who you outpace by the end of the section.
The book uses a sort of "constructivist" learning model. It asks you to figure things out before you know the formal definitions. The idea is to develop an intuition before formality (I believe, although that may not be the intention of the authors). You may find this annoying at first, but when you get to the higher-level concepts, you will find yourself understanding things way better than you would from reading R5RS, for example. Continuations had me completely baffled until I read all the way through this book. Stick with it and you'll get why the authors take this approach.
On the left of the page:
"Is it true that this is an atom?
atom"
On the right of the page, 2cm away:
"Yes, because atom is a string of
characters beginning with the letter
a".
And similar questions and answers about atoms in the same format for the remainder of the page. I don't think it takes a genius to work out what is going on here.
An atom in Scheme is like in english, something that you can't divide.
Here are some atoms:
'foo 'bar 'baz 123 '() '+
Can someone explain me how the text searching algorithm works? I understand its a huge field but am trying to understand it from high level so that I can look up academic papers on it.
For example, Spelling mistakes is one problem that is tough to solve and of course Google solves it. When I search for a term and misspell it on Google, it automatically suggests the correct spelling. How is indexing done for it? Using MapReduce I can see they index various entities. What do they or some one else index and store? May be I am looking for a practical implementation of MapReduce if I am thinking in the right direction at all.
Pav
I'm afraid this question really is too big, which probably explains why it has not seen an answer yet. As far as Google's spell-checker is concerned, Peter Norvig explains how it is done: How to Write a Spelling Corrector
The exact implementation in productive use at Google surely looks quite a bit different and way more complicated, but this might get you started.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
When I am stuck with a problem:
I search Google for code snippets.
I look at isolating the problem, so that I can better explain it to others in order to get answers.
What search techniques do you use to find the solution to your problem?
I started asking questions in Stack Overflow.
What other techniques or methods do you follow, to fix the problem more quickly?
Go and do something else. No, really. I've found that putting the problem away in the back of my mind helps. I can't count the number of times I thought of a great solution to something I've been working on when I was working on something else, or watching TV, or eating. It seems your brain is still working on the problem in the background.
If that fails to solve your problem, try talking to someone. You'd be surprised how often others can give solutions to your problem that are so simple you'd facepalm.
Well there's:
Google
Google
Google
Stack Overflow
Google
Google
Maybe a book if I have one.
Seriously, I started (hobby) programming in the 1980s and even into the mid 90s you had to know things and have a technical library. Then Google came along and it's easier to Google something than it is to look up (bookmarked!) API documentation (Google "java stringbuilder" will get me there faster than navigating will) let alone an actual book (electronic or paper).
Most problems you're trying to solve have been solved before. Many times.
The rest of debugging comes down to decomposition, possibly unit testing (which is related to decomposition) and verifying your assumptions.
By "decomposition", I mean that your solution is structured in such a way that small pieces can be individually tested and readily understood. If you have a 7000 line method you're (probably) doing something wrong.
Understanding what assumptions you've made is key too so you can verify them. For example, when I started with PHP I wrote a piece of code like this:
$fields = $_SESSION["fields"]; // $fields is an associative array
$fields["blah"] = "foo";
and I was scratching my head trying to figure out why it didn't work (the array wasn't being updated next time I queried $_SESSION). I came from a Java background where you might do this:
Map fields = (Map)httpSession.get("fields");
fields.put("blah", "foo");
and that would most definitely work. PHP however copies the array. A working solution is to use references:
$fields =& $_SESSION["fields"]; // $fields is an associative array
$fields["blah"] = "foo";
or simply:
$_SESSION["fields"]["blah"] = "foo";
The last thing I'll say about debugging and writing robust code in general is to understand the boundaries of your solution. By this I mean if you're implementing a linked list then the boundary conditions will revolve around when the list is empty.
Explain the problem to a colleague, or write it down to describe it. That will makes you think a different way, from a different perspective. In order to be more accurate, and to describe the context of the problem, you'll step back, get a higher level view of the problem, you may find out think you overlooked something that is actually important.
Sometimes, you even find the explanation before ending your description.
My best friend for many years has been to jump on my bike and go home. Just getting away from the keyboard has solved many problems over the years for me.
If the problem lasts to the end of the day, I try and consciously lock the problem away for solving before I go to sleep.
I realise this sounds a bit out there, but it has been very common in my experience that I'll wake up with at least an alternate approach to the problem, if not the full-on solution. The idea is not to stress about it - but actively decide to solve it over night. That way you can go to sleep without worry.
I think eating well, regular exercise and good sleep are huge contributors to the problem-solving process.
Usually I'll try nut out the problem for a few hours or so, trying different things writing it on paper, making diagrams. If none of that works I'll usually work through the following options.
Put a sticky note on my monitor and keep going with something else
Glance at the note through out the next few hours to keep the problem in the back of my mind
Google for similar problems and the methods used
Consult a co-worker or a friend
Ask on a forum such as stackoverflow
Give up and design the problem away or design a way around the problem so it can be dealt with some other time and stick a TODO note at the site of said workaround
Don't forget Google Code Search
It's often best to clear your head by doing something other than programming for a little while. See this answer for an example - I did it while struggling with a particularly thorny bug, and when I came back to the problem I solved it in about a minute.
Usually I try to solve it until I go to sleep.. Sometimes I write on paper what the code is doing and then I divide it in pieces; I try to know how the variables of the program change when it's running.
Try solving a much smaller version of the problem first and see how you get on with that.
Once you've done that the bigger problem won't look so scary.
Ask yourself: is solving this particular tricky problem really important to what you doing?
For the purposes of your application (or whatever the big picture is) is there a similar but easier problem that you could address to accomplish broadly the same thing.
Normally, I would get pen and paper and try to work out the details of the problem there. If that doesn't help, Google. Failing that, I'd do something else for a while, or ask online. Worked for me so far.
The fact you are stuck might be a 'code-smell'. Suggesting that their is something wrong with the design or approach somewhere else. Try to put your finger on what's causing this and fix this instead.
When you come back to your problem it might no longer exist.
One more time, browse thru what I think might be relevant, then take nap.
There are two other answers which mention sleeping or napping, but this deserves more emphasis. It is now known that there's SERIOUS machinery in there which goes to work when you sleep. Google (( CBS SCIENCE SLEEP )) will get you to a great free video.
If I can't figure out how to solve the real problem, I try to consider a simplified version of the problem. To take a simple example: I recently had the problem of finding a set of shipping routes to get an item from point A to point B, when there is not necessarily a direct route from A to B, but there might be an A to C and then C to B, or A to C, C to D, and then D to B. (I'm sure the airlines and railroads do this all the time.) That was fairly complex, so I tried looking first at the simple case: a direct A to B. That was easy. Then consider how I'd handle it with one stop along the way. Then consider two stops. At that point I was able to see the pattern to a solution.
Solutions to a simplified version of the problem may end up being a part of the bigger solution, with some additional complexity wrapped around them. But even if not, the exercise of solving the easier problem often gives you ideas on how to solve the real problem.
The main techniques I use (should be followed in order so that you can reuse what you have done in previous steps to be more efficient):
Define your issue: Try to clearly define what's the problem, and what's expected. See 2 to help you.
Collect data about the bug: Log everything: your attempts, the expected result, the observed result. This will avoid the need to redo several times the same tests (because your mind cannot memorize it all), and probably help you see the bigger picture.
Reduce your problem. This is true in general for any abstract modelling of natural phenomenons, but it's even more true of programming, because programs are very complex entities. You should try to reduce your code to a minimal program that reproduces your issue. Automated tools exist.
Talk to someone: several anecdotes affirm than about 2/3 of the bugs are resolved just by talking about it. See the Helpful Teddy Bear anecdote. If you have previously reduced your program to a minimal program, and have a clear definition of your issue, both will be useful to your explanation.
Reach for collaborative help: search on Google and on StackOverflow, and post if you can't find anything that answers your problem (but first see 1, you must have a clear definition of your problem).
As you can see, I put the collaborative help as the last step, because you should only ask for help after you have clearly defined your issue and tried to reduce the problem and fix it by yourself. If you reach for collaborative help before having tried the previous steps, you will either end up with a poor help or figure it out by yourself as soon as you have posted.
Also you can be interested in the Coursera Software Debugging course, which also describes several automated debugging methods.
Go to the toilet.
You move, so your brain gets oxygen.
You relax, so you focus on other things.
Peeing for innovation! :)