Amazon Affiliate

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