Thursday, June 30, 2011

chrislearnspython

If I can get through LPTHW and DITP I'll pick up the O'Reilly Python book.

LPTHW takes a really, really slow approach and doesn't get into functions and classes until the end and is more of a "learn programming" instead of "learn Python". DITP is the opposite.

Wednesday, June 29, 2011

Python CSV module

Oof, Python works in mysterious and curiously documented ways. No, that's not fair - I'm just not down with all the lingo.

I wanted to quick and test to see if I can pull in some CSV data.

test = csv.reader(open ("Support Worklist.csv"))

test is now a type of storage variable called an "iterator". An iterator is an object that uhm.. iterates? It's a bizarre concept because it has basically one method called next() that delivers the next line of the data and once it reaches the end of the data it stops. There is no "previous" or "rewind" method which is really baffling. I have no idea what use it is! Maybe I'm just not thinking very Pythony yet. I need to get through those real popular intro documents "Learning Python the Hard Way" and "Diving Into Python".

So all this quick test did was reveal that I'm pretty ignorant of Python's data types and it's preventing me from doing anything interesting.

Dang. I was really hoping the CSV module actually DID things with CSV files other than read/write them.

EDIT:
Hmm... http://docs.python.org/library/csv.html#csv.DictReader

EDIT x2:
StackOverflow's definition of the "iterator" tag is pretty revealing:
An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory. Iterators may be further limited in particular traversal directions.

Python project

http://docs.python.org/library/csv.html

I'm itching to try and write a quick and dirty program for zipping through an Excel spreadsheet I keep for work stuff and giving me some basis stats. Nothing fancy - it's just a list of support cases where every row is a new case and the first colum is the date I entered it in MM/DD/YYYY format. I was trying to find a module that read in .xlsx files but then discovered that there is a built in CVS reading module. Score. Now I gotta figure out how to extract all the dates and then (for example) count how many entries were made per month (per month that exists - I started this spreadsheet in January of this year).

Using a shared library written in C with Python

I'm trying to combine some C and Python. Did some "light" reading on the ctypes module in Python.

There are several ways of using C libraries in Python. Ctypes, SWIG, Cython, and plain making a Python module in C. Ctypes seemed the most straightforward.

Ok, to start with I needed a library. I jumped into Ubuntu and started a new shared library project in CodeBlocks and did the following:

This compiles to a .so file. I named the project sayhello so it compiled libsayhello.so. I put this file in /user/lib.
Alrighty so I have my library. Time to get into Python. I don't have any Python IDE so I just use the terminal. I want to use Python to call my sayhello() function.

And there you have it - a VERY simple example of calling a C function in Python.
There is a lot about shared libraries I don't understand. I have a superficial knowledge of what's happening behind the scenes (and I know what the benefits of using dynamic link libraries are). I keep reading things about "exporting" or "importing" functions but I'm not sure what that means, and I don't understand some of the stuff I see in Windows .dll source code.

Aaaanyway. I've been up since 6am on this and I am done thinking about this for today.

Tuesday, June 28, 2011

win7

NewEgg is having a memorial day sale so I bought another copy of Windows 7 for my laptop to dual boot to. I've basically proven I can't get any real work done on my PC since it's where my games are, so a copy of Windows on the laptop will be for programming and work stuff.

The topic of Python came up at work in a meeting again. I should resume reading those Python tutorials just to make sure I have a good heads up on what's what if that ever comes around.

Monday, June 27, 2011

OO jump

I haven't made any move to learn object oriented programming because I haven't been in the middle of a program and thought to myself that I could really do this or that faster/better with objects. I'll admit my knowledge of OOP is extremely superficial but the bits I know aren't really geared towards the small program I'm writing.

On the other hand I do totally get why it would be useful with GTK (indeed a lot of GTK is mimicking OO behavior) in the readability department. I think the C++ bindings look more like:

windowwidget.setsize() instead of SetWindowWidgetSize(*window, size)

Which would be nice sometimes, but not important enough to make the switch just yet.

Sunday, June 26, 2011

is this cheating?

If I take the square root of a number and then square the result I'll get the same number.

If I take a number and take the square root, then obliterate anything after the decimal point (cast to int perhaps) then square that result I can compare it to the original number and see if they're the same. If they're the same then the square root must have been a whole number.

That's legit, right?

Saturday, June 25, 2011

PE9 finding if a number is a square

http://stackoverflow.com/questions/295579/fastest-way-to-determine-if-an-integers-square-root-is-an-integer

Ok, maybe it's not such a simple problem!

EDIT: Wait maybe I'm overcomplicating this.

Let's take 40 and 36.

The factors of 40 are 1,2,4,5,8,10.

The factors of 36 are 1,2,3,4,6,12.

Is there any way I can get the factors of a number and then square each one to see if they make the product?

So for 40:
1x1 != 40
2x2 != 40
4x4 != 40
5x5 != 40
8x8 != 40
10x10 != 40

For 36:
1x1 != 36
2x2 != 36
3x3 != 36
4x4 != 36
6x6 = 36
12x12 != 36

Hmmmmm. I can't think of any situations where it wouldn't work.

The nagging suspicion that I'm misinterpreting this problem is creeping up on me.

PE9 WIP

Every time I take another look at PE 9 I confuse myself. Woof.

I did PE 8 in XCode and it really helped to have the debugger telling me what all my variables are as the loop progressed. Every problem I was having I diagnosed through that. That's an extremely superficial use of the debugger from what I can tell.

EDIT: Thought of a first step. Remember that a < b < c, so the largest that c can be is 997 (in the instance of a = 1 and b = 2). Obviously that's not a real life situation, but what other constraints are there? b can only be 499 at the most when a = 1, b = 499, and c = 500. Again, not ever actually going to happen but it's a good place as any to make my loop end.

What's an instance where a ^ 2 + b ^ 2 = c ^ 2 such that c isn't a natural number?

2 ^ 2 + 5 ^ 2 = 29. Ok, how do I know that the square root of 29 isn't a natural number? I need to find a way to test for square-rootness. I'm sure there's a good way to do it that's just not jumping out at me.

Project Euler 9

I have no idea how to go about this...

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.


I think I can figure out a way to brute force it, but it's not pretty. Well, there are only so many numbers for a+b+c=1000. 1+1+998, but 1 ^ 2 + 1 ^ 2 != 998 ^ 2.

No, maybe that won't work? Shoot, this is the first PE problem where it wasn't obvious how to brute force it. Oh wait, there's the additional constraint that a < b < c. Maybe that's helpful? Ok so start with 1 < 2 < ? That's 1 ^ 2 + 2 ^ 2 = 5. I can check that 5 isn't a square number (I hope). IF the sum isn't a square I increment b (nested loop?). If it is square, I check for a+b+c=1000. Once I hit a square number as c that causes a+b+c < 1000 then I know I need to start over with incrementing a to 2 and starting b at 3. Then repeat.

That's the way to brute force it... maybe.

Project Euler 8 solution



This was easy once I found a good way to handle stepping through the array. I went with subtracting '0' (the character, not the number) from the elements in the array for getting the actual number. atoi() was not working the way I wanted it to, but I didn't really explore the option with a smaller sanity check program.

Project Euler 8

I like this one.

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

What first comes to me to do is make the numbers on big string and then scan through the string. That's easy enough, but I'm nervous about again having to use atoi() to convert the "number as character" to "number as an integer". Is that just what you're supposed to do or is there a better way? Characters 0 - 9 are numbers 48 - 57. K&R uses a method where any number as a character has '0' subtracted from it to actually turn it into its proper number. So, '9' - '0' = 9.

Maybe I'll try both ways?

Tuesday, June 21, 2011

GUI

I was so hot to trot at the start to learn a GUI framework because I didn't count anything as "real programming" unless it looked like the "real programs" I use on a day-to-day basis. Now that I'm hobbling along with the basics (buttons, text, input) I realize two things: you can't easily tack on a UI to an existing set of functions, and there is nothing "real" about any large enough toolkit/framework.

On the first point I suppose I could have written PE7 differently that more heavily compartmentalized the prime detector function and the part where it counts the first N primes and kept them separate from any UI related code. That's certainly a goal of the next time I do this.

On the second point I'm trying to say that despite all the fancy things in the code I never had to manually allocate memory or do anything too complicated because the framework had all these canned functions for me. I bet it's really easy to be a programmer stuck in one or two APIs and over time lose general programming skills.

I still have never coded a linked list or a queue or a stack or any other advanced data structure. I've never even encountered a problem that would require that! I'd hate to artificially shoehorn one into a PE problem.

Project Euler 7 Solution



It works, but I think it goes too fast and doesn't update the text until the very end so you don't get a running tally of the primes found. That's ok.

A few thoughts:

1) My variable naming is poor. I got better near the end but I am NOT consistent with uppercase/lowercase rules.

2) The way I extract an integer from the string stored in the text field is a bit... unpleasant. gtk_entry_buffer_get_text returns a pointer to the text buffer containing what's in the entry field. Then I use atoi() to pass the values to an integer variable and I then perform all the prime checking. I'm extremely puzzled that there wouldn't be a built-in function for this as part of GtkEntry. There are plenty of functions for getting/setting and even highlighting and returning the number of letters and whatnot, but nothing to tackle the times you want to use that entry box to contain and return integers. The only number entry widget is a box with an up/down arrow. Kind of ugly. Anyways, it works.

3) I got to use sprintf() in a "real life" setting. That's a first for me.

4) There is NO error checking or any way to cancel the operation (but there is a hard limit on how high of a number it will check to. Error checking or checking for NULL in any initialized pointer would have taken way too long so I skipped it. The temporary character arrays I populate with sprintf() before they're passed to the labels are size 50. Should be plenty but I didn't include any checks there.

5) In a lot of ways this program proved to me that I understand pointers, structs, and how to read the GTK API docs. I had no idea how the GtkEntry widget worked but through trial and error and lots of reading I figured it out.

I don't think I'll create a user interface for every PE problem. UI is hard. I need to get through the GTK book more so that I can get to the chapters on using Glade (the WYSIWYG UI layout editor for GTK).

Monday, June 20, 2011

matlab

Matlab has a front end program called "mex" that calls a 3rd party compiler (whatever you have installed that's compatible) which will compile a C program into .mexw32, which is basically an executable file that is called in Matlab. Making a legit .mexw32 file looks pretty intense. A sample C program that takes in a number and returns 2x that number is 30 lines long and contains a lot of pretty crazy looking stuff. I spent some time studying it today and found that most of the crazy parts were sanity checks to make sure you weren't passing anything that it couldn't handle. The rest of the crazy is simply setting up Matlab-compatible data types to pass values into.

If I could find a sample program that shows how to fill up an array of data then it shouldn't be too tough to shoehorn in my file reader program such that it would read a file and dump the data into Matlab arrays. That would be pretty neat.

PE7 WIP code



It only pops up the GUI, but the last part SHOULD be pretty straightforward. I copied over the prime finder from PE3 and it sits in a separate .c file. I'm glad I was able to combine my two learning projects: GTK and PE.

EDIT: I don't think it will be long before I'm going to force myself to learn Glade so that I don't have to code the GUI by hand. That was super tedious and there are only six widgets involved!

So the typedef for the struct is because in my callback function I needed to initialize a pointer to the widget data structure to the gpointer passed to the function. Just saying "struct Widgets *widgets" wasn't working so then I put in the typedef part. THEN I learned that a type defined in main doesn't carry over to functions that main calls so I tossed it outside of main. I don't know if making it typedef or putting it outside of main is what let me define a pointer to it but it doesn't matter. I'm really close to putting it into a header file to clean things up a bit.

Sunday, June 19, 2011

PE7 WIP

Man, really focusing on pointers early on has done nothing but saved my skin today.

I've been packing all my widget variables into a structure which has really helped. I wound up needing to make it a typedef struct so that I could declare a pointer to the struct data type. Maybe I didn't need to do that but it fixed the issue I was having so I'm happy.

Right now the program will let you enter something into the text box and when you click the button it prints whatever you entered to the console. That was a big step because now I can take that entry and do whatever with it.

PE7 GUI WIP

http://i.imgur.com/Vz6SI.jpg


Spent a lot of this morning on this. Haven't even handled the actual task at hand yet - just trying to get the GUI to compile without complaining. Got a handle on it but it took some trial and error.

Saturday, June 18, 2011

better idea

I'm going to change the program up - it will have a button to start and a field for which prime to find (10 would find the 10th prime).

The book hasn't covered text entry fields so this is a test of how well I can go from API docs to practice without having seen an example program.

Friday, June 17, 2011

we'll do it live

You know what? STRUCT EVERYTHING!

structs

You know I just realized that a lot of parameters in SDL are packed into structs and I bet it's to avoid having to have dozens of arguments getting passed to a function. It's easier to just pass a pointer to a big structure of stored parameters.

PE7 WIP

I'm going to try to shoehorn in some GTK with Project Euler 7.

The plan is to pop up a window holding a GtkFixed container widget that contains a button and two labels. The button will just say "start" and label one will say "Primes Found:" and the second will say "Current Prime:". The idea is that you click start and then it gets a move on finding primes and updating as it goes. I need practice designing programs and using GTK as much as I need to do these Project Euler programs.

There isn't a particular reason I'm using the GtkFixed container widget - maybe I guess because I don't have to guess at how it will do default positioning?

Thursday, June 16, 2011

good thread

My gripe GTK is that the callback functions (functions executed when events are detected, like a button press) only take one argument - a generic pointer to some data. If I had a function that adds two numbers together I couldn't just copy/paste it into my GTK-oriented program and then pass the two numbers after clicking a button. The callback function HAS to obey the prototype dictated by the type of signal being caught and dealt with.

This thread: http://www.linuxquestions.org/questions/programming-9/gtk-signals-and-callback-functions-376872/ goes over a few ways of handling that. What I've taken away from the research I've done is that it's HARD to just program on the fly. You really need to map out everything you want the program to do. I couldn't for example write my function for adding numbers and recycle it between console-oriented and GTK-oriented programs. Well, that's not true. The GTK version could easily work with the console version.

So basically (and this is a non-functional simplification) my console program function would look like this:

int add(int a, int b)
{ return a+b; }

GTK only allows for one variable (the pointer) to pass to my function. I'd have to put my a and b either to a struct and pass a pointer to the struct or into an array and pass a pointer to the array. The thread linked above shows examples of both.

int add(void *data)
{ return((data->a)+(data->b)); }

I might be missing a cast up there.

virtualbox fix

The latest update to VirtualBox fixed the 64-bit issues that prevented me from running Ubuntu without having to reboot my laptop in 32-bit mode. That's certainly going to be one less hinderance to my C learning progress.

Monday, June 13, 2011

catching static

I figured now would be a good time to push my knowledge a bit. Project Euler is great for learning how to think logically and apply math, but it's not enriching my knowledge of the language or compiler much.

I've decided to put my prime finder function into a static library. In fact I might put several versions of it in one library (the brute force, the only-odds, the square root method, etc).

I ran a quick test today of putting a function to print "Hello World" in a library, and then in a new project linking against that library to get access to that function. It worked just fine but it seems like a hollow victory because XCode makes it pretty easy. I might do this one via command line just to make sure I'm following the whole process. Haven't decided yet.

UPDATE: In short...

Sunday, June 12, 2011

Project Euler 7 WIP

Oof, more primes.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

I already wrote a brute force "is this number prime?" function. One solution would be to just increase an index and test that against whether or not it's prime and then increase a counter when it is. Actually that IS the solution. The "trick" is the prime finding function. I need to make a smarter prime finder.

EDIT: Finally writing things out in this journal has payed off! I found my notes on prime finders from last February. I think the two big things were to not check even numbers and you only need to check up to the square root of the number in question: http://chrislearnsc.blogspot.com/2011/02/maybe-i-got-it.html

Project Euler 6

This one wasn't hard at all.

http://projecteuler.net/index.php?section=problems&id=6



In fact I'm not even going to bother fixing the indentation.

Thursday, June 9, 2011

PE5 optimizations

I implemented the optimization mentioned in the last post and got the running time down from 13 seconds to 3 seconds. Oh, and there is a pretty nifty time profiler that even shows the time taken by the individual functions.

Changing the program to only test even numbers got it from 13s to 9s, and checking only against 11 to 20 got it down to 3.

Wednesday, June 8, 2011

Project Euler 5 Solution



No optimizing in this one because I was in a hurry. CodeBlocks does the favor of timing execution but XCode doesn't (not in an obvious way that I can see).

This was a simple one so I decided to try doing K&R style brackets. Don't like it.

EDIT: I suppose two things are that I don't need to check odd numbers, and I only need to check 11-20 in the divisors.

8pm

Wedding invitation ordering is dominating the evening so I might not get PE5 done.

Getting into GTK+ dramatically propelled my C knowledge and even more important taught me how to read API documentation. If I ever want to get into C++ I'll need an equally good catalyst for pushing the boundaries of my knowledge. Qt maybe?

I wonder if anyone actually does proper Windows programming anymore.

Project Euler 5

PE5 states:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

The brute force way is to increment from 1 to a billion (or any big number) and for each one of those numbers test it for even divisibility by 1 to 20. I'm trying to think of clever non-brute-force ways, but the best I can do is reduce the need for some of the numbers to divide by. Everything is divisible evenly by 1 so there's no need to check for that. If I'm checking for divisibility by 9 then checking for 3 isn't necessary. Is there a pattern here that I'm missing?

Maybe if I go backwards: If a number is divisible by 20 then it's assumed that it's also divisible by 10, 5, and 2. 19 needs to be checked. 18 is divisible by 2 and 6... Ok maybe the trick is that I only need to check for divisibility the numbers that aren't already evenly divisible by other numbers in the 1-20. There has to be a more graceful way of saying that. The point is I can reduce the 20 checks per number.. So...(scribbling on paper here)... I should only have to check from 11-20. That's better, I guess? 10 is covered by the check of 20, 9 by 18, 8 by 16, etc.

Energy permitting I'll try to bang this out in C tonight.

Monday, June 6, 2011

optimism

In my more optimistic days I purchased a book called "Professional C++". I think I picked it up in 2006 before I picked up Stroustrup and of course well before I was ready to tackle anything at all!

I've been flipping through it and it actually looks really interesting. It covers using debuggers, input/output streams, and it has a ton of design guidance and a dozen or so "real life" examples of when to use stuff. I wish there was a book like this for C. There probably is but truth be told I've done a lot of literature searching and I haven't found anything like this yet.

Friday, June 3, 2011

buried treasure

I have to move out in a month so I'm trying to go through boxes of stuff I've accumulated to throw out or give away or store elsewhere. Most of my accumulated stuff is books. I'm putting a Perl book in the "take to work" pile along with a few old neuroscience books.

One of the books I found is "The C++ Programming Language" by Bjarne Stroustrup, which is the K&R of C++. I totally forgot I bought that back in 2007 (the receipt is still in there). I'm not sure why I picked that up but it's nice to know I have it whenever I decide to go down that road.

Wednesday, June 1, 2011

noise

I want to try to make some noise with SDL. It ties into a work thing I want to do.

http://www.lazyfoo.net/SDL_tutorials/lesson11/index.php

Also I want to find a solid tutorial on serial communication.