Pages, some stolen, some original

Wednesday, March 16, 2016

Programming is so weird

From the Matrix, because that's where we live now, isn't it?
I've been playing with some simple programming puzzles over on codingame.com, trying to learn the Go programming language, and I've been making some progress. I started working on the Mini-Sudoku puzzle yesterday and in short order I had written a solution. Well, no. Didn't work. So I spent a couple hours yesterday trying to figure out what was wrong. I figured I had made a typo somewhere, interchanged a couple of indices or something, so I added a bunch of debug statements that would inform me of what was going on inside this basically trivial program, but nothing pops. Everything is behaving as it should. Okay, let's let it run for more than one iteration, and boom!, out pops the problem.
    The puzzle involves filling in a little grid with numbers. At each iteration you can fill in some more numbers, but only if you are working from your worksheet. If you keep starting over with the original problem you will never get any farther than the first step. Doh!
    The problem, for me, stems from the difference in the way the C and Go programming languages treat strings. In C a string is just another array of bytes that is filled with characters. You can replace those characters willy-nilly, just like in any other array. In Go, however, strings are inviolate. Once created they cannot be altered. If you want to alter them, you need to copy them into a byte array and then you can muck about with the array as much as you like.
    I wonder if there is someway to convert a byte array to a string? So far the only ways I've seen to create strings is to write them into the source code so they are created at compile time*, or to read them into string variables from a file. There's probably a way to do it, but I haven't gotten to that lesson yet.

*compile time is that moment when you feed your source code, the program you have written, to the compiler, another computer program that turns your source code into machine code. Machine code is what the computer likes to read. Compile time is like a final exam at school, you submit your work to the powers-that-be and wait for it to be accepted or rejected. It's usually rejected, but you get to keep trying over and over and over again until you get it right. Exams are bad, full of trepidation, but at least they have a time limit and when they are over you are done. Trying to get a piece of source code to compile successfully can go on forever.

1 comment:

  1. I'm currently working in Python, which is easy to learn, interpretive, so rapid debug cycles, object-oriented and with a YUUUGE number of libraries for almost everything available online for free. Also good community support :-)

    ReplyDelete