Wednesday, August 21, 2013

Project Euler

Project Euler is a collection of mathematical problems that generally require writing a program to solve. It's probably not the best way to learn a language, but it's a very good way to exercise your mental capability for coming up with algorithms heavily grounded in logic.

I've been toying around here using Python as my language and it's quite satisfying so far. What nice about python for doing this:


  • Strings and integers are easily changed back and forth.
    e.g.
    if a=12345, and I want the 4th digit as an integer:
    String way: a4=int(str(a)[3])
    i.e. convert a to string, grab index 3 (0 is first), and convert back to int.
    Of course there's an integer method too which can render this specific argument moot (a4=(a//10)%10) but it is still useful for other examples.
  • Integers have no size limit. So no need to rely on outside modules for "big" numbers - i.e. numbers that are actually long strings.
  • Python list ('array' sort of...) manipulation is very very simple. Reading in data and organizing strings is incredibly easy. Growing a list is a basic operation. If this were C, we'd have to use another class, such as vector.
  • List elements can be anything. String, integers, more lists etc.
One of the major weaknesses I've seen would be OS specific functions. In some cases, it's fine. But say you want to pull of PID information on a process in windows, or have a script run only once, and not run again to prevent duplicates. Differences in Windows and Linux/Unix implementations prevent this from being truly portable. In some cases - like pexpect - it ends up really only working properly in Linux, and while I guess it's possible to get working in windows, running it in a virtual machine is less of a hoop to jump through.

I'll try posting a few Euler solutions in subsequent posts as it's actually somewhat relaxing.


http://projecteuler.net/

No comments:

Post a Comment