Merge branch 'master' of github.com:msaf9/Ruby

This commit is contained in:
Sahil Afrid Farookhi 2021-04-17 14:31:24 +05:30
commit 040b917b3d
2 changed files with 55 additions and 18 deletions

View file

@ -2,7 +2,7 @@
# Addition or sum of numbers means adding each and every element of the inputs # Addition or sum of numbers means adding each and every element of the inputs
# Sum or addition of 1 and 3 is 1 + 3 = 4 # Sum or addition of 1 and 3 is 1 + 3 = 4
def self_add(*array) def add(*array)
sum = 0 sum = 0
array.each { |a| sum+=a } array.each { |a| sum+=a }
puts "The sum of following elements #{array} is #{sum}" puts "The sum of following elements #{array} is #{sum}"
@ -10,11 +10,25 @@ def self_add(*array)
puts "Error: Please provide number only!" puts "Error: Please provide number only!"
end end
#
# Valid inputs # Valid inputs
self_add(1) #
self_add(2, 5, -4)
self_add(25, 45)
puts add(1)
# The sum of following elements [1] is 1
puts add(2, 5, -4)
# The sum of following elements [2, 5, -4] is 3
puts add(25, 45)
# The sum of following elements [25, 45] is 70
#
# Invalid inputs # Invalid inputs
self_add("1", 2, 3) #
self_add("a", 1)
puts add("1", 2, 3)
# Error: Please provide number only!
puts add("a", 1)
# Error: Please provide number only!

View file

@ -1,18 +1,18 @@
# A ruby program to find average median # A ruby program to find average median
# Reference: https://dev.to/colerau/how-to-find-the-median-and-mean-of-an-array-in-ruby-4f04
module AverageMedian module AverageMedian
def self.average_median(n, *array) def self.average_median(n, *array)
if n.instance_of? Integer if n.instance_of? Integer
if n == array.size if n == array.size
array.sort array.sort
if array.size%2 == 0 if array.size % 2 == 0
mid_element_1 = array.size/2 mid_element_1 = array.size/2
mid_element_2 = mid_element_1 + 1 mid_element_2 = mid_element_1 + 1
puts "The average median of the following elements #{array} is #{(array[mid_element_1-1] + array[mid_element_2-1])/2}." puts "The average median of the following elements #{array} is #{(array[mid_element_1 - 1] + array[mid_element_2 - 1]) / 2}."
else else
mid_element = (array.size + 1)/2 mid_element = (array.size + 1) / 2
puts "The average median of the following elements #{array} is #{array[mid_element-1]}." puts "The average median of the following elements #{array} is #{array[mid_element - 1]}."
end end
else else
puts "Provide the required #{n} elements properly!" puts "Provide the required #{n} elements properly!"
@ -25,13 +25,36 @@ module AverageMedian
end end
end end
#
# Valid inputs # Valid inputs
AverageMedian.average_median(2, 3, 1) #
AverageMedian.average_median(5, 1, 2, 3, 4, 5)
AverageMedian.average_median(3, 2, 2, 2)
AverageMedian.average_median(1, 5)
puts AverageMedian.average_median(2, 3, 1)
# The average median of the following elements [3, 1] is 2.
puts AverageMedian.average_median(5, 1, 2, 3, 4, 5)
# The average median of the following elements [1, 2, 3, 4, 5] is 3.
puts AverageMedian.average_median(3, 2, 2, 2)
# The average median of the following elements [2, 2, 2] is 2.
puts AverageMedian.average_median(1, 5)
# The average median of the following elements [5] is 5.
#
# Invalid inputs # Invalid inputs
AverageMedian.average_median(2, 3, 1, 5) #
AverageMedian.average_median(2, 3, "a")
AverageMedian.average_median("a", 1, 2) puts AverageMedian.average_median(2, 3, 1, 5)
# Provide the required 2 elements properly!
puts AverageMedian.average_median(2, 3, "a")
# Traceback (most recent call last):
# 4: from /Users/voliveira/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `<main>'
# 3: from /Users/voliveira/.rvm/rubies/ruby-2.7.0/bin/irb:23:in `load'
# 2: from /Users/voliveira/.rvm/rubies/ruby-2.7.0/lib/ruby/gems/2.7.0/gems/irb-1.2.1/exe/irb:11:in `<top (required)>'
# 1: from (irb):30
# NameError (undefined local variable or method `verageMedian' for main:Object)
puts AverageMedian.average_median("a", 1, 2)
# Error: Please provide number only!