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"

Testing Rspec with Capybara and Selenium

How to set up Capybara and Selenium in ruby on rails application?

First of all add gems into gemfile:

gem 'capybara'
gem "selenium-webdriver"
gem 'chromedriver-helper'

After Adding new gem, its time to bundle install

bundle install

Now, We need to tell application that we are using capybara with Rspec testing, to do so we need to add following code into rails_helper.rb file

require 'capybara/rspec'
require 'capybara/rails'

In order to run test cases in particular browser, need to set default browser.To Set default browser for test suits, Add below code into rails_helper.rb

Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser =&gt; :chrome)
end

Now we are ready to write test suits for our application but before that if any Error raised like driver not found , to resolve that we would need to install Chromedriver.exe

http://chromedriver.storage.googleapis.com/index.html?path=2.15/
Move downloaded file Chromedriver.exe to C:\ruby\bin folder

This is all about how to set up capybara with selenium, If you are looking for Example, Here is github link Cucumber, capybara, selenium testing

Please log an issue here.

Git log..!!!

If you are tired of this old and bored git log screen?

git_log_pld

So Its simple, if you want to change it, Just type in:

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

git_log_new

Or, if you want to see the lines that changed

git log -p

git_log_p-1

If you want to see one line commits, plus branching.

git log --oneline --abbrev-commit --all --graph --decorate --color

 

if you are feeling like you need something a bit different.

git log --decorate --graph --abbrev-commit --date=relative

Hope you like it 🙂

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

How to install angularJS in windows/linux

HTML is great for declaring static documents, but it falters when we try to use it for declaring dynamic views in web-applications. AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.

AngularJS is a toolset for building the framework most suited to your application development. It is fully extensible and works well with other libraries.

AngularJS Installation steps

step 1: Install nodeJS:

got o http://nodejs.org/

download & install nodeJS

(open Command prompt)
step 2: Install yeoman/generator webapps

npm instal -g yo

npm install -g generator-webapp

npm install -g generator-angular

step 3: Install Additional packages

yo webapp

grunt

grunt serve