This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
Enter a String | |
bengaluru | |
Enter a Sub String | |
uru | |
=> Present | |
2 | |
Enter a String | |
bengaluru |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 | |
Enter a String | |
bengaluru | |
Enter a Sub String | |
uru | |
=> Present | |
2 | |
Enter a String | |
bengaluru |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### OUTPUT | |
1 | |
Enter comma seperated integers | |
1,2,3,4,6,7,8 | |
Enter pair sum | |
10 | |
[[2, 8], [3, 7], [4, 6]] | |
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
NewerOlder