Skip to content

Instantly share code, notes, and snippets.

@ravi-ndra
ravi-ndra / Hash11.rb
Created September 14, 2022 10:51
Create Integer hash, Floating hash, String hash.
# h = {a: 1, b:2, c: 3.5, d: 'hello', e: 15.5, f: 'Good', g:15, h: "Morning!", i:1.5}
# Create Integer hash, Floating hash, String hash.
# Ex:
# string_hash = {a:'hello', b: 'Good', c: 'Morning!'}
# int_hash= {a: 1,b:2,c:15}
hs = {a: 1, b:2, c: 3.5, d: 'hello', e: 15.5, f: 'Good', g:15, h: "Morning!", i:1.5}
int_hash = {}
string_hash = {}
float_hash = {}
@ravi-ndra
ravi-ndra / Hash3.rb
Created September 14, 2022 10:32
Calculate sum of All Integer values form hash.
# h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, g: 'my name is namizuko', h: 15}
# calculate the sum of all integer values
hs = {a: 1,b: 2,c: 3,'d' => 4,'e' => 5,g: 'my name is Ravindra',h: 15}
sum = 0
hs.each do |key,value|
if value.class == Integer
sum += value
end
@ravi-ndra
ravi-ndra / Hash10.rb
Created September 14, 2022 10:21
Sort hash in Descending order.
# h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
# Make this hash into descending order.
hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
puts "Hash is Descending is :"
puts "#{hs.sort_by {|k,v| v}.reverse}"
@ravi-ndra
ravi-ndra / Hash9.rb
Last active September 14, 2022 10:20
Sort Hash in Ascending order.
# h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
# Make this hash into ascending order.
hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
puts "Hash is Ascending is :"
puts "#{hs.sort_by {|k,v| v}}"
@ravi-ndra
ravi-ndra / Hash8.rb
Created September 14, 2022 09:53
Find the keys of Minimum and Maximum values from Hash.
# h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
# Find the keys of Max and Min value.
hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
max = 0
min = hs[:a]
hs.each do |key,value|
if value > max
max = value
@ravi-ndra
ravi-ndra / Hash7.rb
Created September 14, 2022 09:47
Find Maximum and Minimum values from Hash.
# h1 = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
# Find the Max and Min value.
hs = {a: 100, b:123, c: 30, 'd' => 134, 'e' => 05, h: 15}
max = 0
min = hs[:a]
hs.each do |key,value|
if value > max
max = value
@ravi-ndra
ravi-ndra / Hash6.rb
Created September 14, 2022 09:45
Merge two hash.
# h1 = {a: 1, b:2, c: 3} h2 = {'d'=> 4, 'e' => 5, h: 15}
# Marge both the hash value.
hs1 = {a: 1, b:2, c: 3}
hs2 = {'d'=> 4, 'e' => 5, h: 15}
puts hs1.merge(hs2)
@ravi-ndra
ravi-ndra / Hash5.rb
Created September 14, 2022 09:33
Sum of all Even values in Hash.
# h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15}
# calculate the sum of all Even values
hs = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15}
sum = 0
hs.each do |key,value|
if value % 2 ==0
sum += value
end
@ravi-ndra
ravi-ndra / Hash4.rb
Created September 14, 2022 09:32
Sum of all Odd values in Hash.
# h = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15}
# calculate the sum off all Odd values
hs = {a: 1, b:2, c: 3, 'd' => 4, 'e' => 5, h: 15}
sum = 0
hs.each do |key,value|
if value % 2 !=0
sum += value
end
@ravi-ndra
ravi-ndra / String2.rb
Created September 14, 2022 09:08
Program to swap strings without third variable.
#Swap string values with out using third variable
str1 = "This is String 1 !"
str2 = "This is String 2 !"
puts "Before swapping :"
puts "String 1 : #{str1}"
puts "String 2 : #{str2}"