Skip to content

Instantly share code, notes, and snippets.

View aarti's full-sized avatar
🎯
Focusing

aarti

🎯
Focusing
  • San Francisco Bay Area
View GitHub Profile
@aarti
aarti / reverse_key_value_of_hash.rb
Created January 10, 2014 17:55
Reverse Key Value in Hash
irb(main):001:0> h = { a: 1, b: 2 }
=> {:a=>1, :b=>2}
# each_with_object same as reduce or inject, memo is a new hash.
# unpack arguments in block
irb(main):002:0> h.each_with_object({}) {|(k,v),new_hash| new_hash[v] =k }
=> {1=>:a, 2=>:b}
@aarti
aarti / sorting_array_concepts.rb
Created January 23, 2014 16:34
Sort or Sort_by could do the same thing since they both take a block
a =[{n: 1}, {n: 5}, {n: 3}]
a.sort {|x,y| x[:n]<=>y[:n] } #=> [{:n=>1}, {:n=>3}, {:n=>5}]
a.sort_by{|x| x[:n].to_i } #=> [{:n=>1}, {:n=>3}, {:n=>5}]
@aarti
aarti / git_prune_branches
Created February 20, 2014 21:37
Delete unused git branches
→ git remote prune origin
→ git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
# Remember to do this from the master branch
# If you run it from a feature branch the master branch gets deleted.
→ git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
Deleted branch master (was 644eb76).
You can recheckout master branch
→ git checkout -b master origin/master
@aarti
aarti / negative_la_lb__regexp
Created February 26, 2014 16:51
Regexp using negative lookahead and negative look behind assertions
irb(main):040:0> testcases
=> ["hello world", "'hello world", "'hello' world", "hello' world"]
irb(main):041:0> testcases.each { |i| puts /(?<!')hello(?!')/.match(i) }
hello
=> ["hello world", "'hello world", "'hello' world", "hello' world"]
audience_range_values = []
age_from = nil
audience_range_values << age_from || 3 # does not work, returns book.a
print audience_range_values
audience_range_values = []
# output [nil]
@aarti
aarti / extract_largest_value.rb
Created May 2, 2014 00:29
How can I extract the bigger value for each key in a list of hashes in Ruby
Many concepts here to revisit, so saving in gist.
http://stackoverflow.com/questions/21712814/how-can-i-extract-the-bigger-value-for-each-key-in-a-list-of-hashes-in-ruby/21729254?noredirect=1#comment32860522_21729254
a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]
# the below is the main trick, to group the hashes and sorted the key/value pair
# in **ascending order**.
a.sort_by(&:to_a)
# => [{1=>12.4}, {1=>19.4}, {2=>29.4}, {2=>39.4}, {2=>59.4}, {3=>12.4}]
@aarti
aarti / string_multiplication
Created June 17, 2014 05:37
string multiplication
irb(main):001:0> 'A' * 3
=> "AAA"
@aarti
aarti / splat_operator
Created June 25, 2014 18:38
combining ranges into an array using splat operator to generate a random string of two chars
# I thought this was neat
# Reference: http://stackoverflow.com/questions/21404323/combining-two-different-ranges-to-one-in-ruby
[*'0'..'9', *'a'..'z', *'A'..'Z'].shuffle.first(2).join
@aarti
aarti / js_map_generic
Created July 8, 2014 15:20
Javascript Map Calling directly or Generically
function genericallyCallingMap(dna) {
var complements = {"C":"G", "G":"C", "T":"A", "A":"U"};
var map = Array.prototype.map
var a = map.call(dna, function(x) {
return complements[x];
})
return a.join("");
};
function callingMapOnAnArray(dna) {
var a = dna.split("").map( function(x) {
@aarti
aarti / bst.rb
Last active August 29, 2015 14:04
Binary Search Tree Count Unival
class Bst
attr_accessor :data, :left, :right
def initialize(data)
self.data = data
end
def insert_r(data)
if (self.right )