Skip to content

Instantly share code, notes, and snippets.

@dharshan
dharshan / dominator.rb
Created December 26, 2022 07:21
Ruby find dominator/leader element
class Dominator
def solution(a)
counter = 0
candidate = 0
a.each do |elem|
if counter == 0
candidate = elem
counter += 1
elsif candidate == elem
@dharshan
dharshan / voracious_fish.rb
Created December 26, 2022 05:30
Voracious fish problem ruby using stack
class Fish
def solution(weights, direction)
stack = []
survivor_count = 0
weights.each_with_index do |elem, idx|
if direction[idx] == 1
stack.push(elem)
else
if stack.empty?
@dharshan
dharshan / bracket_matching.rb
Created December 26, 2022 03:27
Ruby bracket matching using stack
class BracketDemo
def solution(str)
valid = true
stack = []
str.each_char do |char|
case char
when '[', '{', '('
stack.push(char)
when ']'
@dharshan
dharshan / cyclic_rotation.rb
Last active December 25, 2022 18:29
Ruby cyclic rotation of array elements
class CyclicRotation
def solution(array, num)
new_array = Array.new(array.length)
array.each_with_index do |element, idx|
# (index + element) % array_size will give new index value to which we have to insert current value
new_array[(idx + num) % array.length] = element
end
new_array
class Solution
def solution(array)
left_part = array[0]
right_part = array.sum - left_part
diff = (left_part - right_part).abs
total = array.length
array.each_with_index do |element, index|
next if index == 0
@dharshan
dharshan / ruby_zip_demo.rb
Last active December 13, 2022 13:30
Ruby zip a folder - rubyzip
require 'zip'
input_directory = '' # directory to be zipped
zipfile_name = '' # zip-file name
# zip a folder with only files (NO SUB FOLDERS)
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
Dir[File.join(input_directory, '*')].each do |file|
zipfile.add(file.sub(input_directory, ''), file)
end
@dharshan
dharshan / cloudfx-demo.rb
Last active August 17, 2018 02:04
Standalone ruby program for accessing AWS bucket using ruby aws-sdk gem.
require './my_aws_helper'
class AwsDemo
include MyAwsHelper
def start_execution
p '1. Bucket Details, 2. Create Bucket 3. Upload To Bucket'
choice = gets.chomp.to_i
start_user_interaction(choice)
end
@dharshan
dharshan / add2.sh
Last active May 22, 2018 01:39
shell script to add two numbers using function
#!/bin/bash
# above line used to specify which shell to be used to execute the program
add() # function to add 2 numbers
{
num1=$1
num2=$2
sum=`expr $num1 + $num2` # things insed the backtick `are evaluate or executed
echo "$num1 + $num2 = $sum"
}
@dharshan
dharshan / add1.sh
Last active May 22, 2018 01:41
shell script to add two numbers
#!/bin/bash
# Above line to specify which shell to be used to execute the program
echo "Enter first Number" # echo used to print the message to screen
read first_number # read and store the input from terminal
echo "enter second Number"
read second_number
sum=`expr $first_number + $second_number` # things insed the backtick ` are evaluate or executed
echo "$first_number + $second_number = $sum" # $ to specify arguments
@dharshan
dharshan / innova_hckr_rank.rb
Created March 27, 2018 13:22
Innova solutions count host number from file and write to other file
filename = gets.strip
res = []
File.open(filename).each do |line|
res << line.split(' ').first
end
res_hash = Hash.new(0)
res.each { |i| res_hash[i] += 1 }
text_array = res_hash.to_a.map { |res| res.join(' ') }