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

Debian shutdown at "ACPI: Critical trip point"

Right a few minutes ago my Debian executed a fine shutdown; in /var/log/syslog.log there is:


Apr 3 16:46:38 cdiemst kernel: [ 4336.685904] ACPI: Critical trip point
Apr 3 16:46:38 cdiemst kernel: [ 4336.685920] Critical temperature reached (85 C), shutting down.
Apr 3 16:46:38 cdiemst shutdown[7341]: shutting down for system halt
Apr 3 16:46:38 cdiemst init: Switching to runlevel: 0
Apr 3 16:46:40 cdiemst kernel: [ 4338.723411] Critical temperature reached (77 C), shutting down.
Apr 3 16:54:42 cdiemst kernel: imklog 3.18.6, log source = /proc/kmsg started.


After some research it became obvious, that:
1. Either the fan needs to be cleaned
2. Or there should be set a throttling option for the processors (as described by alioth at: http://alioth.debian.org/~fjp/log/posts/Preventing_overheating_of_my_hp2510p.html).

Checking at:


cdiemst:/proc/acpi/thermal_zone/TZS0# cat trip_points
critical (S5): 85 C
cdiemst:/proc/acpi/thermal_zone/TZS1# cat trip_points
critical (S5): 111 C


shows there is no processor-throttling option enabled with passive trip points. So I have tried with a workaround, changing /etc/sysfs.conf to contain:


class/thermal/thermal_zone0/passive = 80000
class/thermal/thermal_zone1/passive = 80000


as proposed by alioth in the abovementioned article. Waiting for the next temperature anomaly.

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

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