Posts

Showing posts with the label ruby array

Prime Number with out Prime Class

a = [1,4,5,12,9,5,3,5] class Array   def prime_numbers     self.select(&:prime?)   end end class Integer   def prime?     return if self <= 1     (2..Math.sqrt(self).ceil).none? { |i| (self % i).zero? }   end end  a.prime_numbers => [5, 5, 3, 5]

Conference Track Management - Ruby solution

I got to solve this problem as the part of an interview process. It wont look good if I paste the whole code here. So just sharing the link to my gitbub account. Conference Track Management

Ruby Count, Length & Size

These methods can be applied on Arrays, Hashes or Objects.  Here I'm just trying to illustrate the difference between count, length and size. Array#length Basically length method returns number of elements in the array. Example, suppose we have array a rr as, a rr = [1,2,3,4,5] Calling Array#length method on array will give result as, arr.length => 5 Array#size Array#size is just an alias to the Array#length method. This method executes same as that of Array#length method internally. Example, arr.size => 5 Array#count It has more functionalities than length or size. This method can be used for getting number of elements based on some condition . Unlike length/size, we can pass block or an argument to count. This can be called in three ways: Array#count => without condition, Returns number of elements in Array Array#count(n) => passing value, Returns number of elements having value n in Array Array#count{|i| i.zero?} =...