Amazon Affiliate

Monday 27 June 2016

How to make CSS Triangle or Caret?

HTML code is :

<div class=up-arrow></div>
<div class=right-arrow></div>
<div class=left-arrow></div>
<div class=down-arrow></div>

CSS code is :

.up-arrow{
  width: 0;
  height: 0;
  border: 20px solid transparent;
  border-bottom-color: red;
}
.down-arrow{
  width: 0;
  height: 0;
  border: 20px solid transparent;
  border-top-color: red;
}
.left-arrow{
  width: 0;
  height: 0;
  border: 20px solid transparent;
  border-right-color: red;
}
.right-arrow{
  width: 0;
  height: 0;
  border: 20px solid transparent;
  border-left-color: red;
}

Friday 24 June 2016

Find roots of the quadratic equation in ruby.

# Roots of quadratic equation

class QuadraticEquationRoots
def initialize
puts "Equation will be in the format : ax^2 + bx + c"
puts "Enter the value of a :"
a = gets.strip.to_i
puts "Enter the value of b :"
b = gets.strip.to_i
puts "Enter the value of c :"
c = gets.strip.to_i
roots_of_quadratic_equations(a, b, c)
end

def roots_of_quadratic_equations(a, b, c)
d = ((b*b) - (4*a*c))
root_d = Math.sqrt(d.abs).round(2)
if d > 0
puts "Roots are real and different"
x1 = (-b + root_d)/ 2*a
x2 = (-b - root_d)/ 2*a
puts "Roots are #{x1.round(2)} and #{x2.round(2)}"
elsif d == 0
puts "Roots are real and same"
x1 = -b / 2*a
puts "Roots are #{x1}"
else
puts "Roots are imaginary"
puts "Roots are #{-b/2*a}+#{root_d/2*a}i, #{-b/2*a}#{-root_d/2*a}i"
end
end
end

roots = QuadraticEquationRoots.new

Thursday 23 June 2016

Find the three number from the array that sum to a target number.


# Method 1 :

class FindTripletNumbersThatSumToTargetNumber
  def initialize
    p "Enter the size of array :"
    array_size = gets.strip.to_i
    if array_size < 3
      puts "Array size should be greater than 3"
    else
      array = []
      for i in (0..array_size-1)
          p "Enter #{i+1} element :"
          array[i] = gets.strip.to_i
      end
      p "Enter the target number :"
      target_number = gets.strip.to_i
      find_3_Numbers_that_sum_to_target_number(array, array_size, target_number)
    end
  end


  def find_3_Numbers_that_sum_to_target_number(array, array_size, target_number)
    for i in 0..array_size-3
      j = i+1
      for j in j..array_size-2
        k = j+1
        for k in k..array_size-1
          if array[i] + array[j] + array[k] == target_number
            puts "Triplet is : #{array[i]}, #{array[j]}, #{array[k]}"
            return true
          end
        end
      end
    end
    return false
  end
end
number = FindTripletNumbersThatSumToTargetNumber.new

This is not an optimized solution as it's time complexity is O(n^3).

# Method 2 :

class FindTripletNumbersThatSumToTargetNumber
  def initialize
    p "Enter the size of array :"
    array_size = gets.strip.to_i
    if array_size < 3
      puts "Array size should be greater than 3"
    else
      array = []
      for i in (0..array_size-1)
          p "Enter #{i+1} element :"
          array[i] = gets.strip.to_i
      end
      p "Enter the target number :"
      target_number = gets.strip.to_i
      merge_sort array
      find_3_Numbers_that_sum_to_target_number(array, array_size, target_number)
    end
  end

  def merge_sort(array)
  n = array.length
  if n > 1
  mid = n/2
  lefthalf = array[0..mid-1]
  righthalf = array[mid..n-1]
  merge_sort lefthalf
  merge_sort righthalf
  i = j = k = 0
  while i < lefthalf.length and j < righthalf.length
  if lefthalf[i] < righthalf[j]
  array[k] = lefthalf[i]
  i = i+1
  else
  array[k] = righthalf[j]
  j = j+1
  end
  k += 1
  end

  while i < lefthalf.length
  array[k] = lefthalf[i]
  i = i+1
  k = k+1
  end

  while j < righthalf.length
  array[k] = righthalf[j]
  j = j+1
  k = k+1
  end
  end
  end

  def find_3_Numbers_that_sum_to_target_number(array, array_size, target_number)
    for i in 0..array_size-2
      j = i+1
      k = array_size-1
      for j in j..array_size-2
        while j < k
          if array[i] + array[j] + array[k] == target_number
            puts "Triplet is : #{array[i]}, #{array[j]}, #{array[k]}"
            return true
          elsif
            array[i] + array[j] + array[k] < target_number
            j += 1
          elsif array[i] + array[j] + array[k] > target_number
            k += 1
          end
        end
      end
    end
    puts "Triplet doesn't exist"
    return false
  end
end
number = FindTripletNumbersThatSumToTargetNumber.new

In this method we reduced the time complexity from O(n^3)  to O(n^2) simply by sorting the array first. Now this is optimized solution compared to above.

Monday 20 June 2016

Merge Sort in ruby.

# Merge Sort in ruby.

def merge_sort(array)
n = array.length
if n > 1
mid = n/2
lefthalf = array[0..mid-1]
righthalf = array[mid..n-1]
merge_sort lefthalf
merge_sort righthalf
i = j = k = 0
while i < lefthalf.length and j < righthalf.length
if lefthalf[i] < righthalf[j]
array[k] = lefthalf[i]
i = i+1
else
array[k] = righthalf[j]
j = j+1
end
k += 1
end

while i < lefthalf.length
array[k] = lefthalf[i]
i = i+1
k = k+1
end

while j < righthalf.length
array[k] = righthalf[j]
j = j+1
k = k+1
end
end
end

print array = [6,5,3,1,8,7,2,4]
merge_sort array
print array

Selection Sort in ruby.

# Selection Sort in ruby.

def selection_sort(array)
n = array.length-1
for i in 0..n-1
min = i
j = i + 1
for j in j..n
if array[min] > array[j]
min = j
end
end
if i != min
array[i],array[min] = array[min],array[i]
end
end
end

array = [6,5,3,1,8,7,2,4]
selection_sort array
print array

Bubble sort in ruby.

# Bubble Sort in ruby.

def bubble_sort(array)
for i in 0..array.length - 1
for j in 0..array.length - 2
if array[j] > array[j+1]
array[j],array[j+1] = array[j+1],array[j]
end
end
end
end

array = [6,5,3,1,8,7,2,4]
bubble_sort array
print array

Insertion Sort in ruby.

# Insertion Sort in ruby.

def insertion_sort(array)
for i in 1..array.length - 1
j = i
temp = array[i]
while ((j>0) and (array[j-1] > temp))
array[j] = array[j-1]
j -= 1
end
array[j] = temp
end
return array
end

array = [6,5,3,1,8,7,2,4]
insertion_sort array
print array