Skip to content

Instantly share code, notes, and snippets.

@dharshan
dharshan / binary_gap.rb
Last active March 27, 2018 12:48
Ruby binary gap of an Integer
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 January 5, 2018 18:09
PgRoute queries on OSM data
-- 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 January 5, 2018 18:06
PostGIS operation queries on OSM data
-- 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 December 15, 2017 12:49
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.
### 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 November 29, 2017 18:24
Substring in String in Ruby using each loop
1
Enter a String
bengaluru
Enter a Sub String
uru
=> Present
2
Enter a String
bengaluru
@dharshan
dharshan / output.txt
Last active November 29, 2017 17:41
Substring in String in Ruby using each_with_index loop
1
Enter a String
bengaluru
Enter a Sub String
uru
=> Present
2
Enter a String
bengaluru
@dharshan
dharshan / output.txt
Last active November 29, 2017 18:38
Find any pairs of numbers in a sequence that add up to sum N
#### OUTPUT
1
Enter comma seperated integers
1,2,3,4,6,7,8
Enter pair sum
10
((2, 8), (3, 7), (4, 6), (6, 4), (7, 3), (8, 2))
2
Enter comma seperated integers
@dharshan
dharshan / Output
Last active November 19, 2017 13:25
Find any pairs of numbers in a sequence that add up to sum N (Using builtin combination method)
##### OUTPUT
1
Enter comma seperated integers
1,2,3,4,6,7,8
Enter pair sum
10
[[2, 8], [3, 7], [4, 6]]
2
@dharshan
dharshan / reverse_if_4.rb
Created April 28, 2017 10:16
Reverse input string if input string length is less than 4
def string_rev_if_4(input)
return input.reverse if input.length < 4
input
end
string_rev_if_4("he") # "eh"
string_rev_if_4("hel") # "leh"
string_rev_if_4("hell") # "hell"