25/08/2009

Random hex string generator in ruby

I was looking around for a random hex string generator (pseudo random is fine for me too) and I wasn't too happy with what I found (source : http://snippets.dzone.com/posts/show/491 ):

def newpass( len )
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    return newpass
end

I tried to come up with something more to my liking, on my own but didn't fare that much better:

def rand_hexstring(length=8)
  (1..length).inject("") {|x,i|
    x+=((rand()*128)).to_i.chr
  }.unpack("H*").to_s.slice 0..length
end

I had thought of filtering it at some point and came up on a blog ( http://www.rubyrailways.com/rubys-most-underused-keyword/ ) with the following :

  (0..9).map{rand(?z).chr[/[^_\W]/]||redo}*"" 

While in the end I had no use for filtering, and shed the nice redo trick, it made me realize I could improve on my answer by using map instead of inject :

  def rand_hexstring(length=8)
    ((0..length).map{rand(256).chr}*"").unpack("H*")[0][0,length]
  end

What this does is : for each element of the range ( map ), create a one char string ( rand(256).chr ) join the resulting array ( *"" ) unpack it to hexes, get the only element ( [0] ) of the resulting array and slice it to the requested length

16/08/2009

Eclipse 3.5 and Glassfish

I just tried to install a new instance of galileo and got bitten again:/ try to install glasfish support in galileo following the obvious path and you will be presented with a totally cryptic error message that basically tells you to go to hell with your request. ( I remember reading somewhere that p2 error messages are beyond the understanding of mortalsm I can only concur ).
Anyway, it looks like I am not the only one having trouble installing Glassfish v3 support for my favorite Java IDE : http://hardikmehta.wordpress.com/2009/06/29/glassfish-plugin-on-eclipse-3-5-galileo To sum it up, install glassfish support from the following update site: https://ajax.dev.java.net/eclipse/ and you will be setup in a sec

15/08/2009

JavaCamp Paris 5ème édition

Pour sa cinquième édition le JavaCamp paris prendra la forme d'un grand picnic au jardin des tuileries, ça se passe le jeudi 27 Août à 18h30. Le barcamp c'est un format totalement informel, l'inscription sert surtout a annoncer les sujets qui nous interessent, de façon que l'on sache un peu si on trouvera des gens pour discuter de ça pendant le barcamp. Comme cette fois on ne sera pas chez google, il n'y aura pas de buffet, il faut donc penser a amener a manger et a boire. En échage : pas de contrainte de place, je doute que nous remplissions la totalité du jardin des tuileries :D

10/08/2009

Cool iris

En fouinant dans les addons firefox à la recherche de l'addon delicious, je suis retombé sur cooliris. Il est maintenant disponible pour ma plateforme (linux 64bits) Je ne connais pas de meilleur facon de visionner une grande quantité d'images rapidement, pour moi qui passe beaucoup de temps a "lurker" sur flickr, cet addon c'est le bonheur. Merci cooliris!

06/08/2009

Impressions of TDD

A little think back on yesterday's TDD experiment. I left the tests in the order I wrote them. If you look closely enough, you will see that the test test_get_open_lockers_4_est_1_et_4 couldn't work until the last two tests were written. What that means is that I tried to take too big a step and wrote a test I couldn't make pass. I wrote more tests, leaving a failing test behind, fixing first the folowing tests and then that one. Was that the correct answer to too taking too big a step ?

Nerd, Jocks ans Lockers

I am a fanatic reader of the Daily WTF but until now I had never taken the time to try and solve a problem from the programming praxis category. So this time, I have gone and done it for the Nerds, Jocks and locker problem. the following is my entry done trying to follow TDD. test_locker_solver.rb:
require 'test/unit'
require 'locker_challenge'

class TestLockerSolver < Test::Unit::TestCase
 def test_prime_set_number_de_1_est_1_1() 
  solver=LockerSolver.new
  assert_equal [1,1], solver.get_prime_set(1)
 end
 def test_prime_set_number_de_2_est_1_2() 
  solver=LockerSolver.new
  assert_equal [1,2], solver.get_prime_set(2)
 end
 def test_prime_set_number_de_3_est_1_3() 
  solver=LockerSolver.new
  assert_equal [1,3], solver.get_prime_set(3)
 end
 def test_prime_set_number_de_4_est_1_2_2() 
  solver=LockerSolver.new
  assert_equal [1,2,2], solver.get_prime_set(4)
 end

 def test_get_open_lockers_1_est_1
  solver=LockerSolver.new
  assert_equal [1], solver.get_open_lockers(1)
 end
 
  def test_get_open_lockers_4_est_1_et_4
  solver=LockerSolver.new
  assert_equal [1,4], solver.get_open_lockers(4)
 end
 
 def test_count_primes_1_est_1x2
   solver=LockerSolver.new
   
   assert_equal({1 => 2}, solver.count_primes(1))
 end
  def test_count_primes_4_est_1x1_2x2
   solver=LockerSolver.new   
   assert_equal({1 => 1, 2=>2}, solver.count_primes(4))
 end
end
and locker_solver.rb:
class LockerSolver
 def get_prime_set(n=1)
  i=2
  while n%i!=0 and i<=n  
   i=i+1
  end
  i>=n ? [1,n] : [i,get_prime_set(n/i)].flatten.sort  
 end
 
 def count_primes(n=1)
  pset=get_prime_set(n)
  map=pset.inject({}) { |memo,n| memo[n].nil? ? memo[n]=1 : memo[n]+=1; memo }
 
 end
 
 def get_open_lockers(n)
  r=[]
  (1..n).each do |l|
    h=count_primes(l).delete_if {|key, value| key<2}
    isOpen=true
    h.each do |prime,count|
      isOpen &= (count%2==0)
    end
    isOpen ? r << l : false
  end
  r
 end
end