Archive for September, 2008
Project Euler, Problem #1
Posted by Alistair in Programming on September 22, 2008
The first problem within Project Euler isn’t meant to twist the brain in knots and is a gentle introduction to what is coming further down the road. The problem states:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
As I mentioned in my initial post about exploring Project Euler, I noted that I often write in a lowest common denominator with whatever language I’m using at the time. The net effect of that decision is often a working but poorly optimised solution or a solution that doesn’t fit well with the spirit of the programming language.
The solution to problem #1 took three distinct evolutions, the first of which is below and looks like a solution you’d likely see in any programming language:
i = 1
sum = 0
while i < 1000:
if i % 3 == 0 or i % 5 == 0:
sum += i
i += 1
print sum
I wasn’t a fan of needing to declare the variables to start with for such a simple problem, so removed the need for the i but using the Python range function to generate a series of numbers. The range function allows you to generate any length series of numbers, starting and ending where you want with any stepping. By default, it starts at 0 and a stepping of 1 – which means range(1000) is going to generate a list of numbers from 0 – 999.
sum = 0
for x in range(1000):
if x % 3 == 0 or x % 5 == 0:
sum += x
print sum
Python provides functionality for its lists called list comprehensions. The idea behind a list comprehension is that you can perform the same action, whatever that may be, against each item in the set. In the final solution below, I have applied a filter (the if statements), which now only returns those items from the for x in range that match and I’m not applying an expression to the resulting matches (the solitary x before the for). Once collected the new list is passed to the sum function to yield our result.
sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])
Project Euler, An Exercise In Exploration
Posted by Alistair in Programming on September 21, 2008
Some time ago I stumbled onto a math based problem solving site for programmers named Project Euler. The Project Euler site describes itself as:
Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.
The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context.
Possible solutions to the problems are verified through the Project Euler web site, where your successes are recorded if you sign up for an account. You don’t need to work through the problems in order, however doing so may be beneficial as work completed and previous questions is often reused.
I thought Project Euler would be a great exercise to explore more of the Python programming language. While I’ve read quite a bit about the Python language, I’ve not written anything substantial in it and I often find myself using the lowest common denominator in my approaches to solving problems as a by product.
My intention while working through the problems presented within Project Euler is to find a neater, cleaner method of solving the problem which is hopefully more pythonic. I’ll present my solution to the problems and with a little luck, I’ll receive some helpful improvements from the great programming community as well.
Django Internationalisation (i18n) Statistics
The Python web framework Django supports internationalistaion (i18n) for nearly 30 different languages already.
While reviewing the changesets flowing through the Django source repository, I often notice amendments to the internationalisation code and it got me thinking about how ‘complete’ the i18n status is for the languages that Django is attempting to support.
Enter a visually simple but very informative web site built using Google App Engine which polls the Django subversion repository periodically and compiles a table showing the percentage completion for each of the different languages.
I’m impressed that with nearly 30 different languages under their belt that the majority of them are reporting very solid percentage completion numbers, no wonder so many non-English speaking developers are using Django.
Ask.com Deceptive Advertising Practices Is A Grab For Cash
Posted by Alistair in Advertising, Search on September 2, 2008
While doing a little research this morning, I stumbed onto a paid advertisement within Google for Ask.com, informing me that I could booking BreakFree hotels & resorts from within the Australian localised Ask.com portal.
Being the curious kind of person, I followed their advertisement and was quite shocked by how deceptive they were with their ad and also the page it took me to.
Instead of Ask.com providing some sort of useful service inside their portal, they provided 10 Google advertising results front and center which were displayed as though they were organic results, followed by actual organic results (click the image for an expanded screenshot of their handy work).
I don’t necessarily have a problem with then doing paid advertising within Google for services that they offer (though in this case, they don’t have a service relating to my search results which was very deceptive). However, I do have a beef with the way they frame or lack there of, of the paid results from Google within Ask.com search results. If they had placed the same 10 results in the right hand side gutter or boxed them with a different background colour – then at least the user would have a chance of knowing the difference.
I wonder whether or not that sort of behaviour falls within the Google terms of service? It actually reminds me of when Microsoft were advertising on Google for MSN Messenger and taking the user into more search results within Live Search.