Saturday, April 30, 2011

Lausanne 20K race, TODAY!!

After more than 12 weeks following an intense half marathon training program (thanks Hal), the day has finally arrived.

The 2oK of Lausanne it is not exactly a half marathon, as you may suspect from its name. However, is the first big running event of the summer season in the Swiss Riviera, and I can't wait!!!!!!!

After this race I want to try a few mountain ones, starting with one in Vallorbe. If the experience is good, there are plenty of mountains and mountain races in the area :).

I'll post my time as soon as I know it :)

Cheers

Labels:

Friday, April 29, 2011

My first android post

My first post from my new android mobile. Also adding a funny picture


Sunday, April 17, 2011

Kata: Palindrome generation in Python

I took this Kata from topcoder.com. My initial solution:

def isPalindrome(n):
       return n == reverseNumber(n)
      
def generatePalindrome(n):
       while not isPalindrome(n):
               n = n + reverseNumber(n)
       return n

def reverseNumber(n):
       res = 0
       while n > 0:
               res =  (res*10) + (n%10)
               n = n/10
       return res
      
n = 265
print reverseNumber(n)
print n
print isPalindrome(24543)
print generatePalindrome(24543)

Another faster isPalindrome implementation. It is still linear but much faster since it does not require reversing the string. It also uses less memory, since only N/2 characters need to be copied into a new structure (Stack like):


def isPalindrome(word):
    size = len(word)
    if size == 0:
        return False
    
    stack = list(word[0:size/2])
    start = size/2 + size%2
    
    for c in word[start:]:
        if c != stack.pop():
            return False
    return True
        
def tests():
    assert isPalindrome("a")
    assert not isPalindrome("ab")
    assert isPalindrome("aba")
    assert isPalindrome("abba")
    
tests()

Saturday, April 16, 2011

Project Euler N° 2, in Ruby

http://projecteuler.net/index.php?section=problems&id=2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.



def fib(limit)
f_n_1 = 1
f_n_2 = 1
curr = [1,1]
while (f_n_1 + f_n_2) <= limit
curr.push(f_n_1 + f_n_2)
f_n_2 = f_n_1
f_n_1 = curr.last
end
return curr
end

Limit = 4000000
allFib = fib(Limit)
iter = allFib.size/3
puts allFib
puts iter

sum = 0
for i in 0..iter
if i*3 + 2 > allFib.size
break
end
sum += allFib[i*3 + 2]
end

puts sum

Friday, April 15, 2011

Project Euler problem N° 1 in Ruby

http://projecteuler.net/index.php?section=problems&id=1



def addMultiplesOf(n,range)
nb_of_multiples = 1 + range/n
res = n*((nb_of_multiples/2)*(nb_of_multiples-1))
return res
end

N = 999

puts addMultiplesOf(3,N) + addMultiplesOf(5,N) - addMultiplesOf(15,N)


Project Euler is avery interesting site proposing dozens of very amusing and challenging mathematical and computational problems. Give it try, it is well worth your time.

Saturday, April 09, 2011

Swapping to numbers without an auxiliary variable

Hi,

following with my current geek/nerd stuff writing spree, today I want to introduce a very simple way to swap to numbers, without a third variable

a = a + b
b = a - b
a = a - b

Nice and simple, the explanation is dead simple and obvious, but anyway here it goes:

a = a + b , nothing to say here
b = a - b = a + b - b = a
a = a - b = a + b - a = b

There is another nice, yet less intuitive way to do this, by using XOR:

a = a XOR b
b = a XOR b
a = a XOR b

Think about it, and enjoy.

Saturday, April 02, 2011

Simple Event Manager in Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Event
USER_CONNECTED = 1
SERVER_CONNECTED = 2
end

class Listener
def onEvent(event,params=[])
puts event
end
end

class EventManager
def initialize()
@listenersMap = Hash.new
end

def subscribe(event,listener)
listeners = @listenersMap[event]
if listeners == nil
@listenersMap[event] = Array.new
else
if @listenersMap[event].include?(listener)
return
end
end
@listenersMap[event].push(listener)
end

def notify(event)
if @listenersMap[event] != nil
@listenersMap[event].each do |lis|
lis.onEvent(event)
end
end
end
end

puts "Initializing the event manager"
lis = Listener.new
manager = EventManager.new
manager.subscribe(Event::USER_CONNECTED,lis)
manager.subscribe(Event::USER_CONNECTED,lis)
manager.notify(Event::USER_CONNECTED)