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

Friday 27 May 2016

Swap two variables in ruby.

#Swapping two variables in ruby :

puts "Enter first variable :"
a = gets.to_i
puts "Enter second variable :"
b = gets.to_i

puts "the value of a is :#{a}"
puts "the value of b is :#{b}"

puts "After exchanging the value :"

# Ist method

a,b = b,a
puts "the value of a is :#{a}"
puts "the value of b is :#{b}"

#2nd method using temporary variable

c = a
a = b
b = c
puts "the value of a is :#{a}"
puts "the value of b is :#{b}"

#3rd method using arithmetic operation

a = a+b
b = a-b
a = a-b
puts "the value of a is :#{a}"
puts "the value of b is :#{b}"

Saturday 30 April 2016

Staircase program in ruby.

# Input
# You are given an integer N depicting the height of the staircase.

# Output
# Print a staircase of height N that consists of # symbols and spaces.

p "Enter the height of staircase :"
n = gets.strip.to_i

# 1st Method :

def staircase(space , hash)
    puts " "*space + "#"*hash
end

for i in (1..n)
    staircase(n-i , i)
end

#2nd Method :

n.times{|i| puts ("#"*(i+1)).rjust(n)}

Monday 25 April 2016

How to change MAC address in linux?


First find the physical MAC address of your machine by running the following command :

$ ifconfig -a | awk '/HWaddr/ {print "interface: " $1 "/t MAC: " $NF}'
interface: eth0/t MAC: 4e:97:0e:52:29:13
interface: wlan0/t MAC: 02:ec:b4:e1:dc:31


The hexadecimal numbers in blue denote machine's MAC address.

Now, login as root in Linux or use sudo before each command and execute it. 

# ifconfig eth0 down
# ifconfig eth0 hw ether 3c:97:0e:11:21:71 
# inconfig eth0 up
# ifconfig eth0 | awk '/HWaddr/ {print "interface: " $1 "/t MAC: " $NF}'  

I have changed my Mac address to  "3c:97:0e:11:21:71".

Note : This is the temporary change. Once you reboot your machine, the operating system will change the MAC address to the default.  

Monday 11 April 2016

Calculate the product of maximum pair in an array in ruby.

class MaxPairwiseProduct
  def initialize
    array_elements = []
    puts "Enter the size of array :"
    array_size = gets.chomp.to_i
    for i in 0..array_size - 1
      puts "Enter the element of array"
      array_elements << gets.chomp.to_i
    end
    puts "Maximum Product is : #{getNaiveMaxProduct array_size, array_elements}"
    puts "Maximum Product is : #{getOptimisedMaxProduct array_size, array_elements}"
  end

# This is the native solution to find the product of maximum pair wise in an array.

  def getNaiveMaxProduct array_size, array_elements
    result = 0
    for i in 0..array_size - 1
      j = i+1
      for j in j..array_size - 1
        if array_elements[i] * array_elements[j] > result
          result = array_elements[i] * array_elements[j]
        end
      end
    end
    return result
  end

# This is the comparitively optimised solution than previous solution to find the product of maximum pair wise in an array.

  def getOptimisedMaxProduct array_size, array_elements
    max_index1 = -1
    max_index2 = -1
    for i in 0..array_size-1
      if array_elements[i] > array_elements[max_index1]
        max_index1 = i
      end
    end
    for j in 0..array_size - 1
      if (array_elements[j] != array_elements[max_index1]) && (array_elements[j] > array_elements[max_index2])
        max_index2 = j
      end
    end
    result = array_elements[max_index1] * array_elements[max_index2]
    return result
  end
end

max_product = MaxPairwiseProduct.new

Convert and print the 12-hour format time to 24-hour format in ruby.

# Input Format

# A single string containing a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM), where 01≤hh≤12.

# Output Format

# Convert and print the given time in 24-hour format, where 00≤hh≤23.

puts "Enter the time in 12-hour clock format :"
time = gets.strip
a = time.split(":")
if a[-1].gsub(/[^A-Za-z]/,"") == "PM" || a[-1].gsub(/[^A-Za-z]/,"") == "pm"
    a[0] = (12 + a[0].to_i).to_s
    a[-1] = a[-1].gsub(/[^0-9]/,"")
elsif a[-1].gsub(/[^A-Za-z]/,"") == "AM" || a[-1].gsub(/[^A-Za-z]/,"") == "am"
    a[-1] = a[-1].gsub(/[^0-9]/,"")   
end   
puts a.join(":")

Print the sum of the array's elements as a single integer in ruby.

# Input Format

# The first line contains an integer, N, denoting the size of the array.
# The second line contains N space-separated integers representing the array's elements.

# Output Format

# Print the sum of the array's elements as a single integer.


puts "Enter the size of array :"
n = gets.strip.to_i
puts "Enter the elements with one space :"
arr = gets.strip

# 1st method :
arr = arr.split(' ').map(&:to_i)
sum = 0
arr.each{|i| sum+=i}
puts sum

# 2nd method :
puts arr.inject(:+)

# 3rd method :
puts arr.inject(0){|k,v| k+v}

# Passed 0 in inject method(inject(0)) because it will return 0 if we pass an empty array otherwise it will return null.

Print the fraction of the elements of an array according to positive, negative and zero in ruby.

# Given an array of integers, calculate which fraction of its elements are positive, which fraction of its elements are negative, and and which fraction of its elements are zeroes, respectively. Print the decimal value of each fraction on a new line.

# Note: This challenge introduces precision problems. The test cases are scaled to six decimal places.

# Input Format :

# The first line contains an integer, N, denoting the size of the array.
# The second line contains N space-separated integers describing an array of numbers
# (a0,a1,a2,…,an−1)(a0,a1,a2,…,an−1).

# Output Format :

# You must print the following 3 lines:

#     A decimal representing of the fraction of positive numbers in the array.
#     A decimal representing of the fraction of negative numbers in the array.
#     A decimal representing of the fraction of zeroes in the array.


puts "Enter the size of array :"
n = gets.strip.to_i
puts "Enter the elements :"
arr = gets.strip
arr = arr.split(' ').map(&:to_i)
positive_count = 0
negative_count = 0
zero_count = 0
for i in (0..n-1)
    if arr[i] > 0
        positive_count += 1
    elsif arr[i] < 0
        negative_count += 1
    else
        zero_count += 1
    end
end       
puts (positive_count/n.to_f).round(6)
puts (negative_count/n.to_f).round(6)
puts (zero_count/n.to_f).round(6)
               

Print the absolute difference between the two sums of the matrix's diagonals as a single integer in ruby.

# Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals.

# Input Format

# The first line contains a single integer, N. The next N lines denote the matrix's rows, with each line containing N space-separated integers describing the columns.

# Output Format

# Print the absolute difference between the two sums of the matrix's diagonals as a single integer.


p "Enter the size of array :"
n = gets.strip.to_i
a = Array.new(n){Array.new(n)}
for a_i in (0..n-1)
    p "Enter #{a_i+1} row elements with one space :"
    a_t = gets.strip
    a[a_i] = a_t.split(' ').map(&:to_i)
end

first_diagonal = 0
second_diagonal = 0
for i in (0..n-1)
    j=i
    first_diagonal += a[i][j]
    second_diagonal += a[i][n-1]
    n -= 1
end
difference = (first_diagonal - second_diagonal).abs
p "Difference is :", difference