Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Saturday, July 30, 2011

Installing nltk on Mac OSX Lion

Recently I needed to install nltk, it was giving me errors regarding YAML not being installed. In a Python-virtualenv environment, the following did the trick:


$ ls
bin include lib
$ bin/pip install pyyaml
$ bin/pip install http://pypi.python.org/packages/source/n/nltk/nltk-2.0.1rc1.tar.gz
$ . bin/activate

Tuesday, December 7, 2010

Playing with Clojure

I have been playing with Clojure more and more lately, looking for simple problems to improve my understanding of it. One thing I was able to find while surfing on the Internet was the problem about the 'longest common subsequence' of numbers in a collection. I remember writing code for it some time ago in Java - this being code with counters for "max_repeating_so_far" and "max_repeating_in_sequence" numbers, etc., plus some dynamic programming added on top of it. I'm regularly being amazed by the compactness of the code for problems alike when using Clojure (my code here is surely not optimal, but it's been fun):

user=> (def nums '(2 1 1 2 3 3 2 2 2 1))
#'user/nums
user=> (def sublists (partition-by identity nums))
#'user/sublists
user=> sublists
((2) (1 1) (2) (3 3) (2 2 2) (1))
user=> (reduce max (map count sublists))
3
user=> (doseq [numlist sublists]
(when (= (count numlist) (reduce max (map count sublists)))
(println numlist)))
(2 2 2)
nil


Having too much free time, that's a program of mine for calculating sine using Taylor's formula:
(defn fact [n]
(reduce * (take n (iterate inc 1))))

(defn sin-iteration [x n]
(/ (* (Math/pow -1 n)
(Math/pow x (+ (* 2 n) 1)))
(fact (+ (* 2 n) 1))))

(defn sin [x n]
(let [res (atom 0)]
(dotimes [iteration-counter n]
(swap! res #(+ (sin-iteration x iteration-counter) %)))
@res))

;;user=> (sin 0.52359 10)
;;0.4999924000896871

Friday, October 29, 2010

AI contest of Google

For the second time this year there's an AI contest of Google. It got me hooked for a while (submitting a dummy AI bot put clumsily together in Python over a few evenings). I think I liked it trying my luck with it.

Monday, May 17, 2010

Copying lists with Python

For single lists, the "[:]" would do (reference e.g. at http://henry.precheur.org/python/copy_list).

For nested lists, I think a module should be used (copy.deepcopy(list)), as in:

>>> import copy
>>> a
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> c = copy.deepcopy(a)
>>> c
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> c[0][0] = 7
>>> c
[[7, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> a
[[5, 2, 3], [1, 2, 3], [1, 2, 3]]
>>>

Thursday, April 15, 2010

Python graphs adjacency matrix to adjacency list

I've been playing with it, and here's the code I came with (posting it here so that it doesn't get lost afterwards, as it usually happens.


matrix = [
[0,1,0],
[1,0,1],
[0,1,0]]

def adjmatrix_to_adjlist(graph_as_matrix=matrix):
adj_list = {}
for (index,nodes) in enumerate(graph_as_matrix):
adj_list[index+1]=([verteces+1 for verteces in range(len(nodes)) if nodes[verteces]==1])
return adj_list

graph_as_list = adjmatrix_to_adjlist()

Graphs - DFS, BFS in Python

Lately I've been playing with Python and I was curious to implement some graph algorithms. For a start I wrote the DFS and the BFS ones.

The output:
>>> import graphs
>>> graphs.bfs(2)
1 3 4 5 6 12 7 11 10
>>> graphs.dfs(2)
2 5 7 10 12 11 6 4 3 1

The test graph is:



With the following short script the graph will be drawn as png (thanks to the great pygraphviz tutorial):



So that it looks like this:


The BFS algorithm is as follows:



And the DFS one (exactly the same, just using a stack instead of a queue):

Wednesday, April 14, 2010

Django setting TEMPLATE_DIRS

Usually it would be done with something like:


import os
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(CURRENT_DIR, "templates")
)

but on Windows the path is shown for instance as: C:\\path\\to\\dir...
(with "\\" for escaping), so the right setting of the templates directory would be something like:


import os
CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(CURRENT_DIR, "templates").replace('\\','/')
)

which will end as setting the TEMPLATE_DIRS for "current_dir_of_settings.py/templates".

Friday, April 9, 2010

.vimrc mainly for Python & Clojure

When using e.g. Vimclojure or coding in python, I have found it useful (and for vimclojure - necessary) to have the following contents in my .vimrc; some of them come from the python's mailing list, others from the Vimlojure website:


set textwidth=80
set autoindent

set tabstop=4
set expandtab "turns tabs into whitespace

set shiftwidth=4 "indent with autoindent
filetype plugin indent on

syntax enable
set wrap

" Settings for VimClojure
let g:clj_highlight_builtins=1 " Highlight Clojure's builtins
let g:clj_paren_rainbow=1 " Rainbow parentheses'!
let clj_want_gorilla=1
let vimclojure#NailgunClient = "/home/cdiem/drugi/clojure/vimclojure-2.1.2/ng"

Wednesday, April 7, 2010

Image to ascii art in Python

After seeing the image-to-ascii post at bestinclass.dk I wanted to try something similar. Well, there are no lisp macros here, but still I'm delighted with the result. The code here is under the BSD Licence.

The algorithm is as follows:

1. Scale an image to some width
2. Convert the image to greyscale
3. Traverse the image pixel by pixel and choose from a list of predefined values the value corresponding to the greyscale of every pixel.

I have used the icy-ubuntu image from:
[http://gnome-look.org/content/preview.php?preview=1&id=73000&file1=73000-1.jpg&file2=73000-2.jpg&file3=73000-3.jpg&name=Icy+Linux] for my tests:


Converted to ascii art it looked like:


I have also tried them with the image of S. Balmer from the abovementioned website (bestinclass.dk), printing at 40 characters per line, and the results were as follows:


And the code:

Project Euler 11 in Python

I've been playing with some stuff from Project Euler lately; project euler's problem 11:
(http://projecteuler.net/index.php?section=problems&id=11)
took me some time to figure out. It still bothers me that I write the code in Python in non-pythonic way (i.e. too verbose).
After checking the solutions page, there were solutions in Python in about 10 lines of code...
Here's the code anyway.

Saturday, April 3, 2010

Writing a program similar to the unix 'fortune'

A simple program to read fortunes from "fortunes.txt" and print a random one to the screen.


import fileinput
import random

# loading the fortunes file as elements of a list;
# using fileinput we don't load the whole file in memory
fortunes = []
for line in fileinput.input(['fortunes.txt']):
if not line.strip() == '': # skip blank lines
fortunes.append(line)

# printing a random fortune:
print "\n" + fortunes[random.randint(0, len(fortunes)-1)]



The file "fortunes.txt" would have for instance the following contents (or e.g. these), separated by blank lines:


Never take life seriously. Nobody gets out alive anyway.

Behind every successful man is a surprised woman. - Maryon Pearson

Hard work never killed anybody, but why take a chance?

I am not a vegetarian because I love animals; I am a vegetarian because I hate plants. A. Whitney Brown

Friday, November 27, 2009

Basic SVN tutorial

While I was searching for a very quick and good SVN tutorial, I found this one:

http://wordpress.org/extend/plugins/about/svn/

which had just the basics covered for starting within minutes. Cool!

Tuesday, September 8, 2009

Haskell SOE

The Haskell SOE (School of Expression) is absolutely awesome. The lecture slides are on this address, the solutions are on this address. One of the best (and most entertaining) books on Haskell I have found so far.

Friday, August 14, 2009

More on the project with the site redesign

After a discussion on ubuntuforums.org, I decided to follow as many of the advices I was given as possible. I have prepared a new header image; the .jpg seemed a bit better than the .png one to me, and it was a bit smaller in size as well (23 vs 34 kb). Here it is:



Also did some other minor changes (fonts, etc). More to come, but later.

Thursday, August 13, 2009

The redesign of the website seems complete to me

So, a complete redesign of the website was made, absolutely from scratch. For the header I used a part of a GPL'd picture from gnome-look.org. The colour scheme is from colourlovers.com. All other stuff/design is by me - created in a text editor, tested in Firefox 3+/Opera 9.64+/IE 7+ on Windows and Linux - and on all of these browsers it looks ok to me.

I'm still performing some minor improvements, but I think this is what it will look like from now on.

This is how the old design looked like:


I felt absolutely nothing wrong with it, but I wanted to create one by myself. Also, the newer one seems to be more W3C-validator-friendly (about 70 errors vs over 1000 for the older design - and almost all of these 70 errors come because of the upper banner that blogspot.com puts on every website in their domain - hopefully one day I will learn how to fix them ;]).
As well as the newer one:

Categories

code (15) fun (2) linux (31) miscellaneous (9) music (8)

About me

I'm Adrian and this is my blog. Here I usually write about technical stuff (mostly about Linux).
Copyright: BY-NC_SA, Author: aeter