Skip to content

Instantly share code, notes, and snippets.

@dharshan
dharshan / reverse_if_4.rb
Created Apr 28, 2017
Reverse input string if input string length is less than 4
View reverse_if_4.rb
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"
@dharshan
dharshan / Output
Last active Nov 19, 2017
Find any pairs of numbers in a sequence that add up to sum N (Using builtin combination method)
View Output
##### 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 / output.txt
Last active Nov 29, 2017
Substring in String in Ruby using each_with_index loop
View output.txt
1
Enter a String
bengaluru
Enter a Sub String
uru
=> Present
2
Enter a String
bengaluru
@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
@dharshan
dharshan / output.txt
Last active Nov 29, 2017
Find any pairs of numbers in a sequence that add up to sum N
View output.txt
#### 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 / 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 / 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 / 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 / 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 / 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(' ') }