Tuesday, April 2, 2013
recursivereverse
================
Example is here: https://github.com/gryftir/recursivereverse
recursively reverse a linked list. Recursion is mostly used to keep track of state data (in this case, node information and order) using the stack's function call order.
AFAIK there isn't anything you can do with recursion you can't do with an interative approach, but the data structures are going to be somewhat ugly.
Recursion isn't hard, it's just not taught correctly. It's fairly easy to break it down into rules.
And then people memorize how to do stuff, instead of learning it from priniciples.
Take the classic question, recursively reverse a linked list.
You will see a lot of people making false starts, or trying to reverse the list in some way like they would reverse an array, or even repeatedly walking the list to the end.
But if you understand some simple rules about recursion, it's fairly easy to figure out how to do this on the fly. And that is so much better then memorization, because you can explain why you are doing things.
first you are reversing, so you're moving backward, which means you calls your function, then do assignments. If you were doing something moving forwards, like printing in order, you'd print first, then call the function.
Now we need to worry about the end, and the beginning.
First, the beginning, which happens after we have recursively walked the list.
Now, we want to walk to the last node before null, and we need a place to store what we return. so assuming we have something like this in c 'node * recursivereverse(node * current)' and a node structure with next as the next link (we never touch our data)
our test is
if (current->next)
node * newbeginning = recursivereverse(current->next);
else
return current;
//some assignments go here;
return newbeginning;
Then we just need to figure out the assignments.
Now, people may get tripped up and try to use the new beginning... but then they have to walk it to the end... but the current node already knows the node that comes after it.
so we assign current->next->next = current; IE the node that was previously after the current node now points to the current node as it's next.
And that's fine for all the rest of the list, until we get to our original beginning node. We need to worry about the new end.
Since it's going to the end, we need to point it to NULL, otherwise it will point to it's predecessor, meaning we'll have an infinite loop at the end of the last two nodes.
Easy way to deal with it is to just assign the next of the current node to NULL, for each node.
so we add to current->next->next = current;
current->next = NULL;
and then you have a reversed linked list. This makes certain assumptions about the list (like it has at least one node, and isn't circular), so don't just copy this for your interview.
So with some simple rules, I figured out how to reverse a linked list. But those simple rules aren't taught, or at least, I came up with them for myself after I became a computer science tutor at my college, because I needed people to understand recursion.
So I guess my point is, it's not that people can or can't understand recursion, it's whether they are taught to think about things in the right way.
Now to a certain extent, we need to teach people to generalize, but if you teach people a general rule (always worry about the beginning and end of any data structure, and that's important for everything, including memory management and debugging) and a recursion specific rule (call the function first to reverse, call the function after to use the current order), and they can generalize.
Too often people come to program with the ability to copy code and get it working (and that isn't anything to denigrate, because it's not simple), but lack the understanding of how to think about things.
Sunday, March 31, 2013
More Vim stuff, fix indentation and and remove blank lines
I've been using Vim as my ide, and it's been wonderful.
some additional cool things I've found.
you can have your whole file's indentation fixed with gg=G
it may be helpful to :set sw= and :set tabstop= to 4 or 2, and turn cindent on :set cindent, to handle brackets.
gg goes to the top of the file = fixes indentation, and GG does the same to the bottom of the file
you can erase blank lines with :g/^$/d
:g makes it a global command executed on all instances which match your search pattern, ^$ (start of line and end of line with nothing in between) matches blank lines, and d deletes
you might also use something like :g/^\s*$/d to remove all lines with either blanks or only whitespace
for Perl, I recently used :%s/\(\S\+\)\s*\(#.*\)/\r\U\2/g which cuts any comments from the end of a line and inserts them on a new line, in uppercase and :%s/\s*\(#.*\)/U\1/g which removes any spaces between a comment and the beginning of the line and makes the comment uppercase to be useful.
some additional cool things I've found.
you can have your whole file's indentation fixed with gg=G
it may be helpful to :set sw= and :set tabstop= to 4 or 2, and turn cindent on :set cindent, to handle brackets.
gg goes to the top of the file = fixes indentation, and GG does the same to the bottom of the file
you can erase blank lines with :g/^$/d
:g makes it a global command executed on all instances which match your search pattern, ^$ (start of line and end of line with nothing in between) matches blank lines, and d deletes
you might also use something like :g/^\s*$/d to remove all lines with either blanks or only whitespace
for Perl, I recently used :%s/\(\S\+\)\s*\(#.*\)/\r\U\2/g which cuts any comments from the end of a line and inserts them on a new line, in uppercase and :%s/\s*\(#.*\)/U\1/g which removes any spaces between a comment and the beginning of the line and makes the comment uppercase to be useful.
Wednesday, March 13, 2013
new VIM version autocomment fix
I compiled the most recent version of VIM. It has an annoying behavior
if I start a comment in insert mode and press enter in insert mode, the next line is automatically a comment too.
This means cutting and pasting from outside vim will fail at the first comment. I guess it's useful for long comments, but mostly it's an annoyance.
However, by piecing together stuff from a few web pages and vim doc you can easily fix this.
You need to change your format options
you can do this from inside vim with set formatoptions. you can see your current formatoptions with :setlocal
So briefly:
To turn off, you need to disable the q option in formatoptions
:setlocal find your format options then in your .vimrc au FileType * set formatoptions= after the =, put your format options without the q
see http://vimdoc.sourceforge.net/htmldoc/change.html#format-comments for more details
* after FileType can be a language... so c, perl, ruby, python, etc.
Thanks to http://www.kevinslonka.com/index.php?section=1&blog=179&page=1 for getting me started. The comments have several clever fixes, including a compromise to stop commenting if the commented line ends with a space, but I haven't gotten that working myself. You can also fix just the pasting with :set paste, if you prefer.
if I start a comment in insert mode and press enter in insert mode, the next line is automatically a comment too.
This means cutting and pasting from outside vim will fail at the first comment. I guess it's useful for long comments, but mostly it's an annoyance.
However, by piecing together stuff from a few web pages and vim doc you can easily fix this.
You need to change your format options
you can do this from inside vim with set formatoptions. you can see your current formatoptions with :setlocal
So briefly:
To turn off, you need to disable the q option in formatoptions
:setlocal find your format options then in your .vimrc au FileType * set formatoptions= after the =, put your format options without the q
see http://vimdoc.sourceforge.net/htmldoc/change.html#format-comments for more details
* after FileType can be a language... so c, perl, ruby, python, etc.
Thanks to http://www.kevinslonka.com/index.php?section=1&blog=179&page=1 for getting me started. The comments have several clever fixes, including a compromise to stop commenting if the commented line ends with a space, but I haven't gotten that working myself. You can also fix just the pasting with :set paste, if you prefer.
Sunday, January 20, 2013
Too much time in the terminal
I just tried to use Ctrl-A to go to the beginning of the line in chrome :)
Thursday, January 10, 2013
The Juncture Design Pattern
It's 2 am, but if I don't write this now, I probably won't get to it with the same verve.
Lets imagine we are building an object oriented software system to handle everything a school does. Students register for a class section.
a one to one relationship. One college, one president.
many to one. One teacher, several classes. Store information about the relationship in the class info.
Now a student can be in several sections, and each section will have multiple students.
many to many relationship.
You can use callbacks, so that the section has a pointer to the student, and the student has a pointer to the section. But what about information regarding the relationship between the two? The grade they are getting?
That information deserves it's own object. In the middle, with two many to one relationships.
Or something. In this example, it's not clear why. Need to Look up the gang of 4.
Lets imagine we are building an object oriented software system to handle everything a school does. Students register for a class section.
a one to one relationship. One college, one president.
many to one. One teacher, several classes. Store information about the relationship in the class info.
Now a student can be in several sections, and each section will have multiple students.
many to many relationship.
You can use callbacks, so that the section has a pointer to the student, and the student has a pointer to the section. But what about information regarding the relationship between the two? The grade they are getting?
That information deserves it's own object. In the middle, with two many to one relationships.
Or something. In this example, it's not clear why. Need to Look up the gang of 4.
Wednesday, January 2, 2013
Introduction
Hello Folks:
Welcome to my little blog about coding, technical job searches, anything related I think is appropriate.
My name is Lawrence Siebert. I am a software developer. I code. Constantly.
You could call that a recent thing, since I've only been formally programming for about a year, or you could say I've been programming all my life. Before I ever wrote a line in C, I was writing programs for SAS and R to do statistical analysis when I was getting my Psych degree. I wrote scripts for the mIRC irc client on windows when I was 13. I wrote tiny little qbasic programs on a 386 when I was 8.
So that's who I am and what I do.
Subscribe to:
Posts (Atom)