Only one more project euler problem and I am level one
EDIT: YAY level 1
I just noticed that I have done all the python stuff on my laptop where I saved my executables in a directory called bin/ and all my C stuff on my desktop where I saved all my executables in a directory called scripts/
(The reason I’ve done that is because I set up my vimrc for C or Python on my desktop and laptop respectively, the reason that I named the folders that way was because I made the scripts directory for the first bash scripts I did and the bin directory to put world of goo in).
Someone on irc told me about this thing: http://projecteuler.net.
I’ve only done the first one so far (got kinda distracted by trying to put a whole bunch of disparate functions that seemed like they might be useful again sometime in a file and then trying to figure out how to add it’s path to the path for includes – which I did eventually by aliasing gcc to gcc -Idir in my .bashrc, dunno if that is a silly way or not – I think for my next distraction I should work out how to write makefiles :p).
I think it might be kind of fun to do one each day because at the moment I am in a bit of a “schedule fetishism” mood (this normally happens to me much earlier in the long unstructured summer, I think it took so long because I actually spent my time doing stuff spontaneously this year instead of just clicking “refresh” in my browser on some forum like I’ve normally done) ever since I decided to try and get myself up to speed with my maths (also, anyone reading this have any recommendations for a good stats book that would cover the stuff I would have missed out on by not doing A level stats? Because online reviews honestly do not give me a good idea of what to expect from this or that book and the range in local bookshops leaves much to be desired)).
So my attempt to get my head around classes, has not been so successful.
I spent much time making the classes, it was quite boring. Nothing particularily interesting was involved except the always kinda fun part of fixing the bits that were wrong (however inane, oops, forgot to indent etc) and seeing it work when they’d been fixed.
Today I finally got the funnest bit done (which reminded me that I had indeed been bored of making definition upon definition to change object attributes). My main impression of classes from all this is, yes I can see how when you are doing a bigger project than I have ever done it will make it easy to reuse things and be modular and all that jazz, but for everything I have ever tried so far it basically entails doing all the most boring parts at once rather than in chronological order.
What I did today was get the final draft of the process to generate a schedule-todolist from a whole bunch of task objects and the majority of that comes down to variations on this:
who is tim? ….mysterious magic word
….|…………………………|
…\|/………………………..\|/
sorted(tasklist key=lambda task: task.priority)
(1. Randomise list so that when all else is equal the order is random.
2. Sort by priority.
3. Duplicate daystilldeadline/occurancestillcompletion < 1 until = 1
4. Sort by dtd/otc
5. Sort by Fixed (can only be 1 or 0))**
This statement gave me a few things to think about… it also exemplifies what I mean about just looking up magic words to do this. But I am a curious girl, so I am not just going to leave it at that…
Two things I have drawn attention to, sorted and lambda. Sorted, apparantly (if the internet is to be believed) uses something called a “timsort”. I don’t know how those work, but that will ceirtainly be something I want to look into. However I am more intrigued by the mysterious “lambda”.
An excruciatingly intelligent girl in irc mentioned this lambda thing to me a few days, maybe a week ago… she said then it was something about inline functions, she started telling me about functional programming and map and reverse. I like to be challenged but I’m gonna be honest and say… I didn’t really get it. I didn’t think too much about it either because I figured, I need to worry about more basic things for now and I can worry about lambda and map when I am more educated
. However, there it is, lambda, in my sorted… it is helping me somehow to say for each of these objects sort according to this attribute… but HOW does it do that, WHAT is it doing, and on a deep level HOW does it work?
My adventure begins with Wikipedia,
In programming languages such as Lisp and Python, lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An example of this use of lambda in the Python language is this section of computer code that sorts a list alphabetically by the last character of each entry.
Python’s limited support for anonymous functions is the lambda construct. Since the availability of full anonymous functions is non-existent then named functions is the primary use of functions in Python. Lambdas are limited to containing expressions rather than statements, although control flow can still be implemented less elegantly within lambda by using short-circuiting.
http://en.wikipedia.org/wiki/Anonymous_function
Esp. http://en.wikipedia.org/wiki/Anonymous_function#Python
Next stop the Python documentation,
Lambdas
*******lambda_form ::= “lambda” [parameter_list]: expression
old_lambda_form ::= “lambda” [parameter_list]: old_expressionLambda forms (lambda expressions) have the same syntactic position as
expressions. They are a shorthand to create anonymous functions; the
expression “lambda arguments: expression“ yields a function object.
The unnamed object behaves like a function object defined withdef name(arguments):
return expressionSee section *Function definitions* for the syntax of parameter lists.
Note that functions created with lambda forms cannot contain
statements.
…wikipedia was more helpful imho
Python lambda forms cannot contain statements because Python’s syntactic framework can’t handle statements nested inside expressions. However, in Python, this is not a serious problem. Unlike lambda forms in other languages, where they add functionality, Python lambdas are only a shorthand notation if you’re too lazy to define a function.
Functions are already first class objects in Python, and can be declared in a local scope. Therefore the only advantage of using a lambda form instead of a locally-defined function is that you don’t need to invent a name for the function – but that’s just a local variable to which the function object (which is exactly the same type of object that a lambda form yields) is assigned!
OK… I have a reasonable idea WHAT lambda is now (and map to my surprise!) – I am still not at all sure how it is implemented (indeed I think the first step there would be to understand the nature of first class objects as opposed to any other kind).
…and so you see I did have fun with this after all (and it was a lot less confusing that I’d expected)
Other interesting websites I came across on my travels:
http://code.activestate.com/recipes/361452-anonymous-functions-without-lambda/
http://fundae.wordpress.com/2008/05/14/functional-programming-101-lambda-forms/
http://www.ymeme.com/python-vs-lisp.html
http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/
http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
* There are probably far better way of doing this I don’t know but go easy on me I am just starting out with this stuff.
This past week I have spent some time writing, scrapping, rewriting etc. something in Python to get my head around both using classes and python in general.
Tonight I have been thinking about this in comparison with my experiences with C so far.
A lot of websites on the internet mention that they think Python is a good first language, a good teaching language and so on. I don’t know that I entirely agree, as basically pretty much a beginner to programming, I think C is much better to start with, or was for me and probably would be for people with my personality at least.
I’ll explain why, and also why people with a slightly different personality might find the opposite. What I like a lot about C is how much it has taught me about how things work. How much insight, this short time with it, it has given me into exactly what is happening. Every little thing I had to figure out how to do in C showed me something new about what happens when my computer does that thing. All the stuff with pointers, memory allocation etc, I loved that precisely because I had never thought about it before, I had never thought exactly how programs deal with memory or how they know where information is or pass references to memory addresses or anything like that. Now that I have thought about it seems criminal to me that I never thought about it before then, but I hadn’t. I think most people who have never done this sort of thing before would similarly not have thought about those kind of really basic things, and C forces you to think about it.
Python on the other hand, does not, everything I’ve had to find out how to do has been a matter of finding out which module to include and what words to use to get the thing to magically do what I want. OK, I know it is possible to look deeper and find out what is going on, but the truth is, when I am in the middle of trying to get something to work I am not gonna get distracted by looking into what is going on at a deeper level. And yet, despite that, the reality is I am a lot more interested in “what is going on” than making things work.
And this is why I think it in some senses comes down to personality. It is also why I think I never really got into computers properly until I tried Linux. Because someone whose primary interest is in creating things, they might well, even without discovering Linux, find themselves driven to create things and get into that, but me, I never felt a drive to create things – oh I enjoy it, its always nice to finish something and have something to show for yourself – but what really excites me is finding out how things work. And when I had windows, how things work was kind of obscured to me in many ways, whereas, the most glorious thing about Linux is that, its transparent, you can look into everything about it and have a real hope of one day understanding it – or at least, if you can’t it is because of your own limitations and not because anything is hidden from you. That is so cool.
I can completely see the advantage of programming languages which do a lot of more low level stuff behind the scenes, and I think one day I might prefer that too, when I know a lot more, when things that for me now are like “wow, so that is what is going on” have turned into “ugh, I can’t believe I have to do this EVERY TIME” then you can bet I will be singing the praises of Python, I can see that day off in the distance, but for now I like C better, because I don’t know the basics, because I don’t know what is going on and its all magic to me, and anything that can make that magic into something more comprehensible, anything that beats me over the head with what it is actually doing, is something that I want and need right now.
In some ways I think it comes down to a lack of imagination. I don’t imagine what I don’t know until it sneaks up on me and laughs mockingly, and at the moment I don’t know most anything. Yet I find it so delightful to find out new things. I think for me there is no greater pleasure than that moment when things fall into place and I actually finally understand something.
I literally live just for those moments.