Frequency of occurrence

Given an sequence of integers, return the integer that occurs the most and its frequency of occurrence, as well as the integer that occurs the median number of times and it’s frequency.

 

For example [5,1,1,3,3,3] would have the number 3 occurring 3 times (mode) and would have the number 1 occurring 2 times which is the median number of times.
       Code:
def max_occurence_and_median data
  return false if data.count <1  #return false if array is empty

  h_data = Hash.new(0)

  data.each{|x| h_data[x] +=1}

  h_data = h_data.sort_by { |key, value| value}.reverse.to_h

  max_occurent_value, max_occurence = h_data.first
  median = find_median(h_data.values)
  median_value = h_data.key(median.to_i)

  return"max occurent value #{max_occurent_value} occurence #{max_occurence}, Median #{median} and median  value is #{median_value}"
end

def find_median(arr)
  len = arr.length
  (arr[(len - 1) / 2] + arr[len / 2]) / 2.0
end


puts "Input: [5,5,5,5,1,1,1,1,1,3,3,3,3,3,3,3]"
print "Output: "
data = [5,5,5,5,1,1,1,1,1,3,3,3,3,3,3,3]
puts max_occurence_and_median(data)

puts "Input [5,1,1,3,3,3]"
print "Output: "
data = [5,1,1,3,3,3]
puts max_occurence_and_median(data)

 Output:

###############################################################################################

Input: [5,5,5,5,1,1,1,1,1,3,3,3,3,3,3,3]

Output: max occurent value 3 occurence 7, Median 5.0 and median value is 1

********************************************

Input [5,1,1,3,3,3]

Output: max occurent value 3 occurence 3, Median 2.0 and median value is 1

#################################################################################################

Money Collection

Mr.savani is collecting a money from n students for a trip,  like collect $1 from 1st student, $3 from 3rd student and so on.. but it should not include k (unlucky number) into sum. if it includes k into sum, then ignore that student

 

Example:

n = number of student, k = unlucky number
i/p: 2,2, o/p: 3,
i/p: 3,3, o/p: 5, (could be 4 as well but find max sum)

Solution: Using Discriminant maths  D = b2 – 4ac

def maxMoney(n, k)
    sum = (n * (n + 1)) / 2
    sum -= 1 if Math.sqrt(1+8*k) % 1 == 0
    return sum
end

puts maxMoney(2,2)   # o/p 3
puts maxMoney(2,1)   # o/p 2
puts maxMoney(3,3)   # o/p 5
puts maxMoney(2000000000,4000000000000000)

Segregate 0s and 1s in an array

You are given an array of 0s and 1s in random order. Segregate 0s on left side and 1s on right side of the array. Traverse array only once.

Input array   =  [0, 1, 0, 1, 0, 0, 1, 1, 1, 0] 
Output array =  [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

Solution:

def solution(arr)
  left = 0
  right = arr.length - 1

  while left < right do
    while (arr[left] == 0 && left < right) do
      left += 1
    end

    while (arr[right] == 1 && left < right) do
      right -= 1
    end

    if (left < right)
      arr[left] = 0
      arr[right] = 1
      left += 1
      right -= 1
    end
  end
  return arr
end

arr = [0,1,0,1,1,1,0,0,0]
print solution(arr)

How to use Geolocalise gem

Welcome to Geolocalise gem! A Gem which help you to find out current city,state,country,postal-code and country-code with simple methods call.

Links:

Github link : Geolocalise
RubyGem link : Geolocalise

Installation

Add this line to your application’s Gemfile:

gem 'geolocalise'

And then execute:

$ bundle

Or install it yourself as:

$ gem install geolocalise

Usage

In your class –

$ require 'geolocalise'

There are mainly five helpers available for Geolocalise usage. To get city name from latitude and langitude

Geolocalise.get_city("19.07598, 72.8776559")
#=> "Mumbai"

To get state name form city or co-ordinates

Geolocalise.get_state("19.07598, 72.8776559")
#=> "Maharashtra"

Geolocalise.get_state("Mumbai")
#=> "Maharashtra"

To get country name form city or co-ordinates

Geolocalise.get_country("19.07598, 72.8776559")
#=> "India"

Geolocalise.get_country("Delhi")
#=> "India"

To get country code form city or co-ordinates

Geolocalise.get_country_code("19.0759837, 72.8776559")
#=> "IN"

Geolocalise.get_country_code("New York")
#=> "US"

To get postal pode form city or co-ordinates

Geolocalise.get_postal_code("19.07598, 72.8776559")
#=> "400070"

Geolocalise.get_postal_code("New York")
#=>"10007"

Important ruby gem list

To Create admin panel:
ActiveAdmin :  rubygem | github
Activeadmin-axlsx :    rubygemgithub

AES encryption / decryption Ruby gem
Aescrypt : rubygem github

Mailing Functionality:
Letter Opener : rubygemgithub

Calculate difference between two given time:
Time Diff : rubygem github
Time Difference : rubygemgithub

To check the quality of rails code:
Rails best practices : rubygemgithub

To manage performance of application:
Newrelic : rubygems github

For reading and writing zip files:
Rubyzip :  rubygem | github

For background processing:
sidekiq : rubygem | github
Sidekiq-status : rubygem | github
daemons : rubygem | github (Ruby scripts to be run as a daemon and to be controlled by simple start/stop/restart commands)

For short code in view part:
Slim : rubygem | github
haml : rubygem | github
jade : rubygem | github

Exception Notifier Plugin for Rails:
whoops_rails_logger : rubygem | github
exception_notification : rubygem | github

Tracing/debugging the code:
better_errors : rubygem | github
meta_request : rubygem | github
binding_of_caller : rubygem | github
quiet_assets : rubygem | github (Quiet Assets turns off the Rails asset pipeline log. This means that it suppresses messages in your development log)

Notification:
growl : rubygem | github
ruby_gntp : rubygem | github

Font (size, color, drop shadow, and anything that can be done with the power of CSS):
font-awesome-rails : rubygem | github

Testing :
rack-test : rubygem | github
rspec : rubygem | github
rspec-mocks : rubygem | github
guard : rubygem | github
guard-rspec : rubygem | github
rb-fsevent : rubygem | github
fakefs : rubygem | github
cucumber : rubygem | github
aruba : rubygem | github
shoulda-matchers : rubygem | github
shoulda-callback-matchers : rubygem | github
factory_girl_rails : rubygem | github
database_cleaner : rubygem | github
poltergeist : rubygem | github
launchy : rubygem | github
faker : rubygem | github