Archive

Posts Tagged ‘fun’

Generating Combinations with Ruby and Recursion

September 16th, 2009

Hello fellow Rubyists :)

The following code is a recursive combination generator written in Ruby. Given an array of objecs/numbers/..etc, it will return an array of all possible combinations . This originally started out using two functions that contained looping logic in them. It worked well, however I just wanted to stretch myself a bit and try my hand at doing it recursively. Below is the result of my efforts. I hope this helps with anything you may be working on… enjoy!

#javazquez.com

#tail is a clone because of Ruby's passing by reference and shift

#returns an unsorted array

def combination(ary,head=[])
  return ary if(ary==[] )
  head << ary.shift
  tail = ary.clone
  tmp=[]
 if(tail.length>=1)
    tmp+= combination(tail[1..(tail.length)],head.flatten)+
                       combination(Array(tail[-1]),head.flatten)
    tmp+=combination(tail[2..(tail.length)],head.flatten) if(tail[2])
  else
    tmp += combination(tail,Array(head[0]))
  end
  tmp += combination(tail, []) +combination(ary,head.flatten)
  return (tmp << head).uniq
end

combination([1,2,3,4]).sort.each{|it| puts it.inspect  }
#  combination(['A','B','C','D','E','F','G','H','I','J']).sort.each{|it|  puts it.inspect}
#  combination(['A','B','C','D']).sort.each{|row| puts row.inspect}

----output--

[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
[1, 2, 4]
[1, 3]
[1, 3, 4]
[1, 4]
[2]
[2, 3]
[2, 3, 4]
[2, 4]
[3]
[3, 4]
[4]

Ruby, code , , , , , , , , , ,

Ruby to Python Primer

December 16th, 2008

If your like me, you bounce around between languages a lot. Lately, I have been writing python code. It’s not Ruby :D , but it can get the job done. Here is a quick list of similarities between the two languages. I hope it helps… don’t forget to this list in the comments section ;)

#-----find object methods-----
s="hello, I am a string"

#ruby
puts s.methods

#python
print dir(s)

#find out more about a method using python
help(s.split)

#-----view object's class-----
#ruby
s.class

#python
s.__class__

#------Iterate hashes-------

#ruby
h.each{|key,value| puts "#{key}, #{value}"}

#python
for key,value in h.iteritems():
print key, value

#---ternary operators

#ruby
condition ? var = x : var = y

#python.. not exactly an operator, but you get the meaning
#---- var = y if condition is false
var = x if condition else y

#----lengths------
#ruby
s="hello, I am a string"
puts "Length of string is #{s.length} or #{s.size}"

h={:one=>2,:three=>4}
puts "Length of hash is same as string, #{h.length} or #{h.size} "

#python
print("This is the length of a string %s" % len("string"))
print("number of key/value pair= %d" % len({'one':1,'two':2}))

#---slicing lists/arrays
l=[1,2,3,4,5]

#ruby
l[1..3] #=>[2,3]

#python
l[1:3] #=>[2,3]

#--print string multiple times-----

#ruby
4.times{print "hello"} #=> hellohellohellohello

#python
print("hello" * 4) #=> hellohellohellohello

Ruby, Uncategorized, code, python , , , , ,

BarCamp Omaha

July 1st, 2008

I have just been informed/invited to Omaha’s BarCamp! According to the site, this is a “unconference born from the desire for people to share and learn in an open environment.”

The list of topics that have been submitted thus far are already enough to get any developer’s inner geek super-charged. At this point I am not sure what I will talk about, but here are a few ideas.

  1. uploading/updating multiple models in one form (ROR)
  2. Groovy and flickr
  3. Action Script 3 concepts
  4. Linux Administration
  5. Setting up Twiki

Any suggestions or votes on what I could bring to the BarCamp would be awesome. If you can make it, I suggest checking this event out! After all, you don’t want to be sitting there listening to you fellow IT buddies raving about the great time they had learning at BarCamp…right?

Linux, code , , ,