Friday, November 05, 2010

Find the Difference

Ruby is cool, it can write some code like this:
 arrayA = ['a345','v123','765','b666', '333', 'aaaa'];  
 arrayB = ['234','b666','777','v123','c123'];  

 puts arrayA - arrayB;  
 puts arrayB - arrayA;  

Or if you want to practice your head, you can type the code like me :
 def getdiff( array1, array2)  

  array1.sort!  
  array2.sort!  

  a = array1.pop  
  b = array2.pop  

  while (a != nil && b!= nil)  

    if a == b  
     a = array1.pop  
     b = array2.pop  
     next; 
 
    elsif a < b  
     puts b;  
     b = array2.pop();  
     next;  

    elsif a > b  
     puts a;  
     a = array1.pop();  
     next;
  
    end  
  end  

  while(b == nil && a != nil)  
    puts a  
    a = array1.pop();  
  end  

  while(a == nil && b != nil)  
    puts b  
    b = array2.pop();  
  end  

  puts "Mission complete!"  

 end  

 arrayA = ['a345','v123','765','b666', '333', 'aaaa'];  
 arrayB = ['234','b666','777','v123','c123'];  

 getdiff(arrayA, arrayB);  

No comments:

Post a Comment