Archive for the ‘hacks’ Category

Gnome menu icons

Thursday, February 25th, 2010

I’ve lost half an hour wondering why my icons were not displayed beside the menu labels.
In the end I found the solution:

gconftool-2 --type bool --set /desktop/gnome/interface/menus_have_icons

Lorem Ipsum

Thursday, February 4th, 2010
#!/usr/bin/env bash

cat << EOF
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
EOF

Copy it into a file, and make it executable! Enjoy!

svn-tools: mysvn

Monday, January 11th, 2010

Scenario: subversion repository installed on a home-server. Sometimes you need to interact with it from the L.A.N. and sometimes from internet.

This script other then being a wrapper for svn, checks whether the working copy needs to be relocated or not before the operations.

#!/usr/bin/env bash

GATEWAY_IP=192.168.0.254
GATEWAY_MAC=00:14:6c:ab:d2:ca
REPO_LOCAL=192.168.0.4
REPO_REMOTE=code.matteolandi.net

arp -a | grep $GATEWAY_MAC > /dev/null
if [ $? -eq 0 ]; then
 source=$REPO_REMOTE
 destination=$REPO_LOCAL
else
 source=$REPO_LOCAL
 destination=$REPO_REMOTE
fi

url_before=$(svn info | grep URL | cut -d ' ' -f 2)
url_after=${url_before/$source/$destination}

if [ "$url_before" != "$url_after" ]; then
 echo "Relocating ${url_before} -> ${url_after}"
 svn switch --relocate $url_before $url_after
 if [ $? -ne 0 ]; then
   exit 1
 fi
fi

svn $@

I know you could create a new entry inside /etc/hosts and use it depending on the situation, but easy solutions are not my best!

Obviously you will find it inside the hacks/svn-tools repository.

svn-tools: svn-feed update

Monday, January 4th, 2010

Few changes regarding the script for the creation of the rss feeds of the commits. Indeed, I decided to create a new one from scratch using python. With this you will be able both to update the feeds and create them from the origins of your repositories.

You can find the tool inside the hacks repository.

Python: Generate fake B.S.O.D.

Friday, November 27th, 2009


I’ve just created a script which simulate a bsod by creating a fullscreen window and placing the right text in the right position.

In order to make it closer to reality, it’s better for you to download and install this font.

And now the script: bsod

Python: for .. else

Saturday, September 12th, 2009
r = [1, 3, 5, 11, 55, 89]

for elem in r:
  if elem == 15:
    print 'Found an element equal to 15'
    break
else:
  print 'Given array does not contain any elements equal to 15'

Dynamic Programming: Maximum Subarray Sum

Monday, September 7th, 2009

Let’s say we are given an array of integers. What (contiguous) subarray has the largest sum? For example, if our array is [1, 2, -5, 4, 7, -2] then the subarray with the largest sum is [4, 7] with a sum of 11.

# read the array from stdin
vect = map(int, raw_input().split(' '))

# bounds of optimal subarray
(j, k) = (0, 1)

# left bound using for summing elements
l = 0

# sum of the elements of the optimal subarray
best = vect[0]

# sum of the elements from l to current position
sum = vect[0]

for i in xrange(1, len(vect)):
  sum += vect[i]
  if sum < 0: # selected subarray is not convenient
    l = i + 1
    sum = 0
  elif sum > best:
    best += vect[i]
    (j, k) = (l, i)

print vect[j:k + 1]

[ source: 20bits.com ]

Python: double for loop statement

Sunday, September 6th, 2009

Imagine you have a N x M matrix and you need to loop over each element of it.The first thing coming into my mind was to use a double for loop such as:

# ...
for i in xrange(N):
  for j in xrange(M):
    # do some action with element (i, j)
# ...

Now imagine to put this double-for statement inside a single-for one because you need to repeat the same action, let’s say, T times:

# ...
for t in xrange(T):
  for i in xrange(N):
    for j in xrange(M):
      # do some action with element (i, j)
# ...

Wait a moment: is all i have written necessary? Why should i need to generate all the M elements every-time? Here is a pretty solution:

def Cross(a, b):
  for i in a:
    for j in b:
      yield (i, j)

# ...
for t in xrange(T):
  for (i, j) in Cross(xrange(N), xrange(M)):
    # do some action with element (i, j)
# ...

[ source: Google Code Jam ]

Update: hacks repository

Tuesday, July 28th, 2009

I decided to move the svn-tools repository inside hacks.

hacks repository

Thursday, July 9th, 2009

I decided to create a repository containing the stuff i published without being part of a standalone repository; so this would be the shelter for scripts like pmv, rotating-cube, word-challenge-hacked ecc. ecc.

The name of the repository is hacks.