Friday, 17 May 2019

Find the uniq elements in an array without using ruby default method?

a = [1, 2, 3, 4, 5, 6, 4, 3, 2, 1, 4, 5, 4, 34, 76]

Solution:

b = Hash.new 0

a.each do |k|
  b[k] += 1
end

b.keys

Output:
=> [1, 2, 3, 4, 5, 6, 34, 76]

How to remove the duplicate pair in an array using ruby?

Scenario 1
 
a = ['R1,R2','R3,R4','R5,R6', 'R2,R1', 'R4,R3']

Result:

a.uniq {|x| x.split(",").sort.join(',')}

Output => ["R1,R2", "R3,R4", "R5,R6"]


Scenario 2

a = ['R1,R2','R3,R4','R5,R6', 'R2,R1', 'R4,R3']

Result:  

require 'set'
a.uniq {|x| Set.new(x.split(','))} 

Output => ["R1,R2", "R3,R4", "R5,R6"] 

Friday, 3 May 2019

Display the * values in triangle shape in ruby

for i in 1..10
  for j in 1..i
    print ('*')
  end
  print ("\n")
end


output:
*
**
***
****
*****
******
*******
********
*********
**********

Finding the repeated words count from an array in ruby

a = %w(hi this is rajkumar this is uthayaa this is testing for repeat and repeat string)
b = Hash.new 0
a.each do |w|
  b[w] += 1
end
puts b

output: => {"hi"=>1, "this"=>3, "is"=>3, "rajkumar"=>1, "uthayaa"=>1, "testing"=>1, "for"=>1, "repeat"=>2, "and"=>1, "string"=>1}

Finding Dividable by 2 values in an ruby array

a = [1,2,3,4,444,6,5,6,7,8,44,55,333,555,66,88]
b = []
a.each do |h|
  b.push(h) if (h%2) == 0
end
p b

output:=> [2, 4, 444, 6, 6, 8, 44, 66, 88]

Sequences of even numbers from an array in ruby

Finding the sequences of even numbers from an array in ruby


a = [1,2,3,4,444,6,5,6,7,8,44,55,333,555,66,88]
b = []
a.each_slice(2) do |l|
  if l && l[0] && l[0].even? && l[1] && l[1].even?
    b.push(l)
  end
  b
end
p b

output:  => [[444, 6], [66, 88]]


Wednesday, 1 May 2019

Jar Puzzle Problem Resolved in Ruby By Rajkumar

Jar Puzzle Problem in Ruby



In puzzle.rb.

require './get_array.rb'

class Puzzle
def initialize(jar_capacity, initial_state, final_states)
#maximum capacities of jars set by the users
@max_jar, @medium_jar, @small_jar = jar_capacity
#initial state configs set by the users
@start_states = initial_state
#final states configs set by the users
@final_states = final_states
@results = Array.new
@visited_memory = Hash.new
end

def display_data
visit_states(@start_states)
puts '=====Your Results with sequences====='
if !@results.empty?
@results.reverse.collect {|x| p x}
else
puts 'No Path Found'
end
end

def visit_states(jar_states)
jar_a, jar_b, jar_c = jar_states

#compared with final states given by the users
if(jar_a == @final_states[0] && jar_b == @final_states[1])
@results.push(jar_states)
return true
end

#check the states in already visited
if @visited_memory.key?([jar_a, jar_b, jar_c])
return false
end

@visited_memory[[jar_a, jar_b, jar_c]] = 1

#empty the jar_a
if(jar_a > 0)
#empty jar_a into jar_b
if(jar_a + jar_b <= @medium_jar)
if( visit_states([0,jar_a + jar_b,jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a - (@medium_jar - jar_b), @medium_jar, jar_c]) )
@results.push(jar_states)
return true
end
end

#empty jar_a into jar_c
if(jar_a + jar_c <= @small_jar)
if( visit_states([0,jar_b,jar_a + jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a-(@small_jar - jar_c), jar_b, @small_jar]) )
@results.push(jar_states)
return true
end
end
end

#empty the jar_b
if(jar_b > 0)
#empty jar_b to jar_a
if(jar_a + jar_b <= @max_jar)
if( visit_states([jar_a + jar_b, 0, jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([@max_jar, jar_b - (@max_jar - jar_a), jar_c]) )
@results.push(jar_states)
return true
end
end

#empty jar_b into jar_c
if(jar_b + jar_c <= @small_jar)
if( visit_states([jar_a, 0, jar_b + jar_c]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a, jar_b - (@small_jar - jar_c), @small_jar]) )
@results.push(jar_states)
return true
end
end
end

#empty jar_c
if(jar_c > 0)
#empty jar_c into jar_a
if(jar_a + jar_c <= @max_jar)
if( visit_states([jar_a + jar_c, jar_b, 0]) )
@results.push(jar_states)
return true
end
else
if( visit_states([@max_jar, jar_b, jar_c - (@max_jar - jar_a)]) )
@results.push(jar_states)
return true
end
end

#empty jar_c into jar_b
if(jar_b + jar_c <= @medium_jar)
if( visit_states([jar_a, jar_b + jar_c, 0]) )
@results.push(jar_states)
return true
end
else
if( visit_states([jar_a, @medium_jar, jar_c-(@medium_jar - jar_b)]) )
@results.push(jar_states)
return true
end
end
end
return false
end

end

puts "=============Please enter jar capacities e.g. 8,5,3================"
total_capacities = GetArray.convert_array(gets.chomp)
puts "=============Please enter Initial configuration of jars e.g.=======8,0,0=="
initial_states = GetArray.convert_array(gets.chomp)
puts "=============Please enter the final configuration of jars==== e.g. 4,4,0========"
final_states = GetArray.convert_array(gets.chomp)
puzzle = Puzzle.new(total_capacities, initial_states, final_states)

puzzle.display_data


In get_array.rb

class GetArray
  def self.convert_array(inputs)
    if inputs != ''
      begin
        if inputs.split(',').map(&:to_i).size >=3
          inputs.split(',').map(&:to_i)
        else
          raise 'Your input should be minimum three values'
          exit(1)
        end
      rescue Exception => e
        puts e.message
      end
    end
  end
end


Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...