Skip to content

Instantly share code, notes, and snippets.

@dharshan
dharshan / ruby_zip_demo.rb
Last active Jan 1, 2021
Ruby zip a folder - rubyzip
View ruby_zip_demo.rb
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 Aug 17, 2018
Standalone ruby program for accessing AWS bucket using ruby aws-sdk gem.
View cloudfx-demo.rb
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
shell script to add two numbers using function
View add2.sh
#!/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
shell script to add two numbers
View add1.sh
#!/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 Mar 27, 2018
Innova solutions count host number from file and write to other file
View innova_hckr_rank.rb
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(' ') }
@dharshan
dharshan / binary_gap.rb
Last active Mar 27, 2018
Ruby binary gap of an Integer
View binary_gap.rb
def bin_gap(number)
bin = number.to_s(2) # convert number to binary
zeros = bin.split('1') # extract 0's in binary
zeros.reject(&:empty?) # remove if any empty elements
zeros.pop if number % 2 == 0 # pop out last 0's
return 0 if zeros.empty?
zeros.map{ |zero| zero.length }.max # max length from 0's array
end
@dharshan
dharshan / pgRoute_queries.sql
Created Jan 5, 2018
PgRoute queries on OSM data
View pgRoute_queries.sql
-- SHORTEST PATH
SELECT * FROM pgr_dijkstra('SELECT gid id, source, target, cost, reverse_cost FROM ways', 645, 803, directed:=true);
-- COMBINING GEOMETRY
SELECT seq,name, node, edge, pgr.cost, agg_cost, st_astext(the_geom) AS geom FROM pgr_dijkstra('SELECT gid id, source, target, cost, reverse_cost FROM ways', 645, 803, directed:=true) AS pgr JOIN ways w ON pgr.edge = w.gid;
-- VIA
SELECT seq, node, edge, pgr.cost, agg_cost, st_astext(the_geom) AS geom FROM pgr_dijkstravia('SELECT gid id, source, target, cost, reverse_cost FROM ways', array[645, 885, 803], directed:=true) AS pgr JOIN ways w ON pgr.edge = w.gid;
-- ONE TO MANY
@dharshan
dharshan / PostGIS_queries.sql
Last active Jan 5, 2018
PostGIS operation queries on OSM data
View PostGIS_queries.sql
-- Nearby Ameneties
SELECT name, st_geomfromtext('POINT(77.64363 12.97069)') <-> way::geography AS dist, way FROM planet_osm_point WHERE amenity = 'police' ORDER BY dist ASC LIMIT 4 ;
-- Within Distance
SELECT name, ST_Geomfromtext('POINT(77.64363 12.97069)') <-> way::geography AS dist, way FROM planet_osm_point WHERE amenity = 'school' AND ST_Dwithin(way, ST_Geomfromtext('POINT(77.64363 12.97069)',4326), 1000) ORDER BY dist ASC LIMIT 4 ;
-- Finding Length
SELECT st_length(way::geography) FROM planet_osm_line WHERE osm_id = 35133687;
-- Distance between points
@dharshan
dharshan / smartest_set.rb
Created Dec 15, 2017
A smart-set is a set of distinct numbers in which all the elements have the same number of 1s in their binary form. The set of all smallest elements from each smart-set that can be formed from a given array of distinct positive numbers is known as the smartest-set.
View smartest_set.rb
### smart sets, nestaway challenge skilenza
# Pseudo code
# Take number of test case
# Take array size
# Take array elements
# Test all are within give range
# Convert array elements into binary format
# Group binary elements based on number of 1's present
# Sort grouped elements in ascending order
# take first element from each set and display
@dharshan
dharshan / output.txt
Last active Nov 29, 2017
Substring in String in Ruby using each loop
View output.txt
1
Enter a String
bengaluru
Enter a Sub String
uru
=> Present
2
Enter a String
bengaluru