Skip to content

Instantly share code, notes, and snippets.

View chiragmongia's full-sized avatar

Chirag Mongia chiragmongia

View GitHub Profile
#Inverse Case
#Overwrite the default 'to_s' method such that it inverses the case of each letter.
#Eg: "hello WORLD".to_s -> "HELLO world"
class String
def to_s
self.swapcase
end
end
#Question- Fibonacci - Yield
#Fibonacci Series upto 1000 using 'yield'.
def fibonacci_series(number)
@first_number = 0
@second_number = 1
while @first_number <= number
yield(@first_number, @second_number)
end
class String
def to_s
puts "---Swapping the case of string---"
p self.swapcase
end
end
p "Enter the string"
line = gets.chomp
line.to_s
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
def pascal_row(n)
for k in 0..n
def prime(n)
prime_no = []
a =0
if n==2
print "#{n} is prime no"
end
prime_no << 2
3.step(n,1) do |num|
2.step(num/2,1) do |k|
if num % k == 0
class Array
def power(x)
@x = x
@f= []
self.each do |i|
b = i
c = 1
@x.times{ c = c * b }
@f << c
end
p "Enter the string"
line = gets.chomp
p "Enter pattern to be searched"
pattern = gets.chomp
p matchfound = line.gsub(/#{pattern}/,"(#{pattern})")
print "Number of occurences- "
p line.scan(/#{pattern}/).size
p "Enter the string"
line = gets.chomp
arr = line.split(//)
lower = 0
upper = 0
num = 0
other = -1
for i in 0..arr.size
case arr[i]
def reverse(string)
arr = string.split
arr.reverse!
p arr.join(" ")
end
p "Enter the string"
string = gets.chomp
reverse(string)
def factorial(num)
p (1..num).inject(:*) || 1
end
p "Factorial Using Ranges"
print "Enter the number: "
num = gets.to_i
print "Factorial is: "
factorial(num)